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. compose.yml模板文件

    默认的模板文件名称为 docker-compose.yml,格式为 YAML 格式. 示例: version: " services: webapp: image: examples/web ...

  2. ORA-01940: cannot drop a user that is currently connected 问题解析

    https://www.linuxidc.com/Linux/2012-12/76448.htm

  3. BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 7 ...

  4. OO第三单元作业小结

    一.JML理论基础及应用工具链情况 理论基础 1.JML表达式 \result:表示方法执行后的返回值. \old(expr):表示一个表达式expr在相应方法执行前的取值. \foall:全称量词修 ...

  5. Scrum软件开发

    Scrum 什么是Scrum Scrum是迭代式增量软件开发过程,通常用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过程骨架.Scrum中的主要角色包括同项目经理类似的Scrum主管角色负 ...

  6. 【面试 JVM】【第六篇】JVM调优

    六部分内容: 一.内存模型 1.程序计数器,方法区,堆,栈,本地方法栈的作用,保存那些数据 可以画个大图出来,很清晰 jvm内存模型主要指运行时的数据区,包括5个部分. 栈也叫方法栈,是线程私有的,线 ...

  7. 转: memcache, redis, mongodb 对比

    http://db-engines.com/en/system/Memcached%3BMongoDB%3BRedis

  8. 安卓开发懒鬼最爱之ButterKnife,依赖注入第三方是库,进一步加速开发速度

    转载请注明出处:王亟亟的大牛之路 还在烦躁一大堆findById的控件操作而烦恼么? 平时,我们的那一系列findById是一个"浩大的project"样比例如以下 这是以前一个项 ...

  9. 高速清除winXP系统中explorer.exe病毒

    关于这个explorer.exe病毒.是眼下xp最为常见的一个病毒,会大量的消耗系统资源,造成电脑特别的卡顿. 1.关闭还原(假设没有,则跳过),为的是防止我们改动后,还原之后又回来了. 2.打开注冊 ...

  10. webpack2 详解

    1.安装 npm install webpack -g npm install webpack -save-dev 2.编辑配置文件 // 引入 path var path=require('path ...