BZOJ 4817 [LCT][线段树][树链剖分]
来源:互联网 发布:ug电极自动编程 编辑:程序博客网 时间:2024/06/14 04:17
Description
Bob有一棵
定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色。
Bob可能会进行这几种操作:
1 x :把点x 到根节点的路径上所有的点染上一种没有用过的新颜色。2 x y :求x到y的路径的权值。3 x :在以x为根的子树中选择一个点,使得这个点到根节点的路径权值最大,求最大权值。
Bob一共会进行
Solution
后面两个操作维护
第一个操作好像就是
虚边相连的两个点颜色不同,实边相连的两个点颜色相同。这样的话每次
#include <cstdio>#include <cstdlib>#include <iostream>using namespace std;const int N = 101010;inline char get(void) { static char buf[100000], *S = buf, *T = buf; if (S == T) { T = (S = buf) + fread(buf, 1, 100000, stdin); if (S == T) return EOF; } return *S++;}inline void read(int &x) { static char c; x = 0; for (c = get(); c < '0' || c > '9'; c = get()); for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';}template<typename T>inline T Max(T a, T b) { return a < b ? b : a;}int n, m, Gcnt, x, y, z, clc, opt;struct edge { int to, next; edge (int t = 0, int n = 0):to(t), next(n) {}};edge G[N << 1];int head[N];namespace Tree { int pre[N], post[N], son[N], size[N], dep[N], top[N], fa[N]; inline void dfs1(int u) { int to; size[u] = 1; for (int i = head[u]; i; i = G[i].next) { to = G[i].to; if (to == fa[u]) continue; dep[to] = dep[u] + 1; fa[to] = u; dfs1(to); size[u] += size[to]; if (size[son[u]] < size[to]) son[u] = to; } } inline void dfs2(int u, int t) { top[u] = t; pre[u] = ++clc; if (son[u]) dfs2(son[u], t); for (int i = head[u]; i; i = G[i].next) if (G[i].to != son[u] && G[i].to != fa[u]) dfs2(G[i].to, G[i].to); post[u] = clc; } inline int LCA(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] > dep[top[y]]) swap(x, y); y = fa[top[y]]; } return dep[x] < dep[y] ? x : y; }}namespace Seg { int mx[N << 2], add[N << 2]; inline void PushDown(int o) { if (add[o]) { mx[o << 1] += add[o]; mx[o << 1 | 1] += add[o]; add[o << 1] += add[o]; add[o << 1 | 1] += add[o]; add[o] = 0; } } inline void Add(int o, int l, int r, int L, int R, int x) { if (l >= L && r <= R) { add[o] += x; mx[o] += x; return; } int mid = (l + r) >> 1; PushDown(o); if (L <= mid) Add(o << 1, l, mid, L, R, x); if (R > mid) Add(o << 1 | 1, mid + 1, r, L, R, x); mx[o] = Max(mx[o << 1], mx[o << 1 | 1]); } inline int Query(int o, int l, int r, int L, int R) { if (l >= L && r <= R) return mx[o]; int mid = (l + r) >> 1, res = 0; PushDown(o); if (L <= mid) res = Max(res, Query(o << 1, l, mid, L, R)); if (R > mid) res = Max(res, Query(o << 1 | 1, mid + 1, r, L, R)); return res; } inline void Add(int u, int x) { Add(1, 1, n, Tree::pre[u], Tree::post[u], x); } inline int Query(int u) { return Query(1, 1, n, Tree::pre[u], Tree::post[u]); } inline int Dist(int u) { return Query(1, 1, n, Tree::pre[u], Tree::pre[u]); }}namespace LCT { struct node { node *ch[2]; node *fa; int id; }; node *null; node mem[N]; node *T[N]; inline void Init(int n) { for (int i = 0; i <= n; i++) T[i] = mem + i; null = T[0]; null->ch[0] = null->ch[1] = null; null->fa = null; for (int i = 1; i <= n; i++) { T[i]->ch[0] = T[i]->ch[1] = null; T[i]->fa = T[Tree::fa[i]]; T[i]->id = i; } } inline bool IsRoot(node *x) { return x->fa == null || (x->fa->ch[0] != x && x->fa->ch[1] != x); } inline void Rotate(node* &x) { node *y = x->fa, *z = y->fa; int l = (y->ch[0] != x), r = l ^ 1; if (!IsRoot(y) && z != null) { if (z->ch[0] == y) z->ch[0] = x; else z->ch[1] = x; } x->fa = z; y->fa = x; x->ch[r]->fa = y; y->ch[l] = x->ch[r]; x->ch[r] = y; } inline void Splay(node* &x) { while (!IsRoot(x)) { node *y = x->fa, *z = y->fa; if (!IsRoot(y)) { if (y->ch[0] == x ^ z->ch[0] == y) Rotate(x); else Rotate(y); } Rotate(x); } } inline void Access(node* x) { node *t, *y; for (y = null; x != null; x = x->fa) { Splay(x); for (t = x->ch[1]; t->ch[0] != null; t = t->ch[0]); if (t != null) Seg::Add(t->id, 1); for (t = y; t->ch[0] != null; t = t->ch[0]); if (t != null) Seg::Add(t->id, -1); x->ch[1] = y; y = x; } }}inline void AddEdge(int from, int to) { G[++Gcnt] = edge(to, head[from]); head[from] = Gcnt; G[++Gcnt] = edge(from, head[to]); head[to] = Gcnt;}int main(void) { read(n); read(m); for (int i = 1; i < n; i++) { read(x); read(y); AddEdge(x, y); } Tree::dfs1(1); Tree::dfs2(1, 1); LCT::Init(n); for (int i = 1; i <= n; i++) Seg::Add(Tree::pre[i], 1); while (m--) { read(opt); read(x); if (opt == 1) { LCT::Access(LCT::T[x]); } else if (opt == 2) { read(y); z = Tree::LCA(x, y); printf("%d\n", Seg::Dist(x) + Seg::Dist(y) - 2 * Seg::Dist(z) + 1); } else { printf("%d\n", Seg::Query(x)); } } return 0;}
阅读全文
2 0
- BZOJ 4817 [LCT][线段树][树链剖分]
- BZOJ 3779 重组病毒 LCT+线段树维护DFS序
- [LCT 线段树 dfs序] BZOJ 3779 重组病毒
- [LCT] BZOJ 4817 [Sdoi2017]树点涂色
- smoj2017(树链剖分+线段树/LCT)
- bzoj 3514: Codechef MARCH14 GERALD07加强版 lct+可持久化线段树
- BZOJ 3514 [LCT][主席树]
- BZOJ 1036 三种做法 [树链剖分][LCT][块状树]
- bzoj 1984(线段树+树链剖分)
- BZOJ 4034 树链剖分 + 线段树
- BZOJ 2836 树链剖分+线段树
- BZOJ 2243 树链剖分+线段树
- BZOJ 2594 LCT维护最小生成树
- [动态树 LCT] BZOJ 2157 旅游
- bzoj 3282 Tree 动态树LCT
- 【Codechef】【Gangsters of Treeland】Lct 线段树
- Sunshine’s city(lct+线段树)
- bzoj3779 lct+线段树+dfs序
- 业务逻辑
- SourceTree使用介绍
- RXJava详解
- ios-Swift处理异常的三种方式
- 星际争霸:登录韩服等其他国家服务器
- BZOJ 4817 [LCT][线段树][树链剖分]
- [BZOJ1006]神奇的国度(完美消除序列)
- [NLP]论文笔记-A SIMPLE BUT TOUGH-TO-BEAT BASELINE FOR SENTENCE EMBEDDINGS
- 自定义滚动条
- UVA
- 包装流和转换流
- nyoj201 作业题(动归+最长单调序列)
- POJ 2653 Pick-up sticks(判断线段相交)
- Java基础--标识符、变量、数据类型、常量