Codeforces 343D Water Tree(DFS序+线段树+技巧)

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 343D Water Tree(DFS序+线段树+技巧)相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/problemset/problem/343/D

题目:

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
Copy
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
题意:给定一个树,树上有n个点,每个点是一个蓄水池,初始全为空。首先输入一个n,然后输入n - 1行,每行两个点,代表两点之间有边,然后输入一个m,接下来m行操作,操作有3种:
1 v,把v及v的所有子孙注水。
2 v,把v及v的所有祖先放水。
3 v,询问v点有没有水,有输出1,否则0
题解:操作1和操作3很好解决,操作2的话在我们跑DFS把每个点的祖先也标记一下。因为放水的原因,我们操作1的时候,跑那个顶点统领的区间时候,如果初始状态为放水,那么它的祖先也是放水的,我们需要把
它的祖先放水(单点更新一下)。所以操作2只要单点更新。操作3判断一下改点统领的区间中有没有放水的,有的话,那么改点也是放水的。*注意操作3query的是区间(刚刚一直在这里wa...),pushup和pushdown
理解一下就好了。
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 typedef long long LL;
  5 const int N=500010;
  6 vector <int> E[N];
  7 int st[N],en[N],f;
  8 int a[N],vis[N],fa[N],index;
  9 
 10 void dfs(int u){
 11     st[u]=++index;vis[u]=1;
 12     for(int i=0;i<E[u].size();i++){
 13         int v=E[u][i];
 14         if(!vis[v]){
 15             fa[v]=u;
 16             dfs(v);
 17         }
 18     }
 19     en[u]=index;
 20 }
 21 
 22 struct Tree{
 23     int l,r,p,mark;
 24 };
 25 Tree tree[4*N];
 26 
 27 void pushup(int x){
 28     int tmp=x<<1;
 29     if(tree[tmp].p&&tree[tmp|1].p) tree[x].p=1;
 30     else tree[x].p=0;
 31 }
 32 
 33 void pushdown(int x){
 34     int tmp=x<<1;
 35     tree[tmp].p=tree[tmp|1].p=tree[x].mark;
 36     tree[tmp].mark=tree[tmp|1].mark=tree[x].mark;
 37     tree[x].mark=0;
 38 }
 39 
 40 void build(int l,int r,int x)
 41 {
 42     tree[x].l=l;
 43     tree[x].r=r;
 44     tree[x].mark=tree[x].p=0;
 45     if(l==r) return ;
 46     int tmp=x<<1;
 47     int mid=(l+r)>>1;
 48     build(l,mid,tmp);
 49     build(mid+1,r,tmp|1);
 50     //pushup(x);
 51 }
 52 
 53 void update(int l,int r,int c,int x)
 54 {
 55     if(r<tree[x].l||l>tree[x].r) return ;
 56     if(l<=tree[x].l&&r>=tree[x].r)
 57     {
 58         if(tree[x].p==0) f=1;
 59         tree[x].p=tree[x].mark=c;
 60         return ;
 61     }
 62     if(tree[x].mark) pushdown(x);
 63     int tmp=x<<1;
 64     int mid=(tree[x].l+tree[x].r)>>1;
 65     if(r<=mid) update(l,r,c,tmp);
 66     else if(l>mid) update(l,r,c,tmp|1);
 67     else
 68     {
 69         update(l,mid,c,tmp);
 70         update(mid+1,r,c,tmp|1);
 71     }
 72     pushup(x);
 73 }
 74 
 75 void query(int l,int r,int x)
 76 {
 77     if(r<tree[x].l||l>tree[x].r) return ;
 78     if(l<=tree[x].l&&r>=tree[x].r)
 79     {
 80         if(tree[x].p==0) f=0;
 81         return ;
 82     }
 83     if(tree[x].mark) pushdown(x);
 84     int tmp=x<<1;
 85     int mid=(tree[x].l+tree[x].r)>>1;
 86     if(r<=mid) query(l,r,tmp);
 87     else if(l>mid) query(l,r,tmp|1);
 88     else
 89     {
 90         query(l,mid,tmp);
 91         query(mid+1,r,tmp|1);
 92     }
 93 }
 94 
 95 int main(){
 96     int n,m,u,v,op;
 97     scanf("%d",&n);
 98     for(int i=1;i<n;i++){
 99         scanf("%d%d",&u,&v);
100         E[u].push_back(v);
101         E[v].push_back(u);
102     }
103     fa[1]=0;
104     dfs(1);
105     build(1,n,1);
106     scanf("%d",&m);
107     while(m--){
108         scanf("%d%d",&op,&u);
109         if(op==1){
110             f=0;
111             update(st[u],en[u],1,1);
112             if(f) update(st[fa[u]],st[fa[u]],0,1);
113         }
114         else if(op==2){
115             update(st[u],st[u],0,1);
116         }
117         else{
118             f=1;
119             query(st[u],en[u],1);
120             printf("%d\n",f);
121         }
122     }
123     return 0;
124 }

 



以上是关于Codeforces 343D Water Tree(DFS序+线段树+技巧)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 343D Water Tree

codeforces 343D Water Tree 树链剖分 dfs序 线段树 set

Codeforces 343D Water Tree & 树链剖分教程

CF343D Water Tree

CF343D Water Tree 树链剖分

CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)