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

题意:

给你一棵树,n个点n-1条边,m个询问

(1)“1 v",表示将以点v为根节点的子树全部赋值为1,

(2)"2 v",表示将点v以及点v的所有祖先节点全部赋值为0,

(3)"3 v",表示查询点v的值。

题解:

我们dfs出dfs序列,再用线段树/树状数组修改一段序列就好了(只有01)

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxn = 5e5+, Pow = << ;
int N, Q, ind[maxn], r[maxn], cur = , a, b;
int emp[*Pow+] , fil[*Pow+];
vector<int> adj[maxn];
void dfs(int v, int par){
ind[v] = cur++;
for(int i = ; i<adj[v].size(); i++) if(adj[v][i]!=par) dfs(adj[v][i], v);
r[v] = cur-;
}
void upd(int l, int r, int t){
l+=Pow; r+=Pow;
while(l<=r){
fil[l] = fil[r] = t;
l = (l+)/;
r = (r-)/;
}
}
int get(int x){
int res = ;
for(int i = Pow+x; i>; i/=) res = max(res, fil[i]);
return res;
}
void add(int x, int t){
for(int i = Pow+x-; i>; i/=) emp[i] = t;
}
int rmq(int p, int l, int r, int a, int b){
if(l>b||r<a) return ;
if(l>=a&&r<=b) return emp[p];
return max(rmq(*p, l, (l+r)/, a, b), rmq(*p+, (l+r)/+, r, a, b));
}
int main(){
ios::sync_with_stdio(false); cin.tie(false); cout.tie(false);
cin >> N;
for(int i = ; i<N-; i++){
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(, -);
cin >> Q;
for(int i = ; i<=Q; i++){
cin >> a >> b;
b--;
if(a==) upd(ind[b], r[b], i);
else if(a==) add(ind[b], i);
else cout << (get(ind[b])>rmq(, , Pow, ind[b], r[b])) << '\n';
}
}

343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构的更多相关文章

  1. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  2. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  3. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  7. Codeforces Round #200 (Div. 1)D. Water Tree

    简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...

  8. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)

    https://codeforces.com/contest/1062/problem/E 题意 给一颗树n,然后q个询问,询问编号l~r的点,假设可以删除一个点,使得他们的最近公共祖先深度最大.每次 ...

随机推荐

  1. Linux程序设计学习笔记——异步信号处理机制

    转载请注明出处: http://blog.csdn.net/suool/article/details/38453333 Linux常见信号与处理 基本概念 Linux的信号是一种进程间异步的通信机制 ...

  2. 百度编辑器图片在线流量返回url改动

    百度编辑器中返回的是我们server中的url,有时并非我们须要的,比方图文编辑中.我想在线浏览上传过的图片 ,那么我返回的应该是腾讯server上面的url.这样才不会被腾讯的过滤器过来掉,全部我们 ...

  3. 随机森林算法demo python spark

    关键参数 最重要的,常常需要调试以提高算法效果的有两个参数:numTrees,maxDepth. numTrees(决策树的个数):增加决策树的个数会降低预测结果的方差,这样在测试时会有更高的accu ...

  4. 两个NSMutableDictionary合并成一个NSMutableDictionary

    解决方案: NSMutableDictionary *targetMutableDictionary = [mutableDictionary1 copy]; [targetMutableDictio ...

  5. Redis学习笔记(十二) 高级命令:服务器管理命令

    原文链接:http://doc.redisfans.com/server/index.html save 执行一个同步操作,将redis实例的所有数据以rdb的形式保存到硬盘,一般来说,生产环境很少执 ...

  6. c# CacheHelper缓存帮助类

    一.开篇 主要功能:改善程序性能.服务器的响应速度,尤其是当数据的处理过程变得复杂以及访问量变大时,变得比较明显.有些数据并非时刻在发生变化,如果我们可以将一些变化不频繁的数据的最终计算结果(包括页面 ...

  7. windows中安装redis的phpredis扩展

    1. 下载php的redis扩展 打开网址 http://pecl.php.net/ (php的扩展库官网),搜索redis,进入地址:http://pecl.php.net/package/redi ...

  8. Eclipse键盘输出文字,显示到屏幕上方法

    方法1 /*标准的思路: * 分析: * 1.来源 * 键盘:System.in * 2.目的地 * 屏幕:System.out * 文件:FIle * 3.分别分析:源,目的地流的类型(字符,字节) ...

  9. 利用PBFunc在Powerbuilder中进行FTP操作

    PBFunc.dll包含了FTP的操作,使用FTP时主要需要以下步骤: 1.调用of_Login函数登录Ftp服务器 2.调用FTP的各种方法 3.Ftp操作完毕后调用of_LoginOut方法进行注 ...

  10. UWP Control Toolkit Collections 求UWP工作

    1. it is like wechat wait-sliderdeleteitem in iOS 看起来比较像微信删掉项 now support listview and gridview in C ...