题干:

A function  is called Lipschitz continuous if there is a real constant Ksuch that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all . We'll deal with a more... discrete version of this term.

For an array , we define it's Lipschitz constant  as follows:

  • if n < 2, 
  • if n ≥ 2,  over all 1 ≤ i < j ≤ n

In other words,  is the smallest non-negative integer such that |h[i] - h[j]| ≤ L·|i - j| holds for all 1 ≤ i, j ≤ n.

You are given an array  of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .

Input

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ q ≤ 100) — the number of elements in array  and the number of queries respectively.

The second line contains n space-separated integers  ().

The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri (1 ≤ li < ri ≤ n).

Output

Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .

Examples

Input

10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9

Output

17
82
23
210

Input

7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5

Output

2
0
22
59
16
8

Note

In the first query of the first sample, the Lipschitz constants of subarrays of  with length at least 2 are:

The answer to the query is their sum.

题目大意:

给定n(n<=1e5)个整数的数组h[i],现在定义一个函数

L(h)的值是区间[L,R]内,abs ( h[i]-h[j] ) / ( i-j )的最大值。

现在有q个询问,每个询问表示询问区间[L,R]内,所有连续子序列的L(h)的值的和。

解题报告:

首先分析L(h)函数,从定义上看就是斜率的绝对值的最大值。

一下一段来自题解:

观察到是斜率的最大值,所以能够很容易考虑到有决策单调性。(比如位子i所选择的最优的位子是j,那么对于位子i+1来讲,肯定选取的位子是大于等于j的);而又考虑是两个值的差除以距离差,那么我们很容易考虑到问题选择的单调性还是相邻的(位子i所选取的最优一定是i-1);

(但是我感觉并没有决策单调性呀、、、)

