Water Tree

Time Limit: 4000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 343D
64-bit integer IO format: %I64d      Java class name: (Any)

 
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.

 

Sample Input

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

Source

 
解题:线段数。。。。。哎 不解释了!如果孩子节点空,那么祖先节点空!
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct node{
int lt,rt;
};
struct Tnode{
int lt,rt,water,lazy;
};
Tnode tree[maxn<<];
int pre[maxn],cnt,n,m;
bool vis[maxn];
vector<int>g[maxn];
node p[maxn];
void dfs(int u,int f){
pre[u] = f;
vis[u] = true;
p[u].lt = ++cnt;
for(int v = ; v < g[u].size(); v++)
if(!vis[g[u][v]]) dfs(g[u][v],u);
p[u].rt = cnt;
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].water = ;
tree[v].lazy = -;
if(lt == rt) return;
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
}
void push_up(int v){
tree[v].water = min(tree[v<<].water,tree[v<<|].water);
}
void push_down(int v){
if(tree[v].lazy != -){
tree[v<<].water = tree[v<<|].water = tree[v].water;
tree[v<<].lazy = tree[v<<|].lazy = tree[v].lazy;
tree[v].lazy = -;
}
}
void addWater(int lt,int rt,int v){
if(tree[v].lt == lt && tree[v].rt == rt){
tree[v].lazy = tree[v].water = ;
return;
}
push_down(v);
int mid = (tree[v].lt+tree[v].rt)>>;
if(rt <= mid) addWater(lt,rt,v<<);
else if(lt > mid) addWater(lt,rt,v<<|);
else {
addWater(lt,mid,v<<);
addWater(mid+,rt,v<<|);
}
push_up(v);
}
void emptyWater(int x,int v){
if(tree[v].lt == tree[v].rt){
tree[v].water = ;
return;
}
push_down(v);
int mid = (tree[v].lt+tree[v].rt)>>;
if(x <= mid) emptyWater(x,v<<);
else emptyWater(x,v<<|);
push_up(v);
}
int query(int lt,int rt,int v){
if(tree[v].lt == lt && tree[v].rt == rt){
return tree[v].water;
}
push_down(v);
int mid = (tree[v].lt+tree[v].rt)>>;
if(rt <= mid) return query(lt,rt,v<<);
else if(lt > mid) return query(lt,rt,v<<|);
else return min(query(lt,mid,v<<),query(mid+,rt,v<<|));
}
int main(){
int i,j,u,v,tmp;
while(~scanf("%d",&n)){
for(i = ; i <= n; i++){
g[i].clear();
vis[i] = false;
}
for(i = ; i < n; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
cnt = ;
dfs(,);
build(,cnt,);
scanf("%d",&m);
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
if(u == ){
tmp = query(p[v].lt,p[v].rt,);
addWater(p[v].lt,p[v].rt,);
if(pre[v] && !tmp) emptyWater(p[pre[v]].lt,);
}else if(u == ){
emptyWater(p[v].lt,);
}else if(u == ){
printf("%d\n",query(p[v].lt,p[v].rt,));
}
}
}
return ;
}
 

xtu summer individual 6 F - Water Tree的更多相关文章

  1. xtu summer individual 5 F - Post Office

    Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...

  2. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  3. 【题解】Luogu CF343D Water Tree

    原题传送门:CF343D Water Tree 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 这明显是弱智题 树剖套珂朵莉树多简单啊 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自 ...

  4. Water Tree(树链剖分+dfs时间戳)

    Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...

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

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

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

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

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

  8. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

随机推荐

  1. CF916E

    Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...

  2. C# 基础知识和VS2010的小技巧总汇

    看了一些基础视频,才发现自己的基础比较薄弱,有很多基础知识都不知道.这里总汇一些基础知识. 1: foreach不仅可以作用于list类的索引集合,还可以遍历dictionary类,这一点比for更简 ...

  3. 3、InputStream

    package com.io.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoun ...

  4. jsp错误处理

    jsp提供了很好的错误能力,除了在java代码中可以使用try语句,还可以指定一个特殊页面,当页面应用遇到未捕获的异常时,用户将看到一个精心设计的网页解释发生了什么,而不是一个用户无法理解的错误信息. ...

  5. 468 Validate IP Address 验证IP地址

    详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...

  6. android开发学习 ------- 【转】Gradle相关

    一直在用AndroidStudio,但是对于其Gradle了解的很少. 推荐 http://www.jianshu.com/p/9df3c3b6067a  觉得说的很棒!

  7. Oracle中的表空间

    表空间是什么? Oracle数据库包含逻辑结构和物理结构. 数据库的物理结构是指构成数据库的一组操作系统文件. 数据库的逻辑结构是指描述数据组织方式的一组逻辑概念及它们之间的关系. 表空间是数据库数据 ...

  8. easy ui combotree的操作

    1.获取combotree的选中值 $("#id").combotree("getValue"); 2.设置combotree的选中值 $('#id').com ...

  9. js内置对象总结

    在js里,一切皆为或者皆可以被用作对象.可通过new一个对象或者直接以字面量形式创建变量(如var i="aaa"),所有变量都有对象的性质. 注意:通过字面量创建的对象在调用属性 ...

  10. 阿里云服务器安装ss使用

    下载安装服务器版shadowsocks yum install epel-release yum update yum install python-setuptools m2crypto super ...