xtu summer individual 6 F - Water Tree

Water Tree

Time Limit: 4000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 343D
64-bit integer IO format: %I64d      Java class name: (Any)

Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

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:

  1. Fill vertex v with water. Then v and all its children are filled with water.
  2. Empty vertex v. Then v and all its ancestors are emptied.
  3. Determine whether vertex v is filled with water at the moment.

Initially all vertices of the tree are empty.

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 aibi(1 ≤ ai, bi ≤ nai ≠ 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

Input

51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5

Output

00010101

Source

Codeforces Round #200 (Div. 1)

解题:线段数。。。。。哎 不解释了!如果孩子节点空,那么祖先节点空!

  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\n",query(p[v].lt,p[v].rt,1));
118             }
119         }
120     }
121     return 0;
122 }

xtu summer individual 6 F - Water Tree

时间: 2024-11-10 11:06:07

xtu summer individual 6 F - Water Tree的相关文章

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres

xtu summer individual 6 E - Find Metal Mineral

Find Metal Mineral Time Limit: 1000ms Memory Limit: 65768KB This problem will be judged on HDU. Original ID: 400364-bit integer IO format: %I64d      Java class name: Main Humans have discovered a kind of new metal mineral on Mars which are distribut

Water Tree(树链剖分+dfs时间戳)

Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Ea

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

D. Water Tree Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water. The vertices of the tree are numbered from 1 to n with the root at vertex 1. For

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o