Orz Huang Long, of course, also want to thank themselves (this play more smoothly, although the problem operation is very few, also relatively simple), refueling, believe that their own, will be more powerful, but still have to be open-minded, will have greater progress. But still a little do not understand, find a time, ask Czl!
1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineRep (i,j,k) for (int i = j; I <= K; i++)5 #defineLC C[k][0]6 #defineRC C[k][1]7 #defineMAXN 102338 using namespacestd;9 Ten intN, M; One intRead () A { - ints =0, t =1;Charc =GetChar (); - while( !IsDigit (c)) { the if(c = ='-') T =-1; c =GetChar (); - } - while(IsDigit (c)) { -s = S *Ten+ C-'0'; c =GetChar (); + } - returnS *T; + } A at intc[maxn][2], PA[MAXN]; - BOOLREV[MAXN]; - intQ[MAXN]; - -InlineBOOLRootintx) - { in intK =Pa[x]; - returnLC! = x && rc! =x; to } + - voidPushdown (intk) the { * if(Rev[k]) { $REV[LC] ^=1, REV[RC] ^=1;Panax Notoginseng swap (LC,RC); -REV[K] ^=1; the } + } A the voidRorate (intk) + { - intFA = Pa[k], GFA =PA[FA]; $ intL = c[fa][1] = = k, R = l ^1; $ if( !root (FA)) { -c[gfa][c[gfa][1] = = FA] =K; - } thePa[k] = GFA, pa[c[k][r]] = FA, pa[fa] =K; -C[FA][L] = C[k][r]; C[K][R] =FA;Wuyi } the - voidSplay (intk) Wu { - inttop =0; AboutQ[++top] =K; $ for(intx = k;!root (x); x =Pa[x]) { -Q[++top] =Pa[x]; - } - while(top) Pushdown (q[top--]); A while( !root (k)) { + intFA = Pa[k], GFA =PA[FA]; the if( !root (FA)) { - if(c[fa][0] = = k ^ c[gfa][0] ==fa) rorate (k); $ Elserorate (FA); the } the Rorate (k); the } the } - in voidAccessintx) the { the for(intt =0; X t = x, x =Pa[x]) { AboutSplay (x), c[x][1] =T; the } the } the + voidMakeroot (intx) - { theAccess (x), splay (x); REV[X] ^=1;Bayi } the the voidLinkintXinty) - { -Makeroot (x); PA[X] =y; splay (x); the } the the voidCutintXinty) the { -Makeroot (x); Access (y), splay (y); c[y][0] = pa[x] =0; the } the the intFindintk)94 { the access (k); splay (k); the while(LC) K =LC; the returnK;98 } About - BOOLSolveintXinty)101 {102 if(Find (x) = = Find (y))return 1;103 Else return 0;104 } the 106 intMain ()107 {108n = Read (), M = Read ();Charc[Ten];109Rep (I,1, M) { thescanf"%s", c);111 intx = Read (), y =read (); the if(c[0] =='C' ){113 link (x, y); the } the if(c[0] =='Q' ){ the if(Solve (x, y)) puts ("Yes");117 ElsePuts"No");118 }119 if(c[0] =='D' ){ - Cut (x, y);121 }122 }123 return 0;124}
Bzoj 2049 LCT