Topic links
Test instructions
To a tree, a Benquan.
1, ask the path of the Benquan and
2. Ask for the K-mark along the path.
Idea: LCT naked question.
#include <iostream>#include <fstream>#include <string>#include <time.h>#include <vector>#include <map>#include <queue>#include <algorithm>#include <stack>#include <cstring>#include <cmath>#include <set>#include <vector>using namespace STD;Template<classT>inline BOOLRD (T &ret) {CharCintSgnif(c = GetChar (), c = = EOF)return 0; while(c! ='-'&& (c<' 0 '|| C>' 9 ')) C = GetChar (); SGN = (c = ='-') ? -1:1; ret = (c = ='-') ?0: (C-' 0 '); while(c = GetChar (), C >=' 0 '&&c <=' 9 ') ret = RET *Ten+ (C-' 0 '); RET *= SGN;return 1;}Template<classT>inline voidPT (T x) {if(X <0) {Putchar('-'); x =-X; }if(x>9) PT (X/Ten);Putchar(x%Ten+' 0 ');}typedef Long Longlltypedefpair<int,int> PII;Const intN =30005;Const intINF =10000000;structNode *null;structnode{Node *fa, *ch[2];intSizeintVal, MA, sum, id;BOOLRev;inline voidPut () {printf("%d:id,%d,%d,%d (%d,%d) fa:%d \ n", ID, Val, MA, Sum, ch[0]->id, ch[1]->id, Fa->id); }voidDebug (Node *x) {if(x = = null)return; X->put ();if(x->ch[0]! = NULL)Putchar(' L '), Debug (x->ch[0]);if(x->ch[1]! = NULL)Putchar(' R '), Debug (x->ch[1]); }inline voidClearint_val,int_id) {FA = ch[0] = ch[1] = null; Size =1; Rev =0; id = _id; val = ma = sum = _val; }inline voidAdd_val (int_val) {val + = _val; sum + = _val; MA = Max (MA, Val); }inline voidPush_up () {size =1+ ch[0]->size + ch[1]->size; sum = ma = val;if(ch[0] = null) {sum + = ch[0]->sum; MA = Max (MA, ch[0]->MA); }if(ch[1] = null) {sum + = ch[1]->sum; MA = Max (MA, ch[1]->MA); } }inline voidPush_down () {if(rev) {Flip (); ch[0]->rev ^=1; ch[1]->rev ^=1; } }inline voidSETC (Node *p,intd) {ch[d] = p; P->FA = This; }inline BOOLD () {returnfa->ch[1] == This; }inline BOOLIsRoot () {returnFA = = NULL | | fa->ch[0] != This&& fa->ch[1] != This; }inline voidFlip () {if( This= = NULL)return; Swap (ch[0], ch[1]); Rev ^=1; }inline voidGo () {//Start updating to this from the chain header if(!isroot ()) Fa->go (); Push_down (); }inline voidRot () {Node *f = fa, *ff = fa->fa;intc = d (), CC = Fa->d (); F->setc (Ch[!c], c); This->setc (f,!c);if(FF->CH[CC] = = f) ff->setc ( This, CC);Else This->FA = FF; F->push_up (); }inlineNode*splay () {go (); while(!isroot ()) {if(!fa->isroot ()) d () = = Fa->d ()? Fa->rot (): Rot (); Rot (); } push_up ();return This; }inlinenode* access () {//access After this is a splay to the root, and this is already the root of this splay for(Node *p = This, *q = null; P! = NULL; Q = p, p = p->fa) {p->splay ()->setc (q,1); P->push_up (); }returnSplay (); }inlinenode* Find_root () {Node *x; for(x = Access (); X->push_down (), x->ch[0]! = NULL; x = x->ch[0]);returnX }voidMake_root () {access ()->flip (); }voidCut () {//Leave the sub-tree of this point outAccess (); ch[0]->FA = null; ch[0] = null; Push_up (); }voidCut (Node *x) {if( This= = X | | Find_root ()! = X->find_root ())return;Else{X->make_root (); Cut (); } }voidLink (Node *x) {if(Find_root () = = X->find_root ())return;Else{make_root (); FA = x; } }}; Node Pool[n], *tail; Node *node[n], *ee[n];intN, Q;voidDebug (Node *x) {if(x = = null)return; X->put (); Debug (x->ch[0]); Debug (x->ch[1]);}inline intAsk (node *x, node *y) {x->access ();//for (int i = 1; I <= n; i++) debug (Node[i]), Putchar (' \ n '); for(x = null; y = null; x = y, y = y->fa) {Y->splay ();//for (int i = 1; I <= n; i++) debug (Node[i]), Putchar (' \ n '); if(Y->FA = = null)returny->ch[1]->sum + x->sum; Y->SETC (x,1); Y->push_up (); }}inlinenode* get_kth (Node *x,intK) { while(x->ch[0]->size +1! = k) {if(x->ch[0]->size >= k) x = x->ch[0];ElseK-= x->ch[0]->size +1, x = x->ch[1]; }returnx;}inline intQUERY_KTH (node *x, node *y,intk) {x->access (); for(x = null; y = null; x = y, y = y->fa) {Y->splay ();if(Y->FA = = null) {if(k = = y->ch[1]->size +1)returny->id;if(K < y->ch[1]->size +1)returnGet_kth (y->ch[1], y->ch[1]->size-k +1)->id;returnGet_kth (x, k-y->ch[1]->size-1)->id; } y->setc (X,1); Y->push_up (); }}structedge{intFrom, to, DIS, ID, NEX;} Edge[n <<1];intHead[n], Edgenum;voidAddintUintVintDisintID) {Edge E = {u, V, dis, id, head[u]}; Edge[edgenum] = E; Head[u] = edgenum++;}BOOLVis[n];voidBFS () {Fill (Vis +1, Vis +1+ N,false); Queue<int>Q Q.push (1); vis[1] =true; while(!q.empty ()) {intU = Q.front (); Q.pop (); for(inti = Head[u]; ~i; i = Edge[i].nex) {intv = edge[i].to;if(Vis[v])Continue; VIS[V] =true; Q.push (v); Ee[edge[i].id] = Node[v]; Node[v]->val = Edge[i].dis; Node[v]->push_up (); NODE[V]->FA = Node[u]; } }}intMain () {intT RD (T); while(t--) {rd (n); Fill (head +1, head + n +1, -1); Edgenum =0; tail = pool; NULL = tail++; Null->clear (-inf,0); Null->size =0; Null->sum =0; for(inti =1; I <= N; i++) {Node[i] = tail++; Node[i]->clear (0, i); } for(inti =1, u, V, D; I < n; i++) {rd (U); Rd (v); Rd (d); Add (U, V, D, I); Add (V, U, D, i); } BFS ();Charstr[Ten];intU, V, K; while(true){scanf('%s ', str);if(str[1] ==' O ') Break; RD (U); RD (v);if(str[0] ==' D ') pt (Ask (Node[u], node[v])),Putchar(' \ n ');Else{rd (k); PT (query_kth (Node[u], node[v], k));Putchar(' \ n '); } }puts(""); }return 0;}/*1 2 4 5 3 6 2DIST 4 6KTH 4 6 4KTH 6 5 4DIST 2 5*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spoj QTREE2 LCT Nude questions