Ref: 51203960
Really not good at this kind of ...
Divide and conquer, for one (L,r), first recursion (L,mid), (Mid+1,r), and then this interval to the answer is through the mid-range
We first preprocess the Mx*mn*len prefix of the right endpoint of mid for L and prefix to mx*mn and then enumerate the left endpoint, and the right endpoint maintains two subscript j,k, representing the legal right endpoint of MN and MX at the left endpoint, respectively
Then deal with three cases, assuming j<k
1. When the right end point is (MID+1,J), the direct calculation
2. Right endpoint at (j,k), equivalent to minimum value in J, maximum value in (j,k), preprocessing a prefix and to calculate
3. The right endpoint at (K,R), directly with the preprocessing prefix and processing, to subtract the right endpoint at (MID+1,J) when the situation
#include <iostream> #include <cstdio>using namespace Std;const int N=500005,mod=1e9;int n,ans,a[n],c[n][2] , F[n],g[n],p[n][2],q[n][2];int read () {int r=0,f=1; Char P=getchar (); while (p> ' 9 ' | | p< ' 0 ') {if (p== '-') f=-1; P=getchar (); } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48; P=getchar (); } return r*f;} void Jia (int &x,int y) {x+=y; x>=mod?x-=mod:0;} void Jian (int &x,int y) {x-=y; x<0?x+=mod:0;} int CLC (int x,int y) {return 1ll* (x+y) * (y-x+1)/2%mod;} void wk (int l,int r) {if (l==r) {Jia (ans,1ll*a[l]*a[l]%mod); Return } int mid= (L+R) >>1; WK (L,mid); WK (mid+1,r); C[mid][0]=c[mid][1]=a[mid]; for (int i=mid-1;i>=l;i--) c[i][0]=min (C[i+1][0],a[i]), C[i][1]=max (C[i+1][1],a[i]); int mn=1e9,mx=-1e9; f[mid]=g[mid]=p[mid][0]=p[mid][1]=q[mid][0]=q[mid][1]=0; for (int i=mid+1;i<=r;i++) {mn=min (Mn,a[i]), Mx=max (Mx,a[i]); f[i]=1ll*mn*mx%mod* (I-mid)%mod; Jia (F[i],f[i-1]); G[i]=1ll*mn*mx%mod; Jia (G[i],g[i-1]); p[i][0]= (P[I-1][0]+MN)%mod; q[i][0]= (Q[I-1][0]+MX)%mod; p[i][1]=1ll*mn* (I-mid)%mod; Jia (p[i][1],p[i-1][1]); q[i][1]=1ll*mx* (I-mid)%mod; Jia (q[i][1],q[i-1][1]); } for (int i=mid,j=mid,k=mid;i>=l;i--) {while (j<r&&c[i][0]<a[j+1]) j + +; while (k<r&&c[i][1]>a[k+1]) k++; Jia (ANS,1LL*C[I][0]*C[I][1]%MOD*CLC (mid-i+2,min (j,k)-i+1)%mod); Jia (ans, (1ll*g[r]* (mid-i+1) +f[r])%mod); Jian (ans, (1ll*g[max (j,k)]* (mid-i+1) +f[max (j,k)])%mod); if (j<k) {Jia (ans, (1ll*p[k][0]* (mid-i+1) +p[k][1])%mod*c[i][1]%mod); Jian (ans, (1ll*p[j][0]* (mid-i+1) +p[j][1])%mod*c[i][1]%mod); } else {Jia (ans, (1ll*q[j][0]* (mid-i+1) +q[j][1])%mod*c[i][0]%mod); Jian (ans, (1ll*q[k][0]* (mid-i+1) +q[k][1])%moD*C[I][0]%MOD); }}}int Main () {n=read (); for (int i=1;i<=n;i++) a[i]=read (); WK (1,n); printf ("%d\n", ans); return 0;}
Bzoj 3745: [Coci2015]norma "Divide and conquer"