Spoj 2398 Qtree3

Source: Internet
Author: User

Description gives a tree, the color of the tree node is initially white, there are two operations: 0 x: The X-node color is reversed 1 x: Ask 1 to X path on the first black point number solution recently want to practice and line segment tree, so think to do Qtree series, will not LCT Boingonium hindering >_< This problem is relatively bare, directly split and then maintain the most shallow black point marking can be noted that the 1 as the root node, in fact, after the split of each interval of the left is obviously more shallow, if there are black dots directly statistical answer can be code
#include <bits/stdc++.h>using namespace Std;#define LS (rt << 1)#define RS (rt << 1 | 1)ConstintN =100005;intN, Q, tot, CNT, to[n <<1], Nxt[n <<1], Head[n],Q[n], Num[n], sz[n], top[n], dep[n], pre[n], son[n], id[n], w[n <<2], F[n <<2];bool Vis[n];inlineint Read(int&t) {intFF =1; Char c; while(c = GetChar (), C <' 0 '|| C >' 9 ')if(c = ='-') FF =-1; T = C-' 0 '; while(c = GetChar (), C >=' 0 '&& C <=' 9 ') T = T *Ten+ C-' 0 '; T*=FF;} void Add (intUintV) {To[tot] = V, nxt[tot] = Head[u], head[u] = tot++; To[tot] = u, Nxt[tot] = Head[v], head[v] = tot++;} void Up (intRT) {if(W[ls]) w[rt] = W[ls], f[rt] = F[ls];ElseW[RT] = W[rs], f[rt] = F[rs];} void Change (intRtintLintRintP) {if(L = = r) {W[rt] ^=1;if(W[rt]) f[rt] = Num[l];ElseF[RT] =0;return; }intMID = L + R >>1;if(P <= mid) change (LS, L, Mid, p);ElseChange (RS, Mid +1, R, p); Up (RT);}intQueryintRtintLintRintLintR) {if(!w[rt])return 0;if(l <= L && R >= R)returnF[RT];intMID = L + R >>1;if(R <= mid)returnQuery (LS, L, Mid, L, R);Else if(L > Mid)returnQuery (RS, Mid +1, R, L, R);Else{intt = query (LS, L, Mid, L, R);if(t)returnTreturnQuery (RS, Mid +1, R, L, R); }}intAskintAintb) {intAns =-1; while(Top[a]! = Top[b]) {if(Dep[top[a]] < dep[top[b]]) swap (A, b);intt = query (1,1, N, Id[top[a]], id[a]);if(t) ans = t;    A = Pre[top[a]]; }if(Dep[a] < dep[b]) swap (A, b);intt = query (1,1, N, Id[b], id[a]);if(t) ans = t;returnAns;} void Init () {memset (head,-1, sizeof (head));Read(n),Read(Q); for(inti =1,x,y; I < n; ++i) {scanf ("%d%d", &x, &y); Addx,y); }intR =0; vis[dep[1] =Q[0]=1] =1; for(inti =0; I <= R; ++i) for(intj = head[Q[i]]; ~j; j = Nxt[j])if(!vis[to[j]]) {Vis[to[j]] =1; dep[Q[++r]= To[j]] = dep[Q[i]] +1; pre[Q[r]] =Q[i]; } for(inti = r; I >=0; I.) {sz[pre[Q[i]]] + = ++sz[Q[i]];if(sz[son[pre[Q[i]]] < sz[Q[i]]) son[pre[Q[i]]] =Q[i]; } for(inti =0; I <= R; ++i)if(!top[Q[i]]) { for(intj =Q[i]; J j = Son[j]) {Top[j] =Q[i];            NUM[ID[J] = ++cnt] = j; }}}void Gao () {intOpx; for(inti =1; I <= Q; ++i) {Read(OP),Read(x);if(!OP) Change (1,1, N, id[x]);Else    printf("%d\ n", Ask (1,x)); }}intMain () {init (); Gao ();return 0;}

Spoj 2398 Qtree3

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.