博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CF343D】 Water Tree(树链剖分)
阅读量:4610 次
发布时间:2019-06-09

本文共 2611 字,大约阅读时间需要 8 分钟。

树剖傻逼题,练练手好久没写树剖了。
查询忘记\(pushdown\)抓了好久虫。。
全文手写,一遍过。。。

#include 
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;}

转载于:https://www.cnblogs.com/Qihoo360/p/10581083.html

你可能感兴趣的文章
java23种设计模式
查看>>
冲刺周期一--站立会议04
查看>>
支持IE6以上阴影效果纯CSS
查看>>
优化算法与特征缩放
查看>>
NOIP模板复习(4)区间操作之莫队算法,树状数组,线段树
查看>>
git warning: LF will be replaced by CRLF in 解决办法
查看>>
浅谈MVP设计模式
查看>>
深入理解PHP中的引用和赋值
查看>>
红黑树
查看>>
(转载)maven profile多环境自动切换配置
查看>>
py三个面试小问题
查看>>
图像类推效果图
查看>>
php pdo_mysql使用方法
查看>>
Android驱动开发第二章随想
查看>>
String API
查看>>
O(1)纬度减少循环次数
查看>>
绑定域名到 GitHub Pages
查看>>
javaweb-简单的验证码和算术验证码
查看>>
深入理解Javascript系列之类型
查看>>
DateTime数据类型保存问题(DateTime2)
查看>>