http://www.lydsy.com/JudgeOnline/problem.php?id=3289
Test instructions: ...
Idea: To find the number of exchanges is the inverse logarithm. Once this is determined, the array is discretized first. Then insert the element in the back, that is, in the interval to find the number of elements larger than it, in front of the element, is to find in the interval smaller than its number of elements. The delete operation is similar. Because the sort is small to large, so to find a larger number than it is the interval length minus less than the number of the element, so is (R-l + 1-sum (A[i])), to find a smaller number than it is (sum (a[i]-1)). Then use the MO team algorithm to deal with the interval query is good.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 using namespacestd;6 #defineN 500107 structNode {8 intL, R, id, ans;9 } P[n];Ten intBit[n], a[n], b[n], Num[n], ans, Kuai, N; One A BOOLcmpConstNode &a,ConstNode &b) { - if(A.l/kuai = = B.l/kuai)returnA.R <B.R; - returnA.l/kuai < B.L/Kuai; the } - BOOLCmpid (ConstNode &a,ConstNode &b) {returna.ID <b.id;} - intLowbit (intx) {returnX & (-x); } - voidAddintXintW) { for(; x <= n; x + = Lowbit (x)) bit[x] + =W;} + intSumintx) {intAns =0; for(; x; x-= Lowbit (x)) ans + = bit[x];returnans;} - + intMain () { A while(~SCANF ("%d", &N)) { at for(inti =1; I <= N; i++) scanf ("%d", &a[i]), b[i] =A[i]; - intQ scanf"%d", &q); - for(inti =1; I <= Q; i++) scanf ("%d%d", &P[I].L, &P[I].R), p[i].id =i; - -memset (bit,0,sizeof(bit)); -Ans =0; Kuai =sqrt (n); inSort (b +1, B +1+n); - intCNT = Unique (b +1, B +1+ N)-B-1; to for(inti =1; I <= N; i++) A[i] = Lower_bound (b +1, B +1+ CNT, a[i])-b; + -Sort (p +1, p +1+Q, CMP); the for(intL =1, R =0, i =1; I <= Q; i++) { * intL = p[i].l, r =P[I].R; $ for( ; L < L; l++) {ans-= SUM (A[l]-1); Add (A[l],-1); }Panax Notoginseng for( ; L > L; l--) {ans + = SUM (a[l-1] -1); Add (a[l-1],1); } - for( ; R < R; r++) {ans + = r-l +1-SUM (a[r+1]); Add (a[r+1],1); } the for( ; R > R; r--) {ans-= r-l +1-SUM (a[r]); Add (A[r],-1); } +P[i].ans =ans; A } theSort (p +1, p +1+Q, cmpid); + for(inti =1; I <= Q; i++) printf ("%d\n", P[i].ans); - } $ return 0; $}
Bzoj 3289:mato File Management (MO-Team algorithm + tree-like array)