所以讲斜率弄成一个新数组,然后单调栈乱搞一下就行了。去重方法和51nod那个题一样。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int L[MAX],R[MAX];
int n,q;
int a[MAX],b[MAX];
stack<int> sk;
ll ans;
int main()
{cin>>n>>q;for(int i = 1; i<=n; i++) cin>>a[i];for(int i = 1; i<n; i++) b[i] = abs(a[i+1] - a[i]);n--;for(int i = 1; i<=n; i++) {while(!sk.empty() && b[sk.top()] <= b[i]) sk.pop();L[i] = sk.empty() ? 0 : sk.top();sk.push(i);}while(sk.size()) sk.pop();for(int i = n; i>=1; i--) {while(!sk.empty() && b[sk.top()] < b[i]) sk.pop();R[i] = sk.empty() ? n+1 : sk.top();sk.push(i);}while(q--) {ans=0;int l,r,LL,RR;scanf("%d%d",&l,&r);for(int i = l; i<=r-1; i++) {LL=max(L[i],l-1);RR=min(R[i],r);ans += 1LL*b[i]*(RR-i)*(i-LL);//b[i] * (RR - LL - 1);}printf("%lld\n",ans);}return 0 ;
}

【CodeForces - 602D】Lipshitz Sequence(思维,单调栈,斜率单调性)相关推荐

  1. CodeForces - 1484E Skyline Photo(dp+单调栈)

    题目链接:点击查看 题目大意:给出 nnn 个建筑,每个建筑有一个高度和一个美丽值,现在要求划分为数个连续的区间,使得所有区间的贡献之和最大,其中每个区间的贡献值为,区间中高度最低的建筑物的美丽值 题 ...

  2. 【Codeforces 631C 】Report(单调栈,思维模拟)

    题干: Each month Blake gets the report containing main economic indicators of the company "Blake ...

  3. CodeForces - 1407D Discrete Centrifugal Jumps(单调栈+dp)

    题目链接:点击查看 题目大意:给出 n 个大楼的高度记为 h,现在需要从第一个大楼到达第 n 个大楼,问最小步数是多少,只有满足以下条件时才能从 i 移动到 j ,设 i < j: 题目分析:无 ...

  4. CodeForces - 1313C2 Skyscrapers (hard version)(单调栈+dp/分治)

    题目链接:点击查看 题目大意:给出 n 块连续的空地可以建造摩天大楼,政府有规定,每块地最高只能建 a[ i ] 的高度,同时每栋大楼需要满足一个规则,即每栋大楼的两侧不允许同时存在比自己高的大楼,输 ...

  5. CodeForces - 548D Mike and Feet(单调栈)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列,现在规定对于任意长度区间为 len 的答案为,所有长度为 len 的区间内的最小值的最大值,题目要求我们输出len为 1 ~ n 时的答案 题 ...

  6. 0x11.基本数据结构 — 栈与单调栈

    目录 一.栈 0.AcWing 41. 包含min函数的栈 (自己造栈) 1.AcWing 128. 编辑器 (对顶栈) 2.AcWing 129. 火车进栈 3.AcWing 130. 火车进出栈问 ...

  7. 【数据结构】单调栈和单调队列 详解+例题剖析

    算法:单调栈和单调队列 一.单调栈和单调队列 二.单调栈例题 1.模板题入门 2.不懂不要急,看这道题 三.单调队列例题 1.入门 2.进阶 一.单调栈和单调队列 单调栈和单调队列与普通的栈,队列不同 ...

  8. 单调栈是单调递增还是单调递减?

    一言以蔽之,最后的需求是什么,单调栈的单调性就是什么. 如果要找第一个小于的元素,单调栈即为递增:找第一个大于的元素,单调栈即为递减.当前元素与目标元素的大小关系就是单调栈内部元素的大小关系.

  9. 算法笔记(三)特殊数据结构——哈希表、有序表、并查集、KMP、Manacher、单调栈、位图、大数据类题

    layout: post title: 算法笔记(三)特殊数据结构--哈希表.有序表.并查集.KMP.Manacher.单调栈.位图.大数据类题 description: 算法笔记(三)特殊数据结构- ...

最新文章

  1. Cortex-M3启动深度解析
  2. Swift中一个类中的枚举(enum)类型的数据该如何实现序列化(NSCoder)
  3. Android Studio中ButterKnife插件的安装与使用
  4. 设计模式-行为-观察者
  5. Sentinel(十四)之控制台
  6. Java面向对象编程 第一章 面向对象开发方法概述
  7. 为Java应用程序提供了空前的代码保护控件DashO-Pro
  8. Mac下关于ssh命令的简化
  9. 基于豆瓣影评数据的文本分析系统【数据爬取+数据清洗+数据库存储+LDA主题挖掘+词云可视化】
  10. 获取微信商户平台操作证书
  11. 无法删除文件,无法读源文件或磁盘
  12. 【历史上的今天】4 月 27 日:Tumblr 上线;施乐推出了 Star 工作站;第一台安德伍德打字机诞生
  13. 2021湖北省技能高考成绩查询,刚刚!湖北高考查分及志愿填报时间公布!
  14. android做开场动画,Android_Android开场动画类完整实现代码,本文所述实例为在android中开起 - phpStudy...
  15. 如何批量将多个 PPT 文档中的图片提取出来
  16. Java基于SSH框架的银行业务管理系统
  17. 神秘代码(链接至steam指南)
  18. [RK3288][Android6.0] 不同分辨率的bootanimation.zip下载
  19. 团体程序设计天梯赛-练习集 L1
  20. 【工作技巧】医疗行业标准查询方式

热门文章

  1. 安装用户debian7安装oracle11g
  2. C# 线程手册 第三章 使用线程 Monitor.TryEnter()
  3. POJ 2240题(Floyd)
  4. sharepoint站点移植方案
  5. etlgr是什么服务器_ETL是指什么 - 金融行业 - ITPUB论坛-中国专业的IT技术社区
  6. xml生成2维码_MyBatis(2)之MyBatis-Generator最佳实践
  7. OpenCv的连通域操作
  8. linux脚本ls输出到变量中,bash – 将命令输出的错误消息存储到shell变量中
  9. java处理中文字符串_Java实现读取文章中重复出现的中文字符串
  10. ubuntu php7 memcache,linux上安装php7 memcache扩展