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

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

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

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

路2:2操作用树链剖分,避免了暴力修改,效率还过得去。

(下面的思路1的代码)

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt;
int fa[maxn],in[maxn],out[maxn],times;
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int pre)
{
fa[u]=pre; in[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre) dfs(v,u);
} out[u]=times;
}
int Lazy[maxn<<],all[maxn<<];
struct DFSTree{
void pushdown(int Now)
{
if(Lazy[Now]){
Lazy[Now]=;
all[Now<<]=; all[Now<<|]=;
Lazy[Now<<]=; Lazy[Now<<|]=;
}
}
void pushup(int Now)
{
all[Now]=all[Now<<]&all[Now<<|];
}
void add(int Now,int L,int R,int l,int r)
{ if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) add(Now<<,L,Mid,l,r);
if(r>Mid) add(Now<<|,Mid+,R,l,r);
pushup(Now);
}
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return all[Now];
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid){ if(!query(Now<<,L,Mid,l,r)) return ;}
if(r>Mid) { if(!query(Now<<|,Mid+,R,l,r)) return ;}
pushup(Now);
return ;
}
void del(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) del(Now<<,L,Mid,l,r);
if(r>Mid) del(Now<<|,Mid+,R,l,r);
/*else{
del(Now<<1,L,Mid,l,r);
del(Now<<1|1,Mid+1,R,l,r);
} */
pushup(Now);
}
}Tree;
int main()
{
int N,Q,u,v,i,j,opt;
scanf("%d",&N);
for(i=;i<N;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(,);
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&opt,&u);
if(opt==){
if(!Tree.query(,,N,in[u],out[u]))
Tree.del(,,N,in[fa[u]],in[fa[u]]);
Tree.add(,,N,in[u],out[u]);
}
else if(opt==) Tree.del(,,N,in[u],in[u]);
else printf("%d\n",Tree.query(,,N,in[u],out[u]));
}
return ;
}

加了输入优化后排第二了。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt;
int fa[maxn],in[maxn],out[maxn],times;
void read(int &res){
char c=getchar();
for(;c>''||c<'';c=getchar());
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int pre)
{
fa[u]=pre; in[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre) dfs(v,u);
} out[u]=times;
}
int Lazy[maxn<<],all[maxn<<];
struct DFSTree{
void pushdown(int Now)
{
if(Lazy[Now]){
Lazy[Now]=;
all[Now<<]=; all[Now<<|]=;
Lazy[Now<<]=; Lazy[Now<<|]=;
}
}
void pushup(int Now)
{
all[Now]=all[Now<<]&all[Now<<|];
}
void add(int Now,int L,int R,int l,int r)
{ if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) add(Now<<,L,Mid,l,r);
if(r>Mid) add(Now<<|,Mid+,R,l,r);
pushup(Now);
}
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return all[Now];
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) if(!query(Now<<,L,Mid,l,r)) return ;
if(r>Mid) if(!query(Now<<|,Mid+,R,l,r)) return ;
pushup(Now);
return ;
}
void del(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) {
Lazy[Now]=; all[Now]=; return ;
}
int Mid=(L+R)>>;
pushdown(Now);
if(l<=Mid) del(Now<<,L,Mid,l,r);
if(r>Mid) del(Now<<|,Mid+,R,l,r);
pushup(Now);
}
}Tree;
int main()
{
int N,Q,u,v,i,j,opt;
read(N);
for(i=;i<N;i++){
read(u); read(v);
add(u,v); add(v,u);
}
dfs(,);
read(Q);
while(Q--){
read(opt); read(u);
if(opt==){
if(!Tree.query(,,N,in[u],out[u]))
Tree.del(,,N,in[fa[u]],in[fa[u]]);
Tree.add(,,N,in[u],out[u]);
}
else if(opt==) Tree.del(,,N,in[u],in[u]);
else printf("%d\n",Tree.query(,,N,in[u],out[u]));
}
return ;
}

CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

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

  4. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  5. codechef T6 Pishty and tree dfs序+线段树

    PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...

  6. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  7. CodeForces 620E:New Year Tree(dfs序+线段树)

    E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...

  8. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  9. 【cf343】D. Water Tree(dfs序+线段树)

    传送门 题意: 给出一个以\(1\)为根的有根树,起始每个结点都为\(0\),现在有三种操作: 1.将\(v\)及\(v\)的子树都置为\(1\): 2.将\(v\)及其所有的祖先都置为\(0\): ...

  10. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

随机推荐

  1. Js 中的输出

    document.write()和window.alert() 1.window.document.write(字符串或者是变量名) 作用:它会在body标签内输出内容 说明: window代表当前浏 ...

  2. 扰动函数和拉链法模拟HashMap的存储结构

    HashMap是Map接口下面的子孙,它对外是K,V结构存储的,而内部也着自己的存储结构,它的get操作是O(1)的时间复杂度,可以说是非常快的找到目录,而添加时,也是O(1),所以在键值存储里,它成 ...

  3. NOIP临考经验(转)

    [COGS]NOIP临考经验 1.  提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.  打开编辑器并调整为自己喜欢的界面 3.  熟悉文件目录,写好准确无误的代码模板 4.  压缩包或许还不能 ...

  4. cef network-settings

    Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...

  5. linux svn配置hooks

    先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...

  6. Mysql数据库中CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别

    如图所示,mysql数据库中,当字段类型为timestamp时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,end_time的值自动设置为系统当前时间,如果勾选了 ON ...

  7. ImportError: No module named _curses;Color support is disabled, python-curses is not installed.解决办法

    linux系统默认安装了python2.6, 但是发现python2.7 import curses时 提示 找不到_curses 错误.  用pip(python2.7 )安装了curses-204 ...

  8. Objective-C之成魔之路【0-序章】

    郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 C语言首创 ...

  9. 关于颜色(color、background)

    CSS3 HSL colors使用参考指南语法:<length> || <percentage> || <percentage>取值:<length> ...

  10. 【万里征程——Windows App开发】控件大集合1

    加入控件的方式有多种.大家更喜欢哪一种呢? 1)使用诸如 Blend for Visual Studio 或 Microsoft Visual Studio XAML 设计器的设计工具. 2)在 Vi ...