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. VS2010的一个错误,无法加载类型

    一个解决方案中的一个项目X,启动时总是报错,无法加载一个同一个解决方案中另一个项目A生成EXE中的数据类型. 做了如下的步骤解决问题. 1:检查项目A,未发现错误,调试启动A,一切正常. 2:检查项目 ...

  2. electron打包整理

    最近在折腾把项目打包成桌面应用程序,发现一个工具electron,可以讲项目打包成一个跨平台的应用程序,很方便,来学习一下. 1.先安装electron.electron-packager,安装方法可 ...

  3. border-1px的实现(stylus)

    当样式像素一定时,因手机有320px,640px等.各自的缩放比差异,所以设备显示像素就会有1Npx,2Npx.为保设计稿还原度,解决就是用media + scale. // stylus语法 bor ...

  4. 206 Reverse Linked List 反转链表

    反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...

  5. 转】RMySQL数据库编程指南

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! Posted: Sep 24, 2013 Ta ...

  6. poj2573Bridge(过桥问题)

    链接 A,B为最快和次快 有两种方式可以使c,d过桥 一是a与c一起走,a回来接d再与d一起走,一直到对岸人为0为止 而是 a与b一起走 a回来送灯 c与d一起走 b回来送灯 重复此过程. 只剩2人时 ...

  7. 学习笔记 第七章 使用CSS美化超链接

    第7章  使用CSS美化超链接 学习重点 认识超链接 熟悉伪类 定义超链接样式 能够灵活设计符合页面风格的链接样式 7.1  定义超链接 在HTML5中建立超链接需要两个要素:设置为超链接的网页元素和 ...

  8. Android ImageView setImageBitmap 不显示图片

    从sd卡里读出图片后有时调用setImageBitmap(bitmap)方法会显示不出图片,仔细考虑过后原来是加载的图片过大导致的,解决办法为: BitmapFactory.Options op = ...

  9. Shiro 自定义登陆、授权、拦截器

    Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...

  10. asterisk-java ami5 分机状态,挂机原因之类的

    这些东西网上随便一找一大堆,也只是记录下自己找的.方便以后自己复制粘贴用. 最后为啦实现分机状态在web的实时更新,我选择啦使用websocket. //获得分机状态 public static St ...