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. C++实现位数组

    当我们遇到大量整数排序时候为了节省内存空间我们能够考虑使用bit数组来实现,缺点是其仅仅适用于正整数. 思想: 在32位系统一个int类型占4个字节,按位来计算一个int类型能够记录32个数.因此,採 ...

  2. 2015.04.21,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 32

    TEASER PREVIEW 如何描述这些人: 很友好.容易相处的人: 不知疲倦的人: 简单.直率..aboveboard([ə'bʌv'bɔ:d] adv. 光明正大地, 率直地 adj. 光明正大 ...

  3. crawler4j图片爬虫

    该实例主要演示下如何爬取指定网站的图片: 代码中有详细注释: 首先写一个ImageCrawler类: package com.demo.imageCrawler4j; import java.io.F ...

  4. C++ MAP使用

    1. map的构造函数map<int,string> maphai;map<char,int> maphai;map<string,char> mapstring; ...

  5. 欢迎来到Flask的世界

    不多说,直接上文档链接:Flask的文档 教程 API 快速上手

  6. WebApi不支持跨域访问

  7. Paper-[acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning

    [acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning ABST ...

  8. 为什么叫Unity3d为脚本语言

    初接触Unity,看到大家说的都是工作主要是写脚本语言. 一直纳闷为什么说脚本语言呢,c#可不是脚本语言啊. -- -- 后来释然,说它是脚本语言是因为传统程序都是由代码构成的(像iOS.Androi ...

  9. OCR文字识别软件FineReader系列产品双十一特惠!

    17年的双十一,似乎比以往来的更早一些,说是双十一,这不,从十月里就开始了各种宣传,各种造势,说到底还是影响力太大,关注人群太多,优惠力度太劲爆,简直让人不注意都不行啊!对于ABBYY来说,给用户来点 ...

  10. Springboot统一异常处理(@ControllerAdvice)

    import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...