Description
Sequence of a given length n: A1,a2,..., An, recorded as A[1:n]. Similarly, A[l:r] (1≤l≤r≤n) refers to a sequence: al,al+1,..., ar-
1,ar. If 1≤l≤s≤t≤r≤n, it is called A[s:t] is a sub-sequence of a[l:r]. There are now Q queries, each asking for a given two numbers L and R,1≤l≤r
≤n, the sum of the minimum values of different sub-sequences of a[l:r]. For example, given a sequence 5,2,4,1,3, asking for a given two numbers of 1 and 3, then A[1:3] has
6 subsequence A[1:1],a[2:2],a[3:3],a[1:2],a[2:3],a[1:3], the sum of the minimum values of these 6 sub-sequences is 5+2+4+2+2+2=17.
Input
The first line of the input file contains two integers n and q, each representing the sequence length and the number of queries. Next line, containing n integers, separated by a space
, the first integer is AI, which is the value of the first element of the sequence. Next Q line, each row contains two integers l and R, representing a single query.
Output
For each query, the output line represents the answer to the query.
Preprocessing each a[i] number as the leftmost minimum value extends to the left and right to the position l[i],r[i], then l=l[i]. I,r=i. R[i], the minimum value of the interval is a[i], and the last interval is not heavy and does not leak
Let the interval endpoint l,r the point on the corresponding plane (x=l,y=r), then ask to convert to a rectangle within the and, the preprocessing changes into a rectangle plus a number
A scan line parallel to the Y axis is scanned in the negative direction of the x-axis, maintaining the answer to each y-coordinate on the scan line the coefficients of the one-time function of the x-coordinate, we can find that each rectangle plus operation can be split into four interval plus (the effect of the two coefficients of a function within the interval is a constant) The query is the interval sum (the interval within a function plus the current x to calculate the value), so that the tree array/segment tree maintenance
Time Complexity O ((n+q) logn+qlogq), the tree array is implemented with approximately 16n+4q tree array operations, and the constants are large
#include <cstdio>#include<algorithm>typedefLong Longi64;Const intn=100007;intn,q,a[n],ss[n],sp=0, ls[n],rs[n];i64 ans[n];Charbuf[n* -],*ptr=buf-1;int _(){ intx=0, f=1, c=*++ptr; while(c< -) c=='-'&& (f=-1), c=*++ptr; while(c> -) x=x*Ten+c- -, c=*++ptr; returnx*F;} i64 ks[2][n],bs[2][n];voidAdd (I64*F1,I64*F2,intw0,i64 x) { if(!w0)return; for(intW=W0;W;W-=W&-W) f1[w]+=x; X*=W0; for(intW=W0;W<=N;W+=W&-W) f2[w]+=x;} i64 sum (i64*F1,I64*F2,intw0) { if(!w0)return 0; i64 s=0; for(intW=W0;W<=N;W+=W&-W) s+=F1[w]; S*=W0; for(intw=w0-1; w;w-=w&-w) s+=F2[w]; returns;}structq{intL,r,id; voidrun () {Ans[id]=sum (ks[0],ks[1],r) *l+sum (bs[0],bs[1],r); }}qs[n];BOOL operator< (Q a,q b) {returnA.l>B.L;}structev{intL,R1,R2; I64 k,b; voidRun () {Add (ks[0],ks[1],r2,k); Add (ks[0],ks[1],r1-1,-k); Add (bs[0],bs[1],r2,b); Add (bs[0],bs[1],r1-1,-b); }}e[n*2];BOOL operator< (ConstEv&a,ConstEV&B) {returnA.l>B.L;}intep=0;intMain () {fread (buf,1,sizeof(BUF), stdin); N=_ (); q=_(); for(intI=1; i<=n;++i) a[i]=_(); for(intI=1; i<=n;++i) { while(Sp&&a[ss[sp]]>a[i]) rs[ss[sp--]]=i-1; ss[++sp]=i; } while(SP) rs[ss[sp--]]=N; for(inti=n;i;--i) { while(Sp&&a[ss[sp]]>=a[i]) ls[ss[sp--]]=i+1; ss[++sp]=i; } while(SP) ls[ss[sp--]]=1; for(intI=1; i<=n;++i) {E[ep++]= (EV) {i+1, I,rs[i],-a[i],i64 (i+1)*A[i]}; E[ep++]= (EV) {ls[i],i,rs[i],a[i],i64 (-ls[i]) *A[i]}; } std::sort (E,e+EP); for(intI=0; i<q;++i) {QS[I].L=_(); QS[I].R=_(); Qs[i].id=i; } std::sort (Qs,qs+q); for(intI=0, p=0; i<q;++i) { while(P<EP&&E[P].L>=QS[I].L) e[p++].run (); Qs[i].run (); } for(intI=0; i<q;++i) printf ("%lld\n", Ans[i]); return 0;}
bzoj4540: [Hnoi2016] Sequence