题目链接

树剖傻逼题,练练手好久没写树剖了。

查询忘记\(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(树链剖分)的更多相关文章

  1. CF343D Water Tree 树链剖分

    问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...

  2. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  3. 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 ...

  4. Water Tree(树链剖分+dfs时间戳)

    Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...

  5. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  6. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  7. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  8. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  9. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  10. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

随机推荐

  1. pcap的安装与配置

    1.打开网址:www.tcpdump.org/ 下载 libpcap-1.0.0.tar.gz (512.0KB) 软件包,通过命令 tar zxvf libpcap-1.0.0.tar.gz 解压文 ...

  2. SQL Server bit数据类型

    bit值保存为1/0,1代表true,0代表false读取数据库数据时,可以直接用bool型读取该字段,会直接转换为true/false 数据库表结构 CREATE TABLE [dbo].[BitT ...

  3. 转发---[沧海拾遗]java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch CountDownLatch是一个计 ...

  4. DjangoORM字段参数介绍

    参数介绍: 字段的参数:     null:             ->db是否可以为空     default:          ->默认值     primary_key:     ...

  5. 【转】NHibernate 各种数据库配置

    转载自:http://terrylee.cnblogs.com/archive/2006/04/05/367381.html 自己只用了Oracle 可用! 三.常见的配置示例 Castle网站为我们 ...

  6. VK Cup 2017 Round 3 + Codeforces Round #412

    A 读题题 B 就是你排名第p,得了x分,而最终至少需要y分,你需要最少的successful hack,使得最终得分s>=y,且由s随机取25个数,使p被选中. (1)暴力枚举hack成功几次 ...

  7. 洛谷5月月赛T30212 玩游戏 【分治NTT + 多项式求ln】

    题目链接 洛谷T30212 题解 式子很容易推出来,二项式定理展开后对于\(k\)的答案即可化简为如下: \[k!(\sum\limits_{i = 0}^{k} \frac{\sum\limits_ ...

  8. flex的使用实例

    之前的随笔从阮一峰老师那里学到了flex的基本用法及作用,现在来把flex具体运用到实例中,看看flex的弹性布局效果. 1.  flex设置元素垂直居中对齐 在之前的一篇文章中记载过如何垂直居中对齐 ...

  9. python--生成器协程运算

    生成器 一.yield运行方式 我们定义一个如下的生成器: def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) ...

  10. Mac下安装SVN插件javaHL not available的解决方法

    在Mac下安装Eclipse插件svnEclipse插件后,每次打开Eclipse都会弹出如下弹出框: 提示你本机缺少JavaHL Library. 选择Eclipse→偏好设置(preference ...