【CF343D】 Water Tree(树链剖分)
题目链接
树剖傻逼题,练练手好久没写树剖了。
查询忘记\(pushdown\)抓了好久虫。。
全文手写,一遍过。。。
#include <cstdio>
const int MAXN = 500010;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
struct Edge{
int next, to;
}e[MAXN << 1];
int num, head[MAXN];
inline void Add(int from, int to){
e[++num].to = to; e[num].next = head[from]; head[from] = num;
e[++num].to = from; e[num].next = head[to]; head[to] = num;
}
int n, m;
#define lc (now << 1)
#define rc (now << 1 | 1)
int val[MAXN << 2], lazy[MAXN << 2];
inline void pushup(int now){
val[now] = val[lc] && val[rc];
}
inline void pushdown(int now){
if(lazy[now] == 1){
lazy[lc] = lazy[rc] = 1;
val[lc] = val[rc] = 0;
lazy[now] = 0;
}
if(lazy[now] == 2){
lazy[lc] = lazy[rc] = 2;
val[lc] = val[rc] = 1;
lazy[now] = 0;
}
}
void Update(int now, int l, int r, int wl, int wr, int p){
if(l > wr || r < wl) return;
if(l >= wl && r <= wr){ val[now] = p; lazy[now] = p + 1; return; }
pushdown(now);
int mid = (l + r) >> 1;
Update(lc, l, mid, wl, wr, p);
Update(rc, mid + 1, r, wl, wr, p);
pushup(now);
}
int Query(int now, int l, int r, int p){
if(val[now]) return 1;
if(l == r) return val[now];
pushdown(now);
int mid = (l + r) >> 1;
if(p <= mid) return Query(lc, l, mid, p);
return Query(rc, mid + 1, r, p);
}
int size[MAXN], son[MAXN], dep[MAXN], top[MAXN], pos[MAXN], ID, f[MAXN];
void dfs1(int u, int fa){
dep[u] = dep[f[u] = fa] + 1;
size[u] = 1;
for(int i = head[u]; i; i = e[i].next)
if(e[i].to != fa){
dfs1(e[i].to, u);
size[u] += size[e[i].to];
if(size[e[i].to] > size[son[u]])
son[u] = e[i].to;
}
}
void dfs2(int u, int tp){
top[u] = tp; pos[u] = ++ID;
if(son[u]) dfs2(son[u], tp);
for(int i = head[u]; i; i = e[i].next)
if(e[i].to != son[u] && e[i].to != f[u])
dfs2(e[i].to, e[i].to);
}
void root(int u){
while(u){
Update(1, 1, n, pos[top[u]], pos[u], 0);
u = f[top[u]];
}
}
void child(int u){
Update(1, 1, n, pos[u], pos[u] + size[u] - 1, 1);
}
int a, b;
int main(){
n = read();
for(int i = 1; i < n; ++i) Add(read(), read());
dfs1(1, 0); dfs2(1, 1);
m = read();
while(m--){
a = read(); b = read();
if(a == 1)
child(b);
if(a == 2)
root(b);
if(a == 3)
printf("%d\n", Query(1, 1, n, pos[b]));
}
return 0;
}
【CF343D】 Water Tree(树链剖分)的更多相关文章
- CF343D Water Tree 树链剖分
问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...
- 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 ...
- 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-> ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- 课堂学习Scrum站立会议
项目名称:连连看游戏 小组成员:张政,张金生,李权,武志远 Master:张政 站立会议内容 1.已完成的内容 windows下的基本用户页面,实现了多个BUTTON下7*12的页面布局,但是出现了b ...
- (五)hadoop系列之__集群搭建SSH无密访问多台机器
免密码ssh设置 现在确认能否不输入口令就用ssh登录localhost: $ ssh localhost 如果不输入口令就无法用ssh登陆localhost,执行下面的命令: . 并修改hosts映 ...
- 更新 pip & setuptools
python -m pip install -U pip setuptools
- 【C++】C++函数重载的总结
函数重载: 出现在相同作用域中的两个函数,如果具有相同的名字而形参表不同,则称为重载函数(overloaded function).一定要注意函数重载的两个关键词:形参列表和作用域. 任何程序有且仅有 ...
- 包装类 integer 当做 list的参数时候 会出现无法删除成功的现象
- Git Gerrit Repo User Manual
Git Repo Gerrit User Manual Revision History Revision # Description Date Author ...
- 【开发工具IDE】Eclipse相关配置
1. 修改workspace编码为UTF-8 1.1. 修改jsp编码为UTF-8 2. 修改字体 3. 添加系统中的JDK 4. 导入formatter模板 5. 修改maven配置文件 打开文件: ...
- TortoiseSVN 和 VisualSVN Server 使用教程
TortoiseSVN 和 VisualSVN Server 使用教程 来源 https://blog.csdn.net/xgf415/article/details/75196360 目录: SVN ...
- [NOI2016]优秀的拆分 后缀数组
题面:洛谷 题解: 因为对于原串的每个长度不一定等于len的拆分而言,如果合法,它将只会被对应的子串统计贡献. 所以子串这个限制相当于是没有的. 所以我们只需要对于每个位置i求出f[i]表示以i为开头 ...
- [APIO2014]回文串 manacher 后缀数组
题面:洛谷 题解: 还是这个性质:对于每个串而言,本质不同的回文串最多只有O(n)个. 所以我们先求出这O(n)个本质不同的回文串,然后对整个串求一次sa. 然后对于每个回文串,求出它的出现次数,更新 ...