树链剖分【CF343D】Water Tree
Description
Mad scientist Mike has constructed a rooted tree, which consists of nnvertices. Each vertex is a reservoir which can be either empty or filled with water.
The vertices of the tree are numbered from 1 to nn 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 vv with water. Then vv and all its children are filled with water.
Empty vertex vv . Then vv and all its ancestors are emptied.
Determine whether vertex vv 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-1n−1 lines contains two space-separated numbers \(a_{i}, b_{i}\) ( \(1<=a_{i},b_{i}<=n, a_{i}≠b_{i}\) ) — 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 \(c_{i}( 1<=c_{i}<=3\) ), \(v_{i}\)( \(1<=v_{i}<=n\) ), where \(c_{i}\) is the operation type (according to the numbering given in the statement), and \(v_{i}\) 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.
你不需要理解题意,你只需要知道,这是一个树剖裸题(虽然我没一遍切。)
支持三种操作(初始值全部为\(0\))
- 1.将节点\(v\)及其子树赋值为\(1\).
- 2.将节点\(v\)到根节点\(1\)的路径上的点的值置为\(0\).
- 3.查询当前节点\(v\)的值。(只会为\(0\)或\(1\))
对于每个操作\(3\),输出一行。(具体见代码好了
这是一个不完整的树剖,我没建树,有没用到反\(dfs\)序。 emmm
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define R register
using namespace std;
const int gz=500001;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int head[gz],tot,n,m;
struct cod{int u,v;}edge[gz<<1];
inline void add(R int x,R int y)
{
edge[++tot].u=head[x];
edge[tot].v=y;
head[x]=tot;
}
int dfn[gz],idx,son[gz],f[gz],depth[gz],size[gz],top[gz];
void dfs1(R int u,R int fa)
{
f[u]=fa;depth[u]=depth[fa]+1;size[u]=1;
for(R int i=head[u];i;i=edge[i].u)
{
if(edge[i].v==fa)continue;
dfs1(edge[i].v,u);
size[u]+=size[edge[i].v];
if(son[u]==-1 or size[son[u]]<size[edge[i].v])
son[u]=edge[i].v;
}
}
void dfs2(R int u,R int t)
{
dfn[u]=++idx;top[u]=t;
if(son[u]==-1)return ;
dfs2(son[u],t);
for(R int i=head[u];i;i=edge[i].u)
{
if(dfn[edge[i].v])continue;
dfs2(edge[i].v,edge[i].v);
}
}
int tg[gz<<2],tr[gz<<2];
#define ls o<<1
#define rs o<<1|1
inline void down(R int o)
{
if(tg[o]==-1)return;
tg[ls]=tg[rs]=tg[o];
tr[ls]=tr[rs]=tr[o];
tg[o]=-1;
return ;
}
void change(R int o,R int l,R int r,R int x,R int y,R int k)
{
if(x<=l and y>=r){tr[o]=tg[o]=k;return;}
down(o);
R int mid=(l+r)>>1;
if(x<=mid)change(ls,l,mid,x,y,k);
if(y>mid)change(rs,mid+1,r,x,y,k);
}
int query(R int o,R int l,R int r,R int pos)
{
if(l==r)return tr[o];
down(o);
R int mid=(l+r)>>1;
if(pos<=mid)return query(ls,l,mid,pos);
else return query(rs,mid+1,r,pos);
}
inline void tchange(R int x,R int y)
{
R int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(depth[fx]>depth[fy])
{
change(1,1,n,dfn[fx],dfn[x],0);
x=f[fx];
}
else
{
change(1,1,n,dfn[fy],dfn[y],0);
y=f[fy];
}
fy=top[y],fx=top[x];
}
if(dfn[x]>dfn[y])swap(x,y);
change(1,1,n,dfn[x],dfn[y],0);
return ;
}
int main()
{
in(n);memset(son,-1,sizeof son);
for(R int i=1,x,y;i<n;i++)
{
in(x),in(y);
add(x,y),add(y,x);
}
dfs1(1,0);dfs2(1,1);memset(tg,-1,sizeof tg);
in(m);
for(R int i=1,opt,v;i<=m;i++)
{
in(opt);
switch(opt)
{
case 1:in(v);change(1,1,n,dfn[v],dfn[v]+size[v]-1,1);break;
case 2:in(v);tchange(1,v);break;
case 3:in(v);printf("%d\n",query(1,1,n,dfn[v]));break;
}
}
}
树链剖分【CF343D】Water Tree的更多相关文章
- 【BZOJ3626】LCA(树链剖分,Link-Cut Tree)
[BZOJ3626]LCA(树链剖分,Link-Cut Tree) 题面 Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. ...
- 【BZOJ2157】旅游(树链剖分,Link-Cut Tree)
[BZOJ2157]旅游(树链剖分,Link-Cut Tree) 题面 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥 ...
- 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree
原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...
- 树链剖分 (求LCA,第K祖先,轻重链剖分、长链剖分)
2020/4/30 15:55 树链剖分是一种十分实用的树的方法,用来处理LCA等祖先问题,以及对一棵树上的节点进行批量修改.权值和查询等有奇效. So, what is 树链剖分? 可以简单 ...
- 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 ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Codeforces 343D Water Tree & 树链剖分教程
原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
随机推荐
- 51nod 1831 小C的游戏(博弈论+打表)
比较坑的题目. 题意就是:给出一堆石子,一次操作可以变成它的约数个,也可以拿只拿一个,不能变成一个,最后拿的人输. 经过打表发现 几乎所有质数都是先手必败的,几乎所有合数都是先手必胜的 只有几个例外, ...
- [洛谷P3765]总统选举
题目大意:有$n(n\leqslant5\times10^5)$个数,有$m(m\leqslant5\times10^5)$次询问. 一次询问形如$l\;r\;s\;k\;w_1\;w_2\dots ...
- BZOJ4550 小奇的博弈 【Nimk游戏 + dp + 组合数】
题目 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色.最左边是白色棋子,最右边 是黑色棋子,相邻的棋子颜色不同. 小奇可以移动白色棋子,提比可以移动黑色的棋子,它们每次 ...
- [学习笔记]LCT进阶操作
LCT总结——应用篇(附题单)(LCT) 一般都是维护链的操作.split即可搞定. 进阶操作的话,处理好辅助树和原树的关系即可搞定. 其实,最大的区别就是,splay随便转,辅助树形态变了,但是原树 ...
- bzoj3343: 教主的魔法 分块 标记
修改:两边暴力重构,中间打标记.复杂度:O(n0.5) 查询:中间二分两边暴力.O(n0.5logn0.5) 总时间复杂度O(n*n0.5logn0.5) 空间复杂度是n级别的 标记不用下传因为标记不 ...
- Idea 部署非Maven项目
参考:http://m.blog.csdn.net/z69183787/article/details/78030857 以前一直很好奇,在idea中运行tomcat,把项目部署到其中,运行起来,然后 ...
- 【BZOJ1419】 Red is good [期望DP]
Red is good Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 桌面上有R张红牌和B张 ...
- 【EOJ3652】乘法还原(二分图)
题意: 思路:Orz Claris 先找出所有平方项,将与有平方项的数有关的数对暂时忽略,剩下的直接连边就是一张二分图,暴力DFS染色 将有平方项的数两边都加一个,再判字典序即可 我不会判字典序……耽 ...
- swift mac 使用git, 并使用osc, 打开当前目录命令在终端输入 open . windows 下为start .
使用git.osc而不用github, 因为在osc里面可以设置私有项目,而不需要公开. ssh-keygen -t rsa -C "email@email.com" mac下生成 ...
- 计算Linux权限掩码umask值
创建文件默认最大权限为666 (-rw-rw-rw-),默认创建的文件没有可执行权限x位. 创建目录默认最大权限777(-rwx-rwx-rwx),默认创建的目录属主是有x权限,允许用户进入. 简单的 ...