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. Java 并发编程中的 Executor 框架与线程池

    Java 5 开始引入 Conccurent 软件包,提供完备的并发能力,对线程池有了更好的支持.其中,Executor 框架是最值得称道的. Executor框架是指java 5中引入的一系列并发库 ...

  2. HttpClient配置

    ClientConfiguration.java 该类讲解了HttpClient的各方面的配置 package com.ydd.study.hello.httpclient; import java. ...

  3. getpixel 取色

    HWND hwnd = ::FindWindow(NULL,"<天龙八部OL>"); CRect rect; CString strTmp; if (hwnd != N ...

  4. 《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项

    MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上.MyBatis提供了一级缓存.二级缓存 这两个缓存机制,能够很好地处理和维护缓存,以提高系统的性能.本 ...

  5. Opengl配置

    Opengl配置说明: 本配置文档针对windows64位操作系统,配置vs2008项目工程 1.下载OpenGL的glut类库:http://www.opengl.org/resources/lib ...

  6. android CheckBox使用和状态获得

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  7. [Rust] Pass a JavaScript Function to WebAssembly and Invoke it from Rust

    In some cases it’s useful to be able to invoke a JavaScript function inside Rust. This session showc ...

  8. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. JNI——访问数组

    JNI在处理基本类型数组和对象数组上面是不同的.对象数组里面是一些指向对象实例或者其它数组的引用. 因为速度的原因,先通过GetXXXArrayElements函数把简单类型的数组转化成本地类型的数组 ...

  10. Cocoa root class

    问题来源: 常见面试问题之: NSObject和NSObject protocol有什么区别,为什么要有NSObject protocol, 有没有不继承自NSObject的类? 虽然在iOS开发过程 ...