For the great God, the problem is water. I've been doing this spent maths for 2 days.
What is the advantage of stretching a tree? No matter how you rotate the sequence is not changed, and you want to make the interval reversal, as long as the point K to the root node, then its left subtree is to exchange the interval [l,r), and then exchange around
Sub-tree can be (in order), the root node location is I+siz[ch[root][0]],i is processed by the number of nodes, Siz[ch[root][0]] is Zuozi (need to rotate the number). Rotation can be marked with lazy thoughts, so the time is Logn. Because the value of K is already processed, the root node is deleted directly.
Code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineKey_value Ch[ch[root][1]][0]using namespacestd;Const intMAXN =100010;structnode{intID; intv;} A[MAXN];intpre[maxn],ch[maxn][2],siz[maxn],lazy[maxn],tot1,root;intS[maxn],tot2,n;BOOLCMP (node Fa,node FB) {if(FA.V! =fb.v)returnFA.V <FB.V; returnFa.id <fb.id;}voidTreavel (intx) { if(x) {treavel (ch[x][0]); printf ("knot%2d: Left son%2d right son%2d father node%2d size=%2d,lazy=%2d\n", x,ch[x][0],ch[x][1],pre[x],siz[x],lazy[x]); Treavel (ch[x][1]); }}voidDebug () {printf ("root:%d\n", Root); Treavel (root);}voidNewnode (int&rt,intPaintk) {RT=K; PRE[RT]=PA; SIZ[RT]=1; LAZY[RT]=0; ch[rt][0] = ch[rt][1] =0;}voidPushup (intRT) {Siz[rt]= siz[ch[rt][0]] + siz[ch[rt][1]] +1;}voidPushdown (intRT) { if(Lazy[rt]) {lazy[ch[rt][0]] ^=1; lazy[ch[rt][1]] ^=1; Swap (ch[rt][0],ch[rt][1]); LAZY[RT]=0; }}voidBuildint&rt,intLintRintPA) { if(L >R) { return ; } intm = (l+r)/2; Newnode (RT,PA,M); Build (ch[rt][0],l,m-1, RT); Build (ch[rt][1],m+1, R,RT); Pushup (RT);}voidInit () {root= TOT1 = Tot2 =0; ch[root][0] = ch[root][1] = Siz[root] = Lazy[root] = Pre[root] =0; Build (Root,1N0); Pushup (root);}voidRotate (intRtintkind) {pushdown (pre[rt]); Pushdown (RT); inty =Pre[rt]; ch[y][!kind] =Ch[rt][kind]; Pre[ch[rt][kind]]=y; if(Pre[y]) {ch[pre[y]][ch[pre[y]][1]==y] =RT; } Pre[rt]=Pre[y]; Ch[rt][kind]=y; Pre[y]=RT; Pushup (y); Pushup (RT);}voidSplay (intRtintgoal) {pushdown (RT); while(Pre[rt]! =goal) { if(Pre[pre[rt]] = =goal) {pushdown (pre[rt]); Pushdown (RT); Rotate (rt,ch[pre[rt]][0]==RT); } Else{pushdown (pre[pre[rt]); Pushdown (Pre[rt]); Pushdown (RT); inty =Pre[rt]; intKind = ch[pre[y]][0]==y; if(Ch[y][kind] = =RT) {Rotate (RT,!kind); Rotate (Rt,kind); } Else{Rotate (y,kind); Rotate (Rt,kind); }}} pushup (RT); if(Goal = =0) Root=RT; }intGet_max (intRT) {pushdown (RT); while(ch[rt][1]) {RT= ch[rt][1]; Pushdown (RT); } returnRT;}voidDelintroot) { if(ch[root][0] ==0) {root= ch[root][1]; Pre[root]=0; } Else { intt = Get_max (ch[root][0]); Splay (T,root); ch[t][1] = ch[root][1]; pre[ch[root][1]] =T; Root=T; Pre[root]=0; Pushup (root); }}intMain () {inti; while(~SCANF ("%d",&N), N) { for(i=1; i<=n; i++) {scanf ("%d",&a[i].v); A[i].id=i; } sort (A+1, a+n+1, CMP); Init (); for(i=1; i<=n; i++){ //cout<< "test1:" <<endl;splay (a[i].id,0); //debug ();lazy[ch[root][0]] ^=1; printf ("%d", i+siz[ch[root][0]]); if(I < n) printf (" "); Elseprintf"\ n"); del (root); } }}
hdu1890 stretching tree (interval inversion)