Tree Set Tree
Orz ZYF god Ben
Turn Back the clock ... Reverse processing, change the deletion point to add, dynamic maintenance sequence.
Because it is dynamic, to add a bit, so use a tree-like array, but also need to find in the sequence than the current element of the number of large/small elements, so use a balance tree.
So the method is to maintain a balanced tree of the intervals represented by this node on each node of the tree array.
Why is this the right thing to do? Because the number of elements in the <k is satisfied with the interval addition , the results of multiple tree balances (a balance tree representing an interval) can be added together, which is the result of the whole interval.
(I began to think of it as a modified interval K-big thing ...) Tree-like array of sets of weight-line tree ... sigh)
Wa:ans[m+1] needs to be initialized, the example is because there is only one number left, so the inverse logarithm is 0, but the other data is obviously not the case ...
1 /**************************************************************2 problem:32953 User:tunix4 language:c++5 result:accepted6 time:8432 Ms7 memory:122372 KB8 ****************************************************************/9 Ten //Bzoj 3295 One#include <cmath> A#include <vector> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back A #defineCC (A, B) memset (A,b,sizeof (a)) at using namespacestd; - intGetint () { - intv=0, sign=1;CharCh=GetChar (); - while(!isdigit (CH)) {if(ch=='-') sign=-1; Ch=GetChar ();} - while(IsDigit (CH)) {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; in } - Const intn=100010, m=5000010, inf=~0u>>2; to Const Doubleeps=1e-8; +typedefLong LongLL; - /*******************template********************/ the intN,m,tot,l[m],r[m],s[m],rnd[m],w[m],v[m]; * #defineL L[x] $ #defineR R[x]Panax NotoginsengInlinevoidPUSH_UP (intx) { -s[x]=s[l]+s[r]+W[x]; the } +InlinevoidZigint&x) { A intT=l; L=R[T]; R[t]=x; S[T]=S[X]; PUSH_UP (x); x=T; the } +InlinevoidZagint&x) { - intS=u; R=L[T]; L[t]=x; S[T]=S[X]; PUSH_UP (x); x=T; $ } $ voidInsint&x,intnum) { - if(!x) { -X=++tot; V[x]=num; s[x]=w[x]=1; L=r=0; Rnd[x]=rand ();return; the } -s[x]++;Wuyi if(V[x]==num) w[x]++; the Else if(num<V[x]) { -Ins (l,num);if(rnd[l]<rnd[x]) Zig (x); Wu}Else{ -Ins (r,num);if(rnd[r]<Rnd[x]) zag (x); About } $ } - intRankintXintNUM) {//How many numbers are smaller than x? - if(!x)return 0; - if(V[x]==num)returnS[l]; A Else if(Num<v[x])returnrank (l,num); + Else returns[l]+w[x]+rank (r,num); the } - #undefL $ #undefR the /**********************treap********************/ the intRt[n]; the intSs[n]; the voidUpdateintXinty) { - for(inti=x;i<=n;i+=i&-i) Ins (rt[i],y); in } theLL Getbig (intXintval) { the intt=0; About for(inti=x;i;i-=i&-i) thet+=s[rt[i]]-rank (rt[i],val); the returnT; the } +LL GETSML (intXintYintval) { - intt1=0, t2=0; the for(inti=x;i;i-=i&-i)Bayit1+=rank (rt[i],val); the for(intj=y;j;j-=j&-j) thet2+=rank (rt[j],val); - returnt2-T1; - } the voidAddintx) { the for(intI=x;i<=n;i+=i&-i) ss[i]++; the } theLL sum (intx) { - intans=0; the for(intI=x;i;i-=i&-i) ans+=Ss[i]; the returnans; the }94 /*********************fenwick*******************/ the structdata{ the intV,pos; the BOOL operator< (ConstDATA&B)Const{98 returnv<B.V; About } - }a[n],b[n],d[n];101 LL Ans[n];102 intMain () {103 #ifndef Online_judge104Freopen ("Input.txt","R", stdin); the //freopen ("Output.txt", "w", stdout);106 #endif107N=getint (); m=getint ();108F (I,1, n) a[i].v=getint (), a[i].pos=i,b[i]=A[i];109Sort (b +1, b+n+1); theF (I,1, M) {111d[i].v=getint (); thed[i].pos=B[d[i].v].pos;113 } theF (I,1, N) b[i]=A[i]; theF (I,1, m) b[d[i].pos].v=0; theans[m+1]=0;117 118F (I,1, N)if(B[I].V) {119 Update (I,B[I].V); -ans[m+1]+=sum (N)-sum (B[I].V);121 Add (B[I].V);122 }123 124D (I,m,1){ theans[i]=ans[i+1]+getbig (d[i].pos-1, D[I].V) +GETSML (D[I].POS,N,D[I].V);126 Update (D[I].POS,D[I].V);127 } -F (I,1, m) printf ("%lld\n", Ans[i]);129 return 0; the}
View Code
"Bzoj" "3295" "CQOI2011" dynamic reverse order