1. 程式人生 > >Codeforces 602D Lipshitz Sequence【思維+斜率單調棧】

Codeforces 602D Lipshitz Sequence【思維+斜率單調棧】

D. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

A function  is called Lipschitz continuous if there is a real constant K such 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 l

i 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.


題目大意:

L(h)的值是區間【L,R】內,abs(h[i]-h[j])/(i-j)的最大值。

現在有q個詢問,每個詢問表示詢問區間【L,R】內,所有連續的子序列的L(h)的值的和。

思路:

①觀察到是斜率的最大值,所以能夠很容易考慮到有決策單調性。(比如位子i所選擇的最優的位子是j,那麼對於位子i+1來講,肯定選取的位子是大於等於j的);而又考慮是兩個值的差除以距離差,那麼我們很容易考慮到問題選擇的單調性還是相鄰的(位子i所選取的最優一定是i-1);

②那麼我們維護一個單調棧,棧頂元素最小,那麼過程中,彈出棧頂的元素能夠作用到的最右邊的位子就是i-1;新加入進去的元素能夠作用到的最左邊的位子就是棧頂元素所在原來陣列的位子+1.

③那麼對於每個位子所能夠貢獻的價值就是L【i】*R【i】*a【i】;

那麼對於每個查詢,我們O(nq)去統計即可。

Ac程式碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
#define ll __int64
struct node
{
    ll val,pos;
}now,nex;
ll a[150000];
ll R[150000];
ll L[150000];
int main()
{
    ll n,m;
    while(~scanf("%I64d%I64d",&n,&m))
    {
        stack<node>s;
        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(ll i=n;i>=1;i--)a[i]=abs(a[i]-a[i-1]);
        for(ll i=1;i<=n;i++)
        {
            while(!s.empty())
            {
                now=s.top();
                if(a[i]>now.val)
                {
                    R[now.pos]=i-1;
                    s.pop();
                }
                else break;
            }
            if(s.size()==0)L[i]=1;
            else
            {
                now=s.top();
                L[i]=now.pos+1;
            }
            now.val=a[i],now.pos=i;
            s.push(now);
        }
        while(!s.empty())
        {
            now=s.top();
            R[now.pos]=n;
            s.pop();
        }
        while(m--)
        {
            ll x,y;scanf("%I64d%I64d",&x,&y);
            ll output=0;
            for(ll i=x+1;i<=y;i++)
            {
                ll LL=max(x+1,L[i]);
                ll RR=min(y,R[i]);
                output+=(i-LL+1)*(RR-i+1)*a[i];
            }
            printf("%I64d\n",output);
        }
    }
}