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 ...
随机推荐
- [POI2007]山峰和山谷Grz
Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...
- 洛谷 P1600 天天爱跑步
https://www.luogu.org/problemnew/show/P1600 (仅做记录) 自己的假方法: 每一次跑从a到b:设l=lca(a,b)对于以下产生贡献: a到l的链上所有的点( ...
- 169 Majority Element 求众数 数组中出现次数超过一半的数字
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素.你可以假设数组是非空的,并且数组中的众数永远存在. 详见:https://leetcode.com/p ...
- windows系统下在忘记安装make的Cygwin中如何正确安装make(图文详解)
由于我在安装cygwin时忘了包含make包,所以安装后发现我在bash中无法使用make命令.但是一般在cygwin下面的软件都是要用make来实现编译和安装的.没有make,又如何编译生成make ...
- Java多线程——进程和线程
Java多线程——进程和线程 摘要:本文主要解释在Java这门编程语言中,什么是进程,什么是线程,以及二者之间的关系. 部分内容来自以下博客: https://www.cnblogs.com/dolp ...
- Android学习笔记(十) Activity的生命周期
一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...
- web页面打印--铺满A4
css <style type="text/css"> body { margin: 0; padding: 0; background-color: #FAFAFA; ...
- Protostuff序列化和反序列化
序列化和反序列化是在应对网络编程最常遇到的问题之一. 序列化就是将Java Object转成byte[]:反序列化就是将byte[]转成Java Object. 这里不介绍JDK serializab ...
- vue2.0 组件化
简单理解其实组件就是制作自定义的标签,这些标签在HTML中是没有的. 组件注册的是一个标签,而指令注册的是已有标签里的一个属性.在实际开发中我们还是用组件比较多,指令用的比较少. <!DOCTY ...
- C++ 异常处理(try catch throw)、命名空间
一.c++工具 模板(函数模板.类模板).异常处理.命名空间等功能是c++编译器的功能,语言本身不自带,这些功能已经成为ANSI C++标准了,建议所有的编译器都带这些功能,早期的c++是没有这些功能 ...