This is still the problem of Tao LCT, followed Czl did a lot of LCT problem Ah! All right, Czl take me to fly! Come on, good, smooth. Come on! 1592 milliseconds, the first time to consider the amount of code, I wrote this spent 2800 of the code length, but czl only 1900, code simplification is very important ah!
1#include <cstdio>2#include <iostream>3 #defineLC C[k][0]4 #defineRC C[k][1]5 #defineRep (i,j,k) for (int i = j; I <= K; i++)6 #defineMAXN 302337 using namespacestd;8 9 intc[maxn][2], PA[MAXN], W[MAXN], SUM[MAXN]; Ten BOOLREV[MAXN]; 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 BOOLRootintx) - { - intK =Pa[x]; - returnLC! = x && rc! =x; - } - in voidMaintain (intk) - { toSUM[K] = SUM[LC] + SUM[RC] +W[k]; + } - the voidRorate (intk) * { $ intFA = Pa[k], GFA =PA[FA];Panax Notoginseng intL = c[fa][1] = = k, R = l ^1; - if( !root (FA)) { thec[gfa][c[gfa][1] = = FA] =K; + } APA[FA] = k, pa[k] = GFA, pa[c[k][r]] =FA; theC[FA][L] = C[k][r], c[k][r] =FA; maintain (FA), maintain (k); + } - $ voidPushdown (intk) $ { - if(Rev[k]) { -REV[K] ^=1, REV[LC] ^=1, REV[RC] ^=1; the swap (LC,RC); - }Wuyi } the - intQ[MAXN]; Wu voidSplay (intk) - { About inttop =0; Q[++top] =K; $ for(intx = k;!root (x); x =Pa[x]) -Q[++top] =Pa[x]; - while(top) Pushdown (q[top--]); - while( !root (k)) { A intFA = Pa[k], GFA =PA[FA]; + if( !root (FA)) { the if(c[gfa][1] = = fa ^ c[fa][1] ==k) rorate (k); - Elserorate (FA); $ } the Rorate (k); the } the } the - voidAccessintk) in { the for(intt =0; K T = k, k =Pa[k]) theSplay (k), rc =T, maintain (k); About } the the voidMakeroot (intk) the { +Access (k), splay (k), Rev[k] ^=1; - } the Bayi voidLinkintXinty) the { theMakeroot (x), pa[x] =y; - } - the voidSplitintXinty) the { theMakeroot (x), Access (y), splay (y); c[y][0] = pa[x] =0; the maintain (y); - } the the intFindintk) the {94 access (k), splay (k); the while(LC) K =LC; the returnK; the }98 About intQueryintXinty) - {101Makeroot (x), Access (y), splay (y);returnSum[y];102 }103 104 intMain () the {106 intn =read ();107Rep (I,1, n) w[i] = read ();Charc[ -];108 intm =read ();109Rep (I,1, M) { thescanf"%s", c);111 if(c[0] =='b' ) { the intx = Read (), y =read ();113 intKX = FIND (x), KY =find (y); the if(KX! =ky) { thePuts"Yes"); the link (x, y);117 }118 ElsePuts"No");119 } - if(c[0] =='e' ){121 intx = Read (), y =read ();122 intKX = FIND (x), KY =find (y);123 if(KX = KY) puts ("Impossible");124 Elseprintf"%d\n", query (x, y)); the }126 if(c[0] =='P' ){127 intx = Read (), y =read (); -Splay (x); W[X] =y; maintain (x);129 } the }131 return 0; the}
Bzoj 2843 LCT