Spoj QTREE3 LCT Nude questions

Source: Internet
Author: User

Topic links

Test instructions

Q queries for a given n points

Below the n-1 line gives the tree edge, dots black or white, initialized to white

The following Q line:

There are 2 types of inquiries:

1, 0 x turn the X-dot black to white, white to black

2, 1 x asks the dot of the first black dot on path (1,x), output-1 if no black dot exists

Ideas:

LCT Nude Questions

#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 <class t>  inline BOOL Rd (T &ret) {char c; int sgn;if (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 * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} typedef long Long Ll;typedef pair<int, int> pii;const int N = 30005;const int inf = 10000000;struct Node *null;struc T Node{node *fa, *ch[2];int size;int val, MA, sum, id;bool rev;inline void put () {printf ("%d:id,%d,%d,%d (%d,%d) fa:%d \ n ", id, Val, MA, Sum, Ch[0]->id, Ch[1]->id, fa->id);} inline void Clear (int _val, int _id) {FA = ch[0] = ch[1] = Null;size = 1;rev = 0;id = _id;val = ma = sum = _val;} inline void Push_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 void Push_down () {if (rev.) {ch[0]->flip (); Ch[1]->flip (); rev = 0;}} inline void SetC (Node *p, int d) {Ch[d] = P;p->fa = this;} inline bool D () {return fa->ch[1] = = this;} inline bool IsRoot () {return FA = = NULL | | fa->ch[0]! = this && fa->ch[1]! = this; inline void Flip () {if (this = null) Return;swap (ch[0], ch[1]); rev ^= 1;} The inline void Go () {//is updated from the chain header to Thisif (!isroot ()) Fa->go ();p ush_down ();}  inline void rot () {Node *f = fa, *ff = fa->fa;int C = d (), CC = Fa->d (), F->setc (Ch[!c], C), This->setc (f,!c); (FF-&GT;CH[CC] = = f) ff->setc (this, cc); ElsE This->fa = Ff;f->push_up ();} Inline Node*splay () {go (); while (!isroot ()) {if (!fa->isroot ()) d () = = Fa->d ()? Fa->rot (): Rot (); Rot ();} Push_up (); return this;} Inline node* access () {//access After this is a splay to the root, and this is already the root of the splay for (Node *p = this, *q = NULL; P! = null; q = p, p = P -&GT;FA) {P->splay ()->setc (q, 1);p->push_up (); return splay ();} Inline node* find_root () {Node *x;for (x = Access (); X->push_down (), x->ch[0]! = null; x = x->ch[0]); return x;} void Make_root () {access ()->flip ();} void Cut () {//Leave the sub-tree of this point out of access (); Ch[0]->fa = null;ch[0] = Null;push_up ();} void Cut (Node *x) {if (this = = X | | find_root ()! = X->find_root ()) Return;else {x->make_root (); Cut ();}} void link (Node *x) {if (find_root () = = X->find_root ()) Return;else {make_root (); fa = x;}}}; Node Pool[n], *tail; Node *node[n];int N, q;void Debug (node *x) {if (x = = null) return;x->put ();d ebug (x->ch[0]);d ebug (x->ch[1]);} inline void Change (node* x) {x->access (); X->val ^= 1;x->pusH_up ();} inline int Ask (node* x) {node[1]->make_root (); x->access (); Node[1]->splay ();//for (int i = 1; I <= n; i++) debug (Node[i]), Putchar (' \ n '); Node *r = node[1];if (R->sum = = 0) Return-1;while (true) {if (r->ch[0]->sum = 0 && r->val) return R-&gt ; id;r = r->ch[r->ch[0]->sum==0];}} struct edge{int from, to, NEX;} Edge[n << 1];int Head[n], edgenum;void Add (int u, int v) {Edge E = {u, V, head[u]};edge[edgenum] = e;head[u] = Edge num++;} void Dfs (int u, int fa) {for (int i = head[u]; ~i; i = edge[i].nex) {int v = edge[i].to;if (v = = FA) Continue;dfs (V, u); node[ V]->push_up (); node[v]->fa = Node[u];}} int main () {while (cin>>n>>q) {memset (head,-1, sizeof head); edgenum = 0;tail = Pool;null = Tail++;null->cle AR (0, 0); null->size = 0; Null->sum = 0;for (int i = 1; I <= n; i++) {Node[i] = tail++;node[i]->clear (0, i);} for (int i = 1, u, v; i < n; i++) {rd (U); Rd (v); Add (U, v); Add (v, u);} DFS (1, 1); int u, V;while (q--) {rd (U); Rd (V), if (U = = 0) change (node[v]), Else PT (ask (Node[v)), Putchar (' \ n ');}} return 0;} /*9 81 21 32 42 95 97 98 96 81 30 81 61 70 21 90 21 9*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spoj QTREE3 LCT Nude questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.