D. Water Tree
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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

思路:跟bzoj4034类似;

   传送tp上线;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=; ///数组大小
struct edge
{
int v,next;
} edge[N<<];
int head[N<<],edg,id,n;
/// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int ran[N],top[N],tid[N],mx[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
edg=;
id=;
} void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
} void dfs1(int u,int fath,int deep)
{
fa[u]=fath;
siz[u]=;
dep[u]=deep;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fath)continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
} void dfs2(int u,int tp)
{
tid[u]=mx[u]=++id;
top[u]=tp;
ran[tid[u]]=u;
if(son[u]==-)return;
dfs2(son[u],tp),mx[u]=max(mx[u],mx[son[u]]);
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u])continue;
if(v!=son[u])
dfs2(v,v),mx[u]=max(mx[u],mx[v]);
}
} struct SGT
{
int sum[N<<],lazy[N<<];
void pushup(int pos)
{
sum[pos]=sum[pos<<]+sum[pos<<|];
}
void pushdown(int pos,int l,int r)
{
if(lazy[pos]!=-)
{
int mid=(l+r)>>;
lazy[pos<<]=lazy[pos];
lazy[pos<<|]=lazy[pos];
sum[pos<<]=lazy[pos]*(mid-l+);
sum[pos<<|]=lazy[pos]*(r-mid);
lazy[pos]=-;
}
}
void build(int l,int r,int pos)
{
lazy[pos]=-;
sum[pos]=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int L,int R,int c,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
sum[pos]=c*(r-l+);
lazy[pos]=c;
return;
}
pushdown(pos,l,r);
int mid=(l+r)>>;
if(L<=mid)update(L,R,c,l,mid,pos<<);
if(R>mid) update(L,R,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int p,int l,int r,int pos)
{
if(l==r)return sum[pos];
pushdown(pos,l,r);
int mid=(l+r)>>;
if(p<=mid)return query(p,l,mid,pos<<);
else return query(p,mid+,r,pos<<|);
}
}tree; void up(int l,int r)
{
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
tree.update(tid[top[l]],tid[l],,,n,);
l=fa[top[l]];
}
if(dep[l]<dep[r])swap(l,r);
tree.update(tid[r],tid[l],,,n,);
} int main()
{
init();
scanf("%d",&n);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(,-,);
dfs2(,);
tree.build(,n,);
int q;
scanf("%d",&q);
while(q--)
{
int t,x;
scanf("%d%d",&t,&x);
if(t==) tree.update(tid[x],mx[x],,,n,);
else if(t==) up(,x);
else printf("%d\n",tree.query(tid[x],,n,));
}
return ;
}

Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树的更多相关文章

  1. Codeforces Round #200 (Div. 1)D. Water Tree

    简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...

  2. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  3. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  4. 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构

    D. Water Tree   Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...

  5. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  8. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  9. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

随机推荐

  1. 一个Java系统测试

    实验感受: 本次实验最大的感受,就是不要改代码,自己写,代码改起来真的没完没了,不知道会出现什么问题.还有就是一定要清楚自己要怎么去写,流程很重要,一个个功能去实现. 主界面 数据库 主页面代码 &l ...

  2. 自学Java第三个星期的总结

    在这一周里我在网上学习了java的分支结构.Number&Matht类.Character类.string类.String Buffer和String Builder类以及数组和日期时间等有关 ...

  3. webform 使用富文本编辑器

    <div class="form-group"> <label class="col-xs-2 control-label text-right&quo ...

  4. 计算概论(A)/基础编程练习2(8题)/5:点和正方形的关系

    #include<stdio.h> #include<math.h> int main() { // 输入坐标 float x, y; while(scanf("%f ...

  5. A-作业01

    #1 简单作业 1. 系统的日志文件/var/log/secure /var/log/messages /var/log/cron会自动的进行轮询,系统是通过什么实现的? 2. 写出下面特殊符号在定时 ...

  6. python使用super()调用父类的方法

    如果要在子类中引用父类的方法,但是又需要添加一些子类所特有的内容,可通过类名.方法()和super()来调用父类的方法,再个性化子类的对应函数. 直接使用类名.方法()来调用时,还是需要传入self为 ...

  7. TCP 之 RST 原因分析

    5. 往一个对端已经关闭的套接字上写入数据会收到一个RST信号 1.发送端的 发送缓冲区还有数据,但接收端tcp的接收通道已关闭 2. SYN到达某端口但此端口上没有正在监听的服务器.对于UDP,当一 ...

  8. bash 配置文件

    两类: profile类:为交互式登录的shell进程提供配置 bashrc类:为非交互式登录的shell进程提供配置 登录类型: 交互式登录shell进程: 直接通过某终端输入账号和密码后登录打开的 ...

  9. 位运算之a^b

    题目链接:https://www.acwing.com/problem/content/91/ 参考链接:https://blog.csdn.net/chaiwenjun000/article/det ...

  10. CentOS7 安装git服务器

    在CentOS7系统中安装git服务器有两种方法,分别为yum安装和下载git安装包手动安装,这篇文章只有下载git安装包手动安装方法. 方法一:使用yum安装 暂无 方法二:下载git安装包手动安装 ...