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 ...
随机推荐
- bootstrap基本布局
中文API bootstrap.cn HTML5文档 类型 移动设备优先 width 属性控制设备的宽度.设置为 device-width 确保它能正确呈现在不同设备上. initial-sc ...
- JS中的逻辑运算符&&、||,位运算符|,&
1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...
- 日常记录-代码中Background后Padding 失效
近日,在开发过程中 遇到了 Layout 代码中设置 Background 后,padding失效的问题,只是在Android 4.4.4 和 4.4.2 的手机上遇到了. 网上搜索了下,说是 4.4 ...
- ios开发介绍
iOS开发概述 •什么是IOS •什么是IOS开发 •为什么要选择IOS开发 •学习IOS开发的准备 1.什么是iOS •iOS是一款由苹果公司开发的操作系统(OS是Operating Sys ...
- Microsoft SQL Server学习(二)
目录 关于数据库的语法: 1.创建数据库 create database 数据库名 on primary (主文件属性(name,filename,size等)) -用逗号隔开次要主要文件和次要文件 ...
- vue2.0 路由知识一(路由的创建的全过程)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Win7 32位 遇到微软 silverlight 5.0安装失败的解决办法
刚开始,也是尝试下载安装,多次都是到99%,提示安装失败! 也查找了很多网上朋友分享的办法,还是不行.重新建立一个管理员账号,还是不行. 后来反复不断的测试,找到原因了,安装99%不成功,但是卸载程序 ...
- app dcloud 打包公用证书
Android平台云端打包使用的DCloud公用证书 分类:HTML5+ 5+App开发 HBuilder|HBuilderX应用云端打包Android平台默认使用的DCloud公用证书,其信息如下: ...
- WPF学习- 新建项目后自定义Main()[Type 'App' already defines a member called 'Main' with the same parameter types]
问题点: 在App.xaml.cs中自己添加Main方法,编译会出现如下报错: 错误 CS0111 类型“App”已定义了一个名为“Main”的具有相同参数类型的成员 错误 Type 'App' a ...
- vc++实现控制USB设备启用与否
#include <WINDOWS.H> #include <TCHAR.H> #include <SETUPAPI.H> //#in ...