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. (45)C#网络3 socket

    一.TCP传输 using System.Net.Sockets; 1.最基本客户端连服务器 服务端运行后一直处于监听状态,客户端每启动一次服务端就接收一次连接并打印客户端的ip地址和端口号.(服务端 ...

  2. (1)git

    1.创建一个版本库 #创建一个文件夹 E:\>mkdir pythonGit #进入文件夹 E:\>cd pythonGit #把此目录创建成git版本库 E:\pythonGit> ...

  3. HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)

    题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...

  4. android开发教程之使用线程实现视图平滑滚动示例 改

    package com.melonsapp.messenger.ui.popupuser; import android.os.Handler; import android.view.View; i ...

  5. 微信小程序 - 提取字体图标与其优化

    微信小程序,无论是字体图标还是图标,都差不多,只不过是为了以后字体图标修改方便,或者加效果方便而使用它而已! 1. 下载font-awesome http://fontawesome.dashgame ...

  6. Odoo HR Payslip

    pay slip 可以录入多条 worked_days_line 和 input_line,用来人工调整薪资变动部分,比如销售提成,扣款等. pay slip 可以包含多个pay slip line ...

  7. 分析Cocos2d-x横版ACT手游源码 1、公共

    直接上代码 不说什么 这一款源码 凝视及多 PublicDef.h 公共头文件 #define NF_PLATFORM 1 //当前版本号(默觉得普通版) //版本号列表 #define NF_PLA ...

  8. NSArray中存的是实体时的排序

    NSArray中存储的是实体时的排序 by 伍雪颖 NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKe ...

  9. C#不用union,而是有更好的方式实现 .net自定义错误页面实现 .net自定义错误页面实现升级篇 .net捕捉全局未处理异常的3种方式 一款很不错的FLASH时种插件 关于c#中委托使用小结 WEB网站常见受攻击方式及解决办法 判断URL是否存在 提升高并发量服务器性能解决思路

    C#不用union,而是有更好的方式实现   用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C# ...

  10. 小博客| 登录 | 注册 | 留言 | 提Bug 小博客

     浏览(1502)  赞(29) 一直以来都想开发一个自己的网站,总想做一个网站然后让千千万万的人去访问,去使用,然后收到热烈的好评, 再然后某某著名机构有意投资我的网站(其实收购也是没有问题的), ...