BZOJ3745: [Coci2015]norma

Source: Internet
Author: User

Description

Input line 1th, an integer n, line 2~n+1, an integer per line representing sequence a.

Output outputs the result of the answer to the 10^9 modulo.

Sample Input4
2
4
1
4Sample Output109

"Data Range"
N <= 500000
1 <= a_i <= 10^8HINT Source

Acknowledgement Dzy

That's ********.

Considering the divide-and-conquer approach, the question turns into how to solve the cross-midpoint answer.

For a sequence [i,j], the reverse enumeration of the left endpoint I, the entire value of MAXV, MINV, there are four possible:

1. All are taken at the left half.

Calculate the right end to meet the conditions up to where, remember as cur, because of monotonicity, can O (1) to beg.

ans+=maxvl*minvl* ((mid+1-i+1) + (mid+2-i+1) + + (cur-i+1))

Deformation, with arithmetic progression sum can O (1) Forget it.

2.MAXV is taken on the left, MINV in the right.

Calculate the right end to meet the conditions up to where, remember as CURMN, because of monotonicity, can O (1) to beg.

ans+=maxvl*[minvj* (j-i+1) +minvj+1* (j+1-i+1) + +minvcurmn* (curmn-i+1)]

Deformation, maintenance of minvj*j and MINVJ prefix and can O (1) Forget it.

3.MINV is MAXV on the right, and the left is taken.

Calculate the right end to meet the conditions up to where, remember as CURMX, because of monotonicity, can O (1) to beg.

ans+=minvl*[maxvj* (j-i+1) +maxvj+1* (j+1-i+1) + +maxvcurmx* (curmx-i+1)]

Deformation, maintenance of maxvj*j and MAXVJ prefix and can O (1) Forget it.

4. All are taken in the right half.

ans+=maxvj*minvj* (j-i+1) + +maxvr*minvr* (r-i+1).

Deformation, maintenance of maxvj*minvj*j and MAXVJ*MINVJ prefix and can O (1) Forget it.

It's really easy to say!

#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i!=-1;i=next[i])using namespacestd;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) {        intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +l; }    return*head++;} InlineintRead () {intx=0, f=1;CharC=Getchar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=500010;Const intMod=1000000000; typedefLong Longll;structPair {ll mx,mn;} Q1[MAXN],Q2[MAXN]; Pair update (pair A,ll v) {a.mx=max (a.mx,v); a.mn=min (a.mn,v); returnA;}intn;ll a[maxn],ans,s1[maxn],s2[maxn],s3[maxn],ss1[maxn],ss2[maxn],ss3[maxn];ll Sumlen (intBintSintt) {if(s>t)return 0; return(LL) (s-b+1+t-b+1) * (t-s+1)/2%MoD;}voidSolveintLintr) {if(r-l+1<=Ten) {Rep (i,l,r) {ll MX=a[i],mn=A[i]; Rep (j,i,r) {MX=max (Mx,a[j]); mn=min (mn,a[j]); (Ans+ = (j-i+1) *mx*mn)%=MoD; }        }        return; }    intMid=l+r>>1; Solve (l,mid); Solve (mid+1, R); Q1[mid]= (Pair) {A[mid],a[mid]};d WN (i,mid-1, l) q1[i]=update (q1[i+1],a[i]); Q2[mid+1]= (Pair) {a[mid+1],a[mid+1]};rep (i,mid+2, R) q2[i]=update (q2[i-1],a[i]); Q1[l-1]=q2[r+1]= (Pair) {1e9,-1e9}; S1[mid]=s2[mid]=s3[mid]=ss1[mid]=ss2[mid]=ss3[mid]=0; Rep (I,mid+1, R) {S1[i]= (s1[i-1]+q2[i].mx* (i-mid))%MoD; Ss1[i]= (ss1[i-1]+q2[i].mx)%MoD; S2[i]= (s2[i-1]+q2[i].mn* (i-mid))%MoD; Ss2[i]= (ss2[i-1]+Q2[I].MN)%MoD; S3[i]= (s3[i-1]+q2[i].mx*q2[i].mn%mod* (i-mid))%MoD; Ss3[i]= (ss3[i-1]+q2[i].mn*q2[i].mx)%MoD; }    intCur=mid+1, curmx=mid+1, curmn=mid+1; Dwn (i,mid,l) { while(q2[cur].mx<=q1[i].mx&&q2[cur].mn>=q1[i].mn) cur++;  while(q2[curmx].mx<=q1[i].mx) curmx++;  while(q2[curmn].mn>=q1[i].mn) curmn++; ll T=ans; (Ans+=sumlen (i,mid+1, cur-1) *q1[i].mx%mod*q1[i].mn%mod)%=MoD; if(curmn-1&GT;=CURMX) (ans+=q1[i].mn* (s1[curmn-1]-s1[curmx-1]+mod+ (ss1[curmn-1]-ss1[curmx-1]+MOD) * (mid-i+1)) (%mod))%=MoD; if(curmx-1&GT;=CURMN) (ans+=q1[i].mx* (s2[curmx-1]-s2[curmn-1]+mod+ (ss2[curmx-1]-ss2[curmn-1]+MOD) * (mid-i+1)) (%mod))%=MoD; intp=Max (CURMX,CURMN); if(P&LT;=R) (ans+=s3[r]-s3[p-1]+mod+ (ss3[r]-ss3[p-1]+MOD) * (mid-i+1))%=MoD; }}intMain () {n=read (); Rep (I,1, N) a[i]=read (); Solve (1, n);p rintf ("%lld\n", ans); return 0;}
View Code

BZOJ3745: [Coci2015]norma

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.