One-class governance

Source: Internet
Author: User

There is a kind of problem about the maximum and minimum values of the interval. We can use the monotonic algorithm to solve the problem.

Spoj22343 Norma

Given a series, define the price of the interval as the result of the maximum value, minimum value, and length of the interval, and calculate the price and sum of all intervals.

Since it is sub-governance, we must deal with the answer to a series that spans the midpoint.

If the midpoint of the current series is mid, We will scan forward from mid to I.

Then, based on the monotonicity, we move to the left, the maximum value does not drop monotonically, and the minimum value does not increase monotonically.

Then we can maintain a pointer on the right to indicate the rightmost endpoint of the range that satisfies the maximum value.

Suppose there is such a situation, we can split the range into mid ~ P1, P1 ~ P2, P2 ~ R, three paragraphs.

1. Mid ~ P1, Max, and min are known, Max * min * (R-l + 1 ).

2. P1 ~ P2 and min are known. We need to calculate a sigma (max) and multiply the length of the upper range to get a prefix and calculate it.

3. maxmin is unknown. We also need to maintain the max * min * Interval Length to create a prefix and.

Code

#include<iostream>#include<cstdio>#define N 500010#define inf 1e18using namespace std;typedef long long ll;const int mod=1e9;ll ans,a[N],mn[N][2],mi[N][2],ji[N][2];int n;inline ll calc(ll l,ll r){    return ((l+r)*(r-l+1)/2)%mod;}void solve(int l,int r){    if(l==r){(ans+=a[l]*a[l]%mod)%=mod;return;}    ll mid=(l+r)>>1;    solve(l,mid);solve(mid+1,r);     ll maxn=-inf,minn=inf;    mn[mid][0]=mn[mid][1]=mi[mid][0]=mi[mid][1]=ji[mid][0]=ji[mid][1]=0;    for(ll i=mid+1;i<=r;++i){        maxn=max(maxn,a[i]);minn=min(minn,a[i]);        mn[i][0]=(mn[i-1][0]+maxn)%mod;mn[i][1]=(mn[i-1][1]+maxn*(i-mid)%mod)%mod;        mi[i][0]=(mi[i-1][0]+minn)%mod;mi[i][1]=(mi[i-1][1]+minn*(i-mid)%mod)%mod;        ji[i][0]=(ji[i-1][0]+minn*maxn%mod)%mod;ji[i][1]=(ji[i-1][1]+minn*maxn%mod*(i-mid)%mod)%mod;    }    ll p=mid+1,q=mid+1;maxn=-inf;minn=inf;    for(ll i=mid;i>=l;--i){        minn=min(minn,a[i]);maxn=max(maxn,a[i]);        while(p<=r&&a[p]>=minn)p++;        while(q<=r&&a[q]<=maxn)q++;        int ls=min(p,q),rs=max(p,q);        (ans+=maxn*minn%mod*calc(mid+1-i+1,ls-i)%mod)%=mod;        if(p<q)ans=(ans+((mi[rs-1][0]-mi[ls-1][0])*(mid-i+1)%mod+mi[rs-1][1]-mi[ls-1][1])%mod*maxn%mod+mod)%mod;        if(p>q)ans=(ans+((mn[rs-1][0]-mn[ls-1][0])*(mid-i+1)%mod+mn[rs-1][1]-mn[ls-1][1])%mod*minn%mod+mod)%mod;         ans=((ans+(ji[r][0]-ji[rs-1][0])*(mid-i+1)%mod+(ji[r][1]-ji[rs-1][1])%mod)%mod+mod)%mod;    }}int main(){    scanf("%d",&n);    for(int i=1;i<=n;++i)scanf("%lld",&a[i]);    solve(1,n);     cout<<ans;    return 0;} 

Cf526f

To simplify the meaning of the question, give a given arrangement and determine the number of intervals that meet the requirements of max-min = R-l;

As with the previous question, consider the answer that spans the midpoint. Because we need to calculate a valid range, this is related to the distribution of Max and Min.

Where the maximum and minimum values are on one side, Max, Min, and l are all known. R can be obtained through O (1). It is enough to directly judge.

If the maximum and minimum values are distributed on both sides, we can enumerate the most endpoint on one side. On the right side, we use a double pointer to maintain a range that satisfies the maximum value on the left and the minimum value on the right.

Then, max-min = R-l-> MAX + L = R + min is maintained in the bucket.

The maximum value and minimum value are on the right, and the maximum value on the left is on the right.

Code

#include<iostream>#include<cstdio>#define N 300002#define inf 0x3f3f3f3fusing namespace std;int ma[N],mi[N],a[N],l,r,tong[N<<1],n;long long ans;void solve(int l,int r){    if(l==r){ans++;return;}    int mid=(l+r)>>1;    solve(l,mid);solve(mid+1,r);    ma[mid]=0;mi[mid]=inf;    for(int i=mid+1;i<=r;++i){        ma[i]=max(ma[i-1],a[i]);        mi[i]=min(mi[i-1],a[i]);    }    ma[mid]=mi[mid]=a[mid];    for(int i=mid-1;i>=l;--i){        ma[i]=max(ma[i+1],a[i]);        mi[i]=min(mi[i+1],a[i]);    }    int p=mid+1,q;    for(int i=mid;i>=l;--i){        p=ma[i]-mi[i]+i;        if(p<=r&&p>=mid+1&&ma[p]<ma[i]&&mi[p]>mi[i])ans++;    }    p=mid;    for(int i=mid+1;i<=r;++i){        p=i-ma[i]+mi[i];        if(p>=l&&p<=mid&&ma[p]<ma[i]&&mi[p]>mi[i])ans++;    }     p=q=mid;    for(int i=mid+1;i<=r;++i){        while(ma[p]<ma[i]&&p>=l)tong[mi[p]-p+n]++,p--;        while(mi[q]>mi[i]&&q>p)tong[mi[q]-q+n]--,q--;        ans+=tong[ma[i]-i+n];    }    while(q>p)tong[mi[q]-q+n]--,q--;    p=q=mid+1;    for(int i=mid;i>=l;--i){        while(ma[p]<ma[i]&&p<=r)tong[mi[p]+p]++,p++;        while(mi[q]>mi[i]&&q<p)tong[mi[q]+q]--,q++;        ans+=tong[ma[i]+i];    }    while(q<p)tong[mi[q]+q]--,q++;}int main(){    scanf("%d",&n);    for(int i=1;i<=n;++i){        scanf("%d%d",&l,&r);        a[l]=r;    }    solve(1,n);    cout<<ans;    return 0;}

 

One-class governance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.