Tree-like array this really thought for a long time, still did not want to come out%%% www.cppblog.com/Yuan/archive/2010/08/18/123871.html
Tree-like array of prefixes and greater than or equal to the maximum value of K, the first time to see this method, it is amazing, is not read = =
Two points can also be asked, but the feeling will be slower ...
The idea is to compress all the numbers without asking.
For example, if n equals 10, you ask for 2, 7 is probably the case.
"3,4,5,6,7" "8,9,10"
1 2 3
Divided into 3 pieces, up to Q block, realize discretization.
#include <algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespacestd;Const intN =200005;intOp[n], a[n], b[n], p[n], no[n];intN, Q;structBIT {intArr[n]; intN; intSumintp) {intAns =0; while(P) {ans+=Arr[p]; P-=lowbit (P); } returnans; } voidAddintPintv) { while(P <=N) {arr[p]+=v; P+=lowbit (P); } } intFindintK) {//find the first position greater than or equal to K in the array intpos =0, cnt =0; for(inti = -; I >=0; --i) {pos+= (1<<i); if(POS >= N | | cnt + ARR[POS] >= k) pos-= (1<<i); ElseCNT + =Arr[pos]; } returnpos+1; } voidInitintN) { This->n =N; memset (arr,0,sizeofarr); } intLowbit (intx) {returnx&-x; }} bit;intMain () {//freopen ("In.txt", "R", stdin); intT, CAS =0; scanf ("%d", &T); while(t--) {printf ("Case %d:\n", ++CAs); scanf ("%d%d", &n, &q); Charch[Ten]; intIDX =0; for(inti =1; I <= Q; ++i) {scanf ("%s%d", CH, &A[i]); if(*ch = ='T') Op[i] =1; Else if(*ch = ='Q') Op[i] =2; ElseOp[i] =3; if(Op[i] <3) B[++idx] =A[i]; } b[++IDX] =N; Sort (b+1, B +1+idx); N= Unique (b +1, B +1+IDX)-B-1; Bit.init (2*q); for(inti =1; I <= N; ++i) {bit.add (q+i, b[i]-b[i-1]); No[q+i] = B[i];//No[i] array I at the number original number!! P[i] = q+i;//P[i] Location of number I } inttop =Q; for(inti =1; I <= Q; ++i) {if(Op[i] = =1) { intx = Lower_bound (b +1, B +1+n, A[i])-b; Bit.add (P[x],-1);//to move x to the top p[x] the number of digits in the position is reduced by oneno[p[x]]--;//x go, and the rest is x-1 .P[X] = top;//the position of X becomes topBit.add (Top,1);//top position has a number x +1No[top] =A[i]; Top--; } Else if(Op[i] = =2) { intx = Lower_bound (b +1, B +1+n, A[i])-b; printf ("%d\n", Bit.sum (p[x])); } Else { intpos =Bit.find (A[i]); intSP =bit.sum (POS); if(sp = = A[i]) printf ("%d\n", No[pos]); Elseprintf"%d\n", no[pos]-(sp-a[i])); } } } return 0;}
Splay again ...
HDU 3436--queue-jumpers (tree-like array or splay trees)