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. SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)

    SQL语法精讲(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELE ...

  2. gdbserver 远程调试问题:设置文件和so搜索路径

    编写一个必定crash 的程序 #include <stdio.h> void crash(){ char *a=0; *a=0; } int main() { printf(" ...

  3. 英语发音规则---B字母

    英语发音规则---B字母 一.总结 一句话总结: 1.B发[b]音? bike [baɪk] n. 自行车 bus [bʌs] n. 公共汽车 bag [bæg] n. 袋:猎获物 baby ['be ...

  4. html 中表单元素input type="hidden"的作用

    转自:https://blog.csdn.net/xiaobing_122613/article/details/54946559 (隐藏只是在网页页面上面不显示输入框,但是虽然隐藏了,还是具有for ...

  5. 解决Highcharts指针偏离的问题

    Highcharts 在初始化3D柱状图 时 (注意:3D图形会发生偏移,2D显示正常) 如果加入 shared: true,属性,则可能会导致柱 状图 toolTip提示框偏移,解决方法是去掉或注释 ...

  6. idea设置Template

    在eclipse里面经常会用到syso和main类似这样的内容,但是idea工具里面没有,可以通过 Editor ==> Live templates  ==> 1.首先创建一个自己的Te ...

  7. Python笔记(三)

    # -*- coding:utf-8 -*- # 运算符 a,b=10,20 # 算术运算符:包括+.-.*./.%.**.//运算 print "********************1 ...

  8. 【转】SQL语句删除和添加外键、主键

    --删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter ...

  9. MarkDown 语法备份

    标题 标题1 标题2 标题3 标题4 标题5 无序列表 飞雪连天射白鹿 书神侠倚碧鸳 有序列表 飞雪连天射白鹿 笑书神侠倚碧鸳 超链接 百度 图片 粗体和斜体 粗体 斜体 表格 左对齐标题 右对齐标题 ...

  10. Vue运行npm run dev 时修改端口

    进入项目文件的config文件夹E:\myapp\myproject\config,找到index.js,修改里面的8080端口,改成8088(确定不被别的程序使用的都可以)