Water Tree(树链剖分+dfs时间戳)
Water Tree
http://codeforces.com/problemset/problem/343/D
4 seconds
256 megabytes
standard input
standard output
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:
- 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.
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.
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.
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
树链剖分模板题
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 500005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; int tree[maxn<<],lazy[maxn<<];
int n;
int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
vector<int>ve[maxn]; void pushup(int rt){
if(tree[rt<<]||tree[rt<<|])
tree[rt]=;
else
tree[rt]=;
} void pushdown(int rt){
if(lazy[rt]!=-){
lazy[rt<<]=lazy[rt];
lazy[rt<<|]=lazy[rt];
tree[rt<<]=lazy[rt];
tree[rt<<|]=lazy[rt];
lazy[rt]=-;
}
} void build(int l,int r,int rt){
lazy[rt]=-;
if(l==r){
tree[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void add(int L,int R,int k,int l,int r,int rt){
if(L<=l&&R>=r){
tree[rt]=k;
lazy[rt]=k;
return;
}
int mid=(l+r)/;
pushdown(rt);
if(L<=mid) add(L,R,k,lson);
if(R>mid) add(L,R,k,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt];
}
int mid=(l+r)/;
pushdown(rt);
int ans=;
if(L<=mid) if(ans||query(L,R,lson)) ans=;
if(R>mid) if(ans||query(L,R,rson)) ans=;
pushup(rt);
return ans;
} void dfs1(int now,int f,int deep){
dep[now]=deep;
siz[now]=;
fa[now]=f;
int maxson=-;
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==f) continue;
dfs1(ve[now][i],now,deep+);
siz[now]+=siz[ve[now][i]];
if(siz[ve[now][i]]>maxson){
maxson=siz[ve[now][i]];
son[now]=ve[now][i];
}
}
} void dfs2(int now,int topp){
id[now]=++cnt;
top[now]=topp;
if(!son[now]) return;
dfs2(son[now],topp);
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==son[now]||ve[now][i]==fa[now]) continue;
dfs2(ve[now][i],ve[now][i]);
}
} void addRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(id[x],id[y],k,,n,);
} void addSon(int x,int k){
add(id[x],id[x]+siz[x]-,k,,n,);
} int main(){
std::ios::sync_with_stdio(false);
cin>>n;
int m;
int pos,z,x,y;
for(int i=;i<n;i++){
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
cnt=;
dfs1(,,);
dfs2(,);
build(,n,);
cin>>m;
for(int i=;i<=m;i++){
cin>>pos>>x;
if(pos==){
addSon(x,);
}
else if(pos==){
addRange(,x,);
add(id[x],id[x],,,n,);
}
else if(pos==){
if(query(id[x],id[x],,n,)){
cout<<<<endl;
}
else{
cout<<<<endl;
}
}
} }
Water Tree(树链剖分+dfs时间戳)的更多相关文章
- 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 树链剖分+线段树
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- CF343D Water Tree 树链剖分
问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- 树链剖分||dfs序 各种题
1.[bzoj4034][HAOI2015]T2 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把 ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
随机推荐
- HUD 1175 连连看
连连看 Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- js代码的执行顺序及运算
代码执行顺序:从上往下,一行一行的执行(也叫一个模块一个模块的执行) 变量的提升(它不是变量的功能,而是浏览器的功能) js代码如何执行? js代码执行前,浏览器会给他一个全局的环境 叫window, ...
- jquery的相关应用
1.jQuery获取鼠标事件源(万能) 1 //任意位置 2 3 $(document).ready(function(){ 4 5 $(document).click(function(){ 6 $ ...
- Tornado 框架的使用
Tornado tornado是一个轻量级python的web框架,他是非阻塞式的,而且速度非常快.得利于其 非阻塞的方式和对 epoll 的运用,Tornado 每秒可以处理数以千计的连接,这意味着 ...
- 学习MongoDB 四: MongoDB查询(一)
一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...
- selenium+python自动化87-Chrome浏览器静默模式启动(headless)
前言 selenium+phantomjs可以打开无界面的浏览器,实现静默模式启动浏览器完成自动化测试,这个模式是极好的,不需要占用电脑的屏幕. 但是呢,phantomjs这个坑还是比较多的,并且遇到 ...
- 代码:css小图标
向下小箭头 .icon-tip{ border-color: transparent transparent #bb0808 transparent; border-style:solid; bord ...
- 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输
进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...
- as3 object与dictionary区别
AS3中的Dictionary类(flash.utils.Dictionary)是一个新的AS类.Dictionary类和Object唯一的区别在于:Dictionary对象可以使用非字符串作为键值对 ...
- python常见异常提示
PEP8 expected 2 blank lines, found 1 定义方法时,出现期望是2个空白行,但是实际检测到是1个.方法与上面内容间隔期望为两个换行符 PEP8 This diction ...