MO Team algorithm, offline answer inquiry, by a certain size (sqrt (N*log (n)) the answer block, according to the ① left end of the block ② the right end of the double keyword sorting.
And then the violence shifts.
Use a tree-like array when transferring.
O (N*SQRT (n) *log (n)).
Note: ① adds a number after the number of columns, and the inverse logarithm increases the number of numbers that are larger in the sequence.
② deletes a number after the number of columns, and the inverse logarithm decreases the number of numbers in the series that are larger than it.
③ adds a number to the front of a column number, and the inverse logarithm increases the number of smaller numbers in the sequence.
④ deletes a number in front of a column number, and the inverse logarithm decreases the number of smaller numbers in the sequence.
1#include <cstdio>2#include <algorithm>3#include <cmath>4 using namespacestd;5 structpoint{intx, y;} a[50001];6 structask{intL,r,lb,p;} q[50001];7 BOOL operator< (ConstASK &a,ConstASK &b) {returnA.lb!=b.lb? a.lb<b.lb:a.r<B.R;}8 BOOL operator< (ConstPoint &a,ConstPoint &b) {returna.x<b.x;}9 intn,m,sz,sum,num[50001],b[50001],cnt;Ten intd[50001],ans[50001],res; OneInlineintLowbit (Const int&X) {returnx& (-x);} AInlinevoidAddintPConst int&X) { while(p<=n) {d[p]+=x;p+=lowbit (P);}} -InlineintGetsum (intP) {intres=0; while(p) {Res+=d[p];p-=lowbit (p);}returnRes;} - intRes,num;Charc,ch[ A]; theInlineintG () - { -res=0; C='*'; - while(c<'0'|| C>'9') c=GetChar (); + while(c>='0'&&c<='9') {res=res*Ten+ (C-'0'); C=GetChar ();} - returnRes; + } AInlinevoidPLong Longx) at { -num=0;if(!x) {Putchar ('0');p UTS ("");return;} - while(x>0) ch[++num]=x%Ten, x/=Ten; - while(Num) Putchar (ch[num--]+ -); -Puts""); - } in voidMakeblock () - { toSz=sqrt ((Double) n4.6);intL,r; + for(sum=1; sum*sz<n;sum++) - { theL= (sum-1) *sz+1; r=sum*sz; * for(inti=l;i<=r;i++) num[i]=sum; $ }Panax Notoginsengl=sz* (sum-1)+1; R=N; - for(inti=l;i<=r;i++) num[i]=sum; the } + voidLisan () A { the inten=1; Sort (A +1, a+n+1); b[a[1].y]=1; + for(intI=2; i<=n;i++) - { $ if(a[i].x!=a[i-1].x) en++; $b[a[i].y]=en; - } - } the intMain () - {Wuyin=G (); the for(intI=1; i<=n;i++) - { Wua[i].x=G (); -a[i].y=i; About } $Makeblock (); Lisan (); m=G (); - for(intI=1; i<=m;i++) - { -Q[i].l=g (); Q[i].r=G (); Aq[i].lb=NUM[Q[I].L]; +q[i].p=i; the } -Sort (q+1, q+m+1); $ for(inti=q[1].l;i<=q[1].r;i++) the { thecnt++; theAdd (B[i],1); theres+= (cnt-getsum (B[i])); -} ans[q[1].p]=Res; in for(intI=2; i<=m;i++) the { the if(q[i].l<q[i-1].l) About { the for(intj=q[i-1].l-1; j>=q[i].l;j--) the { theRes+=getsum (b[j]-1); +Add (B[j],1); - } thecnt+= (q[i-1].l-q[i].l);Bayi } the Else the { - for(intj=q[i-1].l;j<q[i].l;j++) - { theRes-=getsum (b[j]-1); theAdd (b[j],-1); the } thecnt-= (q[i].l-q[i-1].l); - } the if(q[i].r<q[i-1].R) the { the for(intj=q[i-1].r;j>q[i].r;j--)94 { theres-= (cnt-getsum (B[j])); thecnt--; theAdd (b[j],-1);98 } About } - Else101 {102 for(intj=q[i-1].r+1; j<=q[i].r;j++)103 {104cnt++; theAdd (B[j],1);106res+= (cnt-getsum (B[j]));107 }108 }109ans[q[i].p]=Res; the }111 for(intI=1; i<=m;i++) P (Ans[i]); the return 0;113}
"Mo Team Algorithm" bzoj3289 Mato's file management