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

Posted ---学习ing---

tags:

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

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.

Example

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

题意:给定一棵树,没棵树上有一个水池,现在有如下操作,

          1,给V和其子树装水。

          2,对V和其祖先排水。

          3,问V是否有水。

思路:看到操作1,操作子树,很自然地想到dfs序;然后但是2操作,V及其祖先在线段树上的位置是离散的,所以需要暴力。

           转化一下思路:本来是多点更改2,单点查询3; 改为:单点更新2,区间查询3。

           即:3操作改为,查询V及其子树是否都有水。

注意:对1操作,假设V的子树有空(存在无水的点)的,则需要把V的父亲改为空。     因为V的子树有空,则V的父亲为空,而且给V和子树加水后V父亲任然为空。但是由于没有单点更改V的父亲,而V的父亲和其子树都满水,则导致误判以为V的父亲有水。  而V的父亲最具有代表性,只改V的父亲节点即可。

 

以上是关于CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)的主要内容,如果未能解决你的问题,请参考以下文章

CF343D Water Tree

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

Codeforces 343D Water Tree

CF343D Water Tree 树链剖分

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

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