To give a string, insert and inquire, insert one character at a time, or ask what the character of P position is.
Idea: We ask offline, reverse all the word nonalphanumeric into the line segment tree, and then query on it, each time to remember to insert the last position of the segment tree, and then to save this position in O (1) query can be.
//see if it explodes. int! array will not be one dimension less! //You must be careful about the conditions of the winning.#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;#defineLL Long Long#defineAll (a) A.begin (), A.end ()#definePB Push_back#defineMk Make_pair#defineFi first#defineSe SecondConst intMAXQ = -+5;Const intMaxch =1002000+5;intTree[maxch <<2];inlinevoidPushup (into) {Tree[o]= Tree[o <<1] + tree[o <<1|1];}voidBuildtree (intOintLintR) { if(L = =R) {Tree[o]=1; return ; } intMid = (L + r)/2; if(L <= mid) Buildtree (o <<1, L, mid); if(R > Mid) buildtree (o <<1|1, Mid +1, R); Pushup (o);}voidUpdateintOintLintRintPosintval) { if(L = = r && L = =POS) {Tree[o]=Val; return ; } intMid = (L + r)/2; if(POS <= mid) update (o <<1, L, Mid, Pos, Val); if(Pos > Mid) update (o <<1|1, Mid +1, R, POS, Val); Pushup (o);}intQueryintOintLintRintPOS) { if(L = = r)returnl; intMid = (L + r)/2; if(Tree[o <<1] >= POS)returnQuery (o <<1, L, Mid, POS); if(Tree[o <<1] < POS)returnQuery (o <<1|1, Mid +1, R, Pos-tree[o <<1]);}///things to know: inquiry, initial ch, Val on line tree node, segment tree How many child nodes are in the nodestructquery{intTy, POS, CNT;///indicates that there are currently several inserted CharVal; Query (intTy =0,Charval =0,intpos =0,intCNT =0): Ty (Ty), Val (Val), POS (POS), CNT (CNT) {}}q[maxch];intTpos[maxch], Q;CharTval[maxch], ch[maxch];intMain () {scanf ("%s", CH); intLen =strlen (CH); scanf ("%d", &Q); intCNT =Len; for(inti =1; I <= Q; i++){ Charc[2]; scanf"%s", c); if(c[0] =='I') { Charcc[2]; scanf"%s", CC); intPos scanf"%d", &POS); Q[i]= Query (c[0], cc[0], POS, CNT +1);///currently the type of inquiry, Val and position, plus how many letters have already beencnt++; } Else { intPos scanf"%d", &POS); Q[i]= Query (c[0],0, POS, CNT); } } /*① in reverse order into the ② to get the reverse into the position after the Val is how much*/ intn =CNT; ///printf ("n =%d\n", n);Buildtree (1,1, N); for(inti = Q; I >=1; i--) {///you know where he is. if(Q[i].ty = ='I') {Q[i].pos=min (Q[i].pos, q[i].cnt); intp = Query (1,1, n, Q[i].pos);///position in the line treeTpos[i] =p; TVAL[P]= Q[i].val;///The segment tree is located under the ValUpdate1,1, N, p,0); } } for(inti =1, j =0; I <= N; i++){ if(Tval[i] = =0) {Tval[i]= Ch[j]; J + +; } } for(inti =1; I <= Q; i++){ if(Q[i].ty = ='I') {Update (1,1, N, Tpos[i],1); } Else { intp = Query (1,1, N, Q[i].pos); printf ("%c\n", Tval[p]); } } return 0;}View Code
Good problem segment tree to save data + offline reverse insert POJ 2887