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 ...
随机推荐
- java实验五——字符数组、String、StringBuffer的相互转化,StringBuffer的一些方法
package hello; import java.util.Scanner; public class 实验五 { public static void main(String[] args) { ...
- sublime text 3 3143
下载链接:https://download.sublimetext.com/Sublime%20Text%20Build%203143%20x64%20Setup.exe 注册信息:sublime t ...
- [转]将字体嵌入程序资源中 C# Winform
http://social.msdn.microsoft.com/Forums/officeapps/zh-CN/61b717ae-f925-443a-baad-2b85f2564826/cwinfo ...
- [UE4]机器人自动寻路
要让机器人能够自动寻路,需要画出自动寻路的范围,可以使用“Nav Mesh Bounds Volume”组件来自定寻路范围 通过“Delay”节点可以实现让AI执行Move To以后停顿1秒,然后继 ...
- unity3d将C#打包成dll方法
方法一:用vs新建工程-C#库,添加UnityEngine.dll引用,注意.netframwork选3.5,编译C#脚本得到dll: 方法二:使用mono的mcs,具体如下 c#提供了dll打包,但 ...
- HTML5之viewport使用
好久都没更新博客了,最近一年转型移动端,当然网页端也得兼顾,慢慢写一写基本性的文章,多积累. 本期介绍下viewport的一些使用: 先看看viewport在页面中的样子: <meta name ...
- python的分支循环
知识内容: 1.if-else分支结构 2.while循环 3.for循环 4.循环结构综述 5.break和continue语句 一.if-else分支结构 1.单分支选择结构 if 表达式: 语句 ...
- vi规范
pep8规范 # vi规范## , 后空一格# 函数和其他代码空两行
- !!! jquery mobile常用代码
Jquery MOBILE: (2014-7-1 发布jquery.mobile 1.4.3版本) <!doctype html> <html> <head> & ...
- VisualSVN:强制必须填写日志信息
上回将到怎么修改已提交的版本日志信息,而开发项目过程中团队中总是有人忘记添加日志信息注释直接提交,这样会后期维护带来不便. 现在先演示一下效果 当直接提交一个空白日志信息时 有填写日志信息时 那怎么实 ...