【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 9Output
17 82 23 210Input
7 6 5 7 7 4 6 6 2 1 2 2 3 2 6 1 7 4 7 3 5Output
2 0 22 59 16 8Note
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(思维,单调栈,斜率单调性)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: svcinit.exe - svcini
- 下一篇: svcproc.exe - svcpro