D. 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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ 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
思路:dfs序+线段树;
先dfs将点映射成线性的,然后维护两棵线段树,一个是往里面灌水的时间,一个是抽水的时间,抽水的时候是单点往上更新,因为这个点的

如果是某个点的字节点,那么查询这个这个父亲节点点所包含的子树时一点包含要更新的点,然后每个节点的灌水和抽水的最晚的时间就可以了,
然后两次查询比较时间先后就可以了。
复杂度n×log(n);
  1 #include<stdio.h>
2 #include<math.h>
3 #include<queue>
4 #include<algorithm>
5 #include<string.h>
6 #include<iostream>
7 #include<stack>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 typedef vector<int>Ve;
12 vector<Ve>vec(600000);
13 int ans[600000];
14 int id[600000];
15 bool flag[600000];
16 int cn = 0;
17 int l[600000];
18 int r[600000];
19 int pre[600000];
20 void dfs(int n);
21 int tree1[600000*4];
22 int tree0[600000*4];
23 int query(int l,int r,int k,int nn,int mm,int *tr,int c);
24 void update(int l,int r,int k,int nn,int mm,int *tr,int i,int c);
25 int main (void)
26 {
27 int n,x,y;
28 scanf("%d",&n);
29 for(int i = 0; i < n-1; i++)
30 {
31 scanf("%d %d",&x,&y);
32 vec[x].push_back(y);
33 vec[y].push_back(x);
34 }
35 dfs(1);
36 for(int i = 1; i <= n; i++)
37 id[ans[i]] = i;
38 int m;
39 scanf("%d",&m);
40 int ccn = 0;
41 while(m--)
42 {
43 int val,c;
44 ++ccn;
45 scanf("%d %d",&val,&c);
46 if(val == 1)
47 {
48 update(l[c],r[c],0,1,cn,tree1,ccn,1);
49 //printf("%d\n",ccn);
50 }
51 else if(val == 2)
52 {
53 update(id[c],id[c],0,1,cn,tree0,ccn,0);
54 }
55 else
56 {
57 int a = query(id[c],id[c],0,1,cn,tree1,1);
58 int b = query(l[c],r[c],0,1,cn,tree0,0);
59 //printf("%d %d\n",a,b);
60 printf("%d\n",a>b);
61 }
62 }
63 return 0;
64 }
65 void dfs(int n)
66 {
67 flag[n] = true;
68 ans[++cn] = n;
69 l[n] = cn;
70 for(int i = 0; i < vec[n].size(); i++)
71 {
72 int ic = vec[n][i];
73 if(!flag[ic])
74 pre[ic] = n,dfs(ic);
75 }
76 r[n] = cn;
77 }
78 void update(int l,int r,int k,int nn,int mm,int *tr,int i,int c)
79 {
80 if(l > mm||r < nn)
81 {
82 return ;
83 }
84 else if(l <= nn&&r >= mm)
85 {
86 tr[k] = max(tr[k],i);
87 }
88 else
89 {
90 if(tr[k]&&c == 1)tr[2*k+1] = tr[k],tr[2*k+2] = tr[k],tr[k] = 0;
91 update(l,r,2*k+1,nn,(nn+mm)/2,tr,i,c);
92 update(l,r,2*k+2,(nn+mm)/2+1,mm,tr,i,c);
93 if(c == 0)
94 tr[k] = max(tr[k],tr[2*k+1]),tr[k] = max(tr[k],tr[2*k+2]);
95 }
96 }
97 int query(int l,int r,int k,int nn,int mm,int *tr,int c)
98 {
99 if(l > mm||r < nn)
100 {
101 return 0;
102 }
103 else if(l <= nn&&r >= mm)
104 {
105 return tr[k];
106 }
107 else
108 { if(c&&tr[k])tr[2*k+1] = tr[k],tr[2*k+2] = tr[k],tr[k] = 0;
109 int nx = query(l,r,2*k+1,nn,(nn+mm)/2,tr,c);
110 int ny = query(l,r,2*k+2,(nn+mm)/2+1,mm,tr,c);
111 return max(nx,ny);
112 }
113 }
												

D. Water Tree的更多相关文章

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

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

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

  3. 【题解】Luogu CF343D Water Tree

    原题传送门:CF343D Water Tree 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 这明显是弱智题 树剖套珂朵莉树多简单啊 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自 ...

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

  5. Water Tree(树链剖分+dfs时间戳)

    Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...

  6. xtu summer individual 6 F - Water Tree

    Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  7. 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

    D. Water Tree   Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...

  8. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  9. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

随机推荐

  1. 14-Reverse Integer

    思路: 先判定符号,整型范围[-2^32,2^32] 取余除10操作,依次进行,越界返回0 Reverse digits of an integer. Example1: x = 123, retur ...

  2. 巩固javaweb第三天

    巩固内容: HTML 标题 HTML 标题(Heading)是通过<h1> - <h6> 标签来定义的. HTML 段落 HTML 段落是通过标签 <p> 来定义的 ...

  3. javascript的事件循环机制

    JavaScript是一门编程语言,既然是编程语言那么就会有执行时的逻辑先后顺序,那么对于JavaScript来说这额顺序是怎样的呢? 首先我们我们需要明确一点,JavaScript是单线程语言.所谓 ...

  4. 扩展kmp 学习笔记

    学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...

  5. STM32代码常见的坑

    1 混淆换行符\和除号/造成的坑 入坑代码: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin ...

  6. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(四)-介绍库函数,获取一些SD卡的信息

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  7. Shell学习(一)——Shell简介

    参考博客: [1]Shell简介

  8. 超过三张表禁止join

    一. 问题提出 <阿里巴巴JAVA开发手册>里面写超过三张表禁止join,这是为什么? 二.问题分析 对这个结论,你是否有怀疑呢?也不知道是哪位先哲说的不要人云亦云,今天我设计sql,来验 ...

  9. 【Linux】【Basis】文件系统

    FHS:Filesystem Hierarchy Standard Web site: https://wiki.linuxfoundation.org/lsb/fhs http://www.path ...

  10. 数据库系统相关SQL

    杀进程 查出所有被锁住的表 select b.owner TABLEOWNER, b.object_name TABLENAME, c.OSUSER LOCKBY, c.USERNAME LOGINI ...