Portal: https://www.luogu.org/problemnew/show/P2801
Reference: Http://hzwer.com/2784.html feel the idea is extremely clear;)
PS: I'm in Bzoj A, I want permission;
Test instructions: How many times the interval query has a number than k;
Ideas: block, both sides of the violence Update and query, the intermediate query is counted with two minutes, each update, if necessary, remember to re-sort (another array corresponding to the interval);
#include <iostream>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<string>using namespaceStd;typedefLong Longll;Const intMAXN =1000009;Const intBMAXN =1009; ll A[MAXN],B[MAXN],ADD[BMAXN];intn,m;intBELONG[MAXN]; intNUM,L[BMAXN],R[BMAXN];//The left end of the block; I, the right side of this block.intBlock//Block SizevoidResetintID) { intLe = L[id],ri =R[id]; for(intI=le; i<=ri; i++) B[i] =A[i]; Sort (b+le,b+ri+1);}voidBuild () {block=sqrt (n); Num= N/block;if(N%block) num++; for(intI=1; i<=num; i++) L[i]= (I-1) *block+1, r[i] = i *Block; R[num]=N; for(intI=1; i<=n; i++) Belong[i]= (I-1)/block +1; for(intI=1; I <= num; i++) reset (i);}voidUpdateintLxintrx,ll val) { if(belong[lx]==Belong[rx]) { for(inti=lx;i<=rx;i++) a[i]+=Val; Reset (BELONG[LX]); } Else { intLi = R[belong[lx]],ri =L[belong[rx]]; for(inti = LX; i<=li; i++) a[i]+=Val; Reset (BELONG[LX]); for(inti = ri; i<=rx; i++) a[i]+=Val; Reset (Belong[rx]); for(inti = Belong[lx] +1; i < BELONG[RX]; i++) add[i]+=Val; }}ll Query (intLxintrx,ll k) {LL res=0; if(belong[lx]==Belong[rx]) { for(inti=lx;i<=rx;i++)if(A[i] >= K-ADD[BELONG[LX]) res++; } Else { intLi = R[belong[lx]],ri =L[belong[rx]]; //cout<<li<< "" <<ri<<endl; for(inti = LX; I <= Li; i++)if(A[i] >= K-ADD[BELONG[LX]) res++; for(inti = ri; I <= Rx; i++)if(A[i] >= K-add[belong[rx]) res++; for(inti = Belong[lx] +1; i<belong[rx]; i++) { intLe = L[i],ri =R[i]; while(Le <=RI) { intMid = (Le+ri) >>1; if(B[mid] < K-add[i]) Le= Mid +1; ElseRI = mid-1; } //printf ("%d\n", le); //cout<<r[i]-le+1<<endl;Res+=r[i]-Le +1; } } returnRes;}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n; i++) scanf ("%lld", &A[i]); Build (); for(intI=1; i<=m; i++) { Chars[ -]; intx, y; ll v; scanf ("%s%d%d%lld",s,&x,&y,&v); if(s[0]=='M') {update (X,Y,V); } Else{ll ans=query (X,Y,V); printf ("%lld\n", ans); } } return 0;}
BZOJ-3343 's Magic + chunking (chunk sorting two points)