Bzoj3289mato's file Management
Test instructions
A total of n data, randomly selected a daily interval [L,r],mato in the order of documents from small to large to see the numbers in this interval. He first copies the files to be read in numbered order, and then sorts the files by the sort program. The number of exchanges that are sorted daily.
Exercises
It's still the team, but when it's transferred, the number of reverse orders is maintained in a tree-like array, with the total Complexity O (nsqrt (n) log2n). Because it is inserted from the large to the small, so the maintenance should use r-l+1 minus.
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <cmath>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineLowbit (a) a& (a)7 #definell Long Long8 using namespacestd;9 Ten structnd1{intx, y;}; One BOOLCMP1 (Nd1 A,nd1 b) {returna.x<b.x;} A structnd2{intL,pl,r; ll ans;intID;}; - BOOLCMP2 (Nd2 A,nd2 b) {if(a.pl!=b.pl)returna.pl<b.pl;if(A.R!=B.R)returna.r<b.r;returna.l<B.L;} - BOOLCmp3 (Nd2 A,nd2 b) {returna.id<b.id;} thell c[100000],ans,l,r;intn,q,ls[100000],pos[100000]; Nd1 f[100000]; Nd2 ask[100000]; -InlinevoidAddintX,ll y) { while(x<=n) c[x]+=y,x+=lowbit (x);} -Inline ll Query (intx) {ll qy=0; while(x>=1) qy+=c[x],x-=lowbit (x);returnqy;} - intMain () { +scanf"%d", &n); Inc (I,1, N) scanf ("%d", &f[i].x), f[i].y=i; Sort (f+1, f+n+1, CMP1); -Inc (I,1, n) ls[f[i].y]=i;intSz= (int) sqrt (n); Inc (I,1, n) pos[i]= (i-1)/sz+1; +scanf"%d", &q); Inc (I,1, q) {intb; scanf"%d%d", &a,&b); Ask[i]= (Nd2) {a,pos[a],b,0, i};} ASort (ask+1, ask+1+Q,CMP2); Memset (c,0,sizeof(c)); ans=0; L=1; R=0; atInc (I,1, Q) { - while(R<ASK[I].R) {intx=r+1; X=N-LS[X]; ll A1=query (x); ANS+=A1; Add (x+1,1); r++;} - while(L>ASK[I].L) {intx=l-1; X=N-LS[X]; ll a1= (r-l+1)-query (x); ANS+=A1; Add (x+1,1); l--;} - while(R>ASK[I].R) {intX=r; X=N-LS[X]; ll A1=query (x); ANS-=A1; Add (x+1,-1); r--;} - while(L<ASK[I].L) {intX=l; X=N-LS[X]; ll a1= (r-l)-query (x); ANS-=A1; Add (x+1,-1); l++;} -ask[i].ans=ans; in } -Sort (ask+1, ask+1+q,cmp3); Inc (I,1, q) printf ("%lld\n", Ask[i].ans); to return 0; +}
20160408
Bzoj3289mato's file Management