Water Tree
64-bit integer IO format: %I64d Java class name: (Any)
The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.
Mike wants to do the following operations with the tree:
- Fill vertex v with water. Then v and all its children are filled with water.
- Empty vertex v. Then v and all its ancestors are emptied.
- Determine whether vertex v is filled with water at the moment.
Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi(1 ≤ ai, bi ≤ n, ai ≠ bi) — the edges of the tree.
The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.
It is guaranteed that the given graph is a tree.
Output
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
Sample Input
5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5
0
0
0
1
0
1
0
1
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 500010; 17 struct node{ 18 int lt,rt; 19 }; 20 struct Tnode{ 21 int lt,rt,water,lazy; 22 }; 23 Tnode tree[maxn<<2]; 24 int pre[maxn],cnt,n,m; 25 bool vis[maxn]; 26 vector<int>g[maxn]; 27 node p[maxn]; 28 void dfs(int u,int f){ 29 pre[u] = f; 30 vis[u] = true; 31 p[u].lt = ++cnt; 32 for(int v = 0; v < g[u].size(); v++) 33 if(!vis[g[u][v]]) dfs(g[u][v],u); 34 p[u].rt = cnt; 35 } 36 void build(int lt,int rt,int v){ 37 tree[v].lt = lt; 38 tree[v].rt = rt; 39 tree[v].water = 0; 40 tree[v].lazy = -1; 41 if(lt == rt) return; 42 int mid = (lt+rt)>>1; 43 build(lt,mid,v<<1); 44 build(mid+1,rt,v<<1|1); 45 } 46 void push_up(int v){ 47 tree[v].water = min(tree[v<<1].water,tree[v<<1|1].water); 48 } 49 void push_down(int v){ 50 if(tree[v].lazy != -1){ 51 tree[v<<1].water = tree[v<<1|1].water = tree[v].water; 52 tree[v<<1].lazy = tree[v<<1|1].lazy = tree[v].lazy; 53 tree[v].lazy = -1; 54 } 55 } 56 void addWater(int lt,int rt,int v){ 57 if(tree[v].lt == lt && tree[v].rt == rt){ 58 tree[v].lazy = tree[v].water = 1; 59 return; 60 } 61 push_down(v); 62 int mid = (tree[v].lt+tree[v].rt)>>1; 63 if(rt <= mid) addWater(lt,rt,v<<1); 64 else if(lt > mid) addWater(lt,rt,v<<1|1); 65 else { 66 addWater(lt,mid,v<<1); 67 addWater(mid+1,rt,v<<1|1); 68 } 69 push_up(v); 70 } 71 void emptyWater(int x,int v){ 72 if(tree[v].lt == tree[v].rt){ 73 tree[v].water = 0; 74 return; 75 } 76 push_down(v); 77 int mid = (tree[v].lt+tree[v].rt)>>1; 78 if(x <= mid) emptyWater(x,v<<1); 79 else emptyWater(x,v<<1|1); 80 push_up(v); 81 } 82 int query(int lt,int rt,int v){ 83 if(tree[v].lt == lt && tree[v].rt == rt){ 84 return tree[v].water; 85 } 86 push_down(v); 87 int mid = (tree[v].lt+tree[v].rt)>>1; 88 if(rt <= mid) return query(lt,rt,v<<1); 89 else if(lt > mid) return query(lt,rt,v<<1|1); 90 else return min(query(lt,mid,v<<1),query(mid+1,rt,v<<1|1)); 91 } 92 int main(){ 93 int i,j,u,v,tmp; 94 while(~scanf("%d",&n)){ 95 for(i = 0; i <= n; i++){ 96 g[i].clear(); 97 vis[i] = false; 98 } 99 for(i = 1; i < n; i++){ 100 scanf("%d %d",&u,&v); 101 g[u].push_back(v); 102 g[v].push_back(u); 103 } 104 cnt = 0; 105 dfs(1,0); 106 build(1,cnt,1); 107 scanf("%d",&m); 108 for(i = 0; i < m; i++){ 109 scanf("%d %d",&u,&v); 110 if(u == 1){ 111 tmp = query(p[v].lt,p[v].rt,1); 112 addWater(p[v].lt,p[v].rt,1); 113 if(pre[v] && !tmp) emptyWater(p[pre[v]].lt,1); 114 }else if(u == 2){ 115 emptyWater(p[v].lt,1); 116 }else if(u == 3){ 117 printf("%d ",query(p[v].lt,p[v].rt,1)); 118 } 119 } 120 } 121 return 0; 122 }