博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Water Tree(树链剖分+dfs时间戳)
阅读量:5327 次
发布时间:2019-06-14

本文共 4457 字,大约阅读时间需要 14 分钟。

 Water Tree

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. 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.

Examples
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
output
0 0 0 1 0 1 0 1

 

树链剖分模板题

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #define maxn 500005 9 #define lson l,mid,rt<<1 10 #define rson mid+1,r,rt<<1|1 11 using namespace std; 12 13 int tree[maxn<<3],lazy[maxn<<3]; 14 int n; 15 int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt; 16 vector
ve[maxn]; 17 18 void pushup(int rt){ 19 if(tree[rt<<1]||tree[rt<<1|1]) 20 tree[rt]=1; 21 else 22 tree[rt]=0; 23 } 24 25 void pushdown(int rt){ 26 if(lazy[rt]!=-1){ 27 lazy[rt<<1]=lazy[rt]; 28 lazy[rt<<1|1]=lazy[rt]; 29 tree[rt<<1]=lazy[rt]; 30 tree[rt<<1|1]=lazy[rt]; 31 lazy[rt]=-1; 32 } 33 } 34 35 void build(int l,int r,int rt){ 36 lazy[rt]=-1; 37 if(l==r){ 38 tree[rt]=0; 39 return; 40 } 41 int mid=(l+r)/2; 42 build(lson); 43 build(rson); 44 pushup(rt); 45 } 46 47 void add(int L,int R,int k,int l,int r,int rt){ 48 if(L<=l&&R>=r){ 49 tree[rt]=k; 50 lazy[rt]=k; 51 return; 52 } 53 int mid=(l+r)/2; 54 pushdown(rt); 55 if(L<=mid) add(L,R,k,lson); 56 if(R>mid) add(L,R,k,rson); 57 pushup(rt); 58 } 59 60 int query(int L,int R,int l,int r,int rt){ 61 if(L<=l&&R>=r){ 62 return tree[rt]; 63 } 64 int mid=(l+r)/2; 65 pushdown(rt); 66 int ans=0; 67 if(L<=mid) if(ans||query(L,R,lson)) ans=1; 68 if(R>mid) if(ans||query(L,R,rson)) ans=1; 69 pushup(rt); 70 return ans; 71 } 72 73 void dfs1(int now,int f,int deep){ 74 dep[now]=deep; 75 siz[now]=1; 76 fa[now]=f; 77 int maxson=-1; 78 for(int i=0;i
maxson){ 83 maxson=siz[ve[now][i]]; 84 son[now]=ve[now][i]; 85 } 86 } 87 } 88 89 void dfs2(int now,int topp){ 90 id[now]=++cnt; 91 top[now]=topp; 92 if(!son[now]) return; 93 dfs2(son[now],topp); 94 for(int i=0;i
dep[y]) swap(x,y);107 add(id[x],id[y],k,1,n,1);108 }109 110 void addSon(int x,int k){111 add(id[x],id[x]+siz[x]-1,k,1,n,1);112 }113 114 int main(){115 std::ios::sync_with_stdio(false);116 cin>>n;117 int m;118 int pos,z,x,y;119 for(int i=1;i
>x>>y;121 ve[x].push_back(y);122 ve[y].push_back(x);123 }124 cnt=0;125 dfs1(1,0,1);126 dfs2(1,1);127 build(1,n,1);128 cin>>m;129 for(int i=1;i<=m;i++){130 cin>>pos>>x;131 if(pos==1){132 addSon(x,1);133 }134 else if(pos==2){135 addRange(1,x,0);136 add(id[x],id[x],0,1,n,1);137 }138 else if(pos==3){139 if(query(id[x],id[x],1,n,1)){140 cout<<1<
View Code

 

转载于:https://www.cnblogs.com/Fighting-sh/p/9704356.html

你可能感兴趣的文章
正则表达式
查看>>
arcgis api 4.x for js 结合 Echarts4 实现散点图效果(附源码下载)
查看>>
YTU 2625: B 构造函数和析构函数
查看>>
apache自带压力测试工具ab的使用及解析
查看>>
加固linux
查看>>
WPF中Image显示本地图片
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
js千分位处理
查看>>
字符串类型的相互转换
查看>>
基础学习:C#中float的取值范围和精度
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
SOPC Builder中SystemID
查看>>
关于 linux 的 limit 的设置
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>
vim中文帮助教程
查看>>
MySQL基础3
查看>>