日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 602D】Lipshitz Sequence(思维,单调栈,斜率单调性) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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(思维,单调栈,斜率单调性)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。