xtu summer individual 6 F - Water Tree
Water Tree
This problem will be judged on CodeForces. Original ID: 343D
64-bit integer IO format: %I64d Java class name: (Any)
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:
- Fill vertex v with water. Then v and all its children are filled with water.
- Empty vertex v. Then v and all its ancestors are emptied.
- 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 ai, bi(1 ≤ ai, bi ≤ n, ai ≠ 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
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
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的更多相关文章
- xtu summer individual 5 F - Post Office
Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...
- xtu summer individual 3 F - Opening Portals
Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- 【题解】Luogu CF343D Water Tree
原题传送门:CF343D Water Tree 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 这明显是弱智题 树剖套珂朵莉树多简单啊 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自 ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- 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/ ...
- 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 ...
- 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 ...
随机推荐
- bryce1010专题训练——线段树习题汇总
一.区间查询,无单点更新 hdu2795 Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 转-MAC 下安装PHONEGAP开发环境
来自:http://jinzhe.net/post/8.html 什么是Phonegap呢?Phonegap是一个利用HTML5去开发App的框架.可以为安卓.iOS.WP.黑莓.火狐等移动操作系统. ...
- [转]为革命保护视力 --- 给 Visual Studio 换颜色
本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...
- String的用法——其他功能
package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...
- 工具类学习-java实现邮件发送激活码
问题:用java实现服务器发送激活码到用户邮件. 步骤一:如果是个人的话,确保在本地安装邮件服务器(易邮服务器)和邮件客户端(foxmail). 步骤二:导入jar包 mail.jar,其他的需要什 ...
- (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记
1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...
- Toolbar自定义布局
Toolbar如何使用想必大家清楚地很,实际开发中标题栏的样式各色各样,因此其基本样式便不能满足我们的需求,这就需要我们自定义布局.打开ToolBar源码我们发现它继承ViewGroup,这就表示我们 ...
- 【数据分析 R语言实战】学习笔记 第三章 数据预处理 (下)
3.3缺失值处理 R中缺失值以NA表示,判断数据是否存在缺失值的函数有两个,最基本的函数是is.na()它可以应用于向量.数据框等多种对象,返回逻辑值. > attach(data) The f ...
- 从0开始搭建SQL Server 2012 AlwaysOn 第二篇(配置故障转移集群)
本篇主要讲配置Windows 故障转移集群及遇到的相关问题(坑),因为AlwaysOn是基于Windows的故障转移集群的 在讲解步骤之前需要了解一下故障转移集群仲裁配置 四种集群的仲裁配置: 1.多 ...
- 【译】x86程序员手册36-9.9异常汇总
9.9 Exception Summary 异常汇总 Table 9-6 summarizes the exceptions recognized by the 386. Table 9-6. Exc ...