题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4817

https://loj.ac/problem/2001

题解

可以发现这个题就是 bzoj3779 重组病毒 的弱化版。

可以这样考虑。对于每一次染色操作,都是把 \(x\) 点到根的路径上的点全部染成一种颜色。

我们考虑用一个东西来记录下来同色的点,可以发现这个操作和 LCT 的 access 操作很像。如果用 LCT 来维护的话,那么就是一个 splay 记录一堆同色的点。

然后 access 的断掉重儿子的时候相当于是在重儿子里面集体加 \(1\),连上新的重儿子就是集体 \(-1\)。这个可以用线段树维护。

然后第二个操作可以树上差分,那么就是 \(f_x + f_y - 2f_{lca} + 1\)。


因为 LCT 的性质,时间复杂度 \(O(m\log^2n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 100000 + 7; int n, m, dfc;
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N]; struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); } namespace SGT {
#define lc o << 1
#define rc o << 1 | 1
struct Node { int add, max; } t[N << 2];
inline void build(int o, int L, int R) {
t[o].add = 0;
if (L == R) return t[o].max = dep[pre[L]], (void)0;
int M = (L + R) >> 1;
build(lc, L, M), build(rc, M + 1, R);
t[o].max = std::max(t[lc].max, t[rc].max);
}
inline void qadd(int o, int L, int R, int l, int r, int k) {
if (l <= L && R <= r) return t[o].add += k, t[o].max += k, (void)0;
int M = (L + R) >> 1;
if (l <= M) qadd(lc, L, M, l, r, k);
if (r > M) qadd(rc, M + 1, R, l, r, k);
t[o].max = std::max(t[lc].max, t[rc].max) + t[o].add;
}
inline int qmax(int o, int L, int R, int l, int r) {
if (l <= L && R <= r) return t[o].max;
int M = (L + R) >> 1;
if (r <= M) return qmax(lc, L, M, l, r) + t[o].add;
if (l > M) return qmax(rc, M + 1, R, l, r) + t[o].add;
return std::max(qmax(lc, L, M, l, r), qmax(rc, M + 1, R, l, r)) + t[o].add;
}
#undef lc
#undef rc
} namespace LCT {
#define lc c[0]
#define rc c[1]
struct Node { int c[2], fa; } t[N];
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
}
inline void splay(int o) {
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa) {
splay(o);
if (t[o].rc) {
int p = t[o].rc;
while (t[p].lc) p = t[p].lc;
SGT::qadd(1, 1, n, dfn[p], dfn[p] + siz[p] - 1, 1);
splay(p), splay(o);
}
t[o].rc = x;
if (t[o].rc) {
int p = t[o].rc;
while (t[p].lc) p = t[p].lc;
SGT::qadd(1, 1, n, dfn[p], dfn[p] + siz[p] - 1, -1);
splay(p), splay(o);
}
}
}
#undef lc
#undef rc
} inline void dfs1(int x, int fa = 0) {
dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1, LCT::t[x].fa = fa;
for fec(i, x, y) if (y != fa) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
top[x] = pa, dfn[x] = ++dfc, pre[dfc] = x;
if (!son[x]) return; dfs2(son[x], pa);
for fec(i, x, y) if (y != f[x] && y != son[x]) dfs2(y, y);
}
inline int lca(int x, int y) {
while (top[x] != top[y]) dep[top[x]] > dep[top[y]] ? x = f[top[x]] : y = f[top[y]];
return dep[x] < dep[y] ? x : y;
} inline void work() {
dfs1(1), dfs2(1, 1);
SGT::build(1, 1, n);
while (m--) {
int opt, x, y, p;
read(opt);
if (opt == 1) read(x), LCT::access(x);
else if (opt == 2) read(x), read(y), p = lca(x, y), printf("%d\n", SGT::qmax(1, 1, n, dfn[x], dfn[x]) + SGT::qmax(1, 1, n, dfn[y], dfn[y]) - (SGT::qmax(1, 1, n, dfn[p], dfn[p]) << 1) + 1);
else read(x), printf("%d\n", SGT::qmax(1, 1, n, dfn[x], dfn[x] + siz[x] - 1));
}
} inline void init() {
read(n), read(m);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树的更多相关文章

  1. 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树

    [BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...

  2. 【BZOJ4817】【SDOI2017】树点涂色 [LCT][线段树]

    树点涂色 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Bob有一棵n个点的有根树,其中1 ...

  3. [Sdoi2017]树点涂色 [lct 线段树]

    [Sdoi2017]树点涂色 题意:一棵有根树,支持x到根染成新颜色,求x到y颜色数,求x子树里点到根颜色数最大值 考场发现这个信息是可减的,但是没想到lct 特意设计成lct的形式! 如何求颜色数? ...

  4. [SDOI2017][bzoj4817] 树点涂色 [LCT+线段树]

    题面 传送门 思路 $LCT$ 我们发现,这个1操作,好像非常像$LCT$里面的$Access$啊~ 那么我们尝试把$Access$操作魔改成本题中的涂色 我们令$LCT$中的每一个$splay$链代 ...

  5. BZOJ4817[Sdoi2017]树点涂色——LCT+线段树

    题目描述 Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色.Bob可能会进 ...

  6. BZOJ 4817 [SDOI2017]树点涂色 (LCT+线段树维护dfs序)

    题目大意:略 涂色方式明显符合$LCT$里$access$操作的性质,相同颜色的节点在一条深度递增的链上 用$LCT$维护一个树上集合就好 因为它维护了树上集合,所以它别的啥都干不了了 发现树是静态的 ...

  7. 【bzoj4817】树点涂色 LCT+线段树+dfs序

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  8. BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树

    同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...

  9. BZOJ 4817: [Sdoi2017]树点涂色(lct+线段树)

    传送门 解题思路 跟重组病毒这道题很像.只是有了一个询问\(2\)的操作,然后询问\(2\)的答案其实就是\(val[x]+val[y]-2*val[lca(x,y)]+1\)(画图理解).剩下的操作 ...

随机推荐

  1. 如何获得select被选中option的value和text和其他属性值

    比如这个: <select id="select"> <option value="A" url="http://www.baidu ...

  2. pyinstaller-python->exe

    pip install pyinstaller. pyinstaller -F /home/base64_decode.py https://www.imooc.com/article/26772 h ...

  3. Application.CreateForm()和TForm.Create()创建的窗体有什么区别么?二者在使用上各有什么技巧?(50分)

    https://wedelphi.com/t/135849/ 请详细些,并给出例子.谢谢. Application.CreateForm()创建的第一个可显示的窗体是自动成为主窗体,并且自动显示,并且 ...

  4. 应用安全 - CMS - PHPCMS漏洞汇总

    CVE-2011-0644 Date: 2011.1 类型: /flash_upload.php SQL注入 影响版本:phpCMS 2008 V2 PHPCMS PHPCMS通杀XSS 在我要报错功 ...

  5. finereport点击图表钻取到明细表包括参数传递

    1.  点击编辑图表 2.  参数传递 3.  选择分类名称 4.  钻取明细表获取 inputs 值得方法 使用公司 $inputs   获取钻取传来的值

  6. @Scheduled(cron = "0/5 * * * * *")将时间改为配置

    有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置. 第二种在你的类前面添加 此处讲解第二种写法 第二种在你的类前面添加@PropertySource("classpa ...

  7. 最全的DevOps自动化工具集合

    版本控制&协作开发:GitHub.GitLab.BitBucket.SubVersion.Coding.Bazaar 自动化构建和测试:Apache Ant.Maven .Selenium.P ...

  8. 【C语言--数据结构】线性顺序表

    线性表的本质: 1.线性表(List)是零个或者多个数据元素的集合: 2.线性表中的数据元素之间是有顺序的: 3.线性表中的数据元素个数是有限的: 4.线性表中的数据元素的类型必须相同: 定义: 线性 ...

  9. Widget代码讲解

    参考:https://zhuanlan.zhihu.com/p/28225011 QT版本为5.12.4 1.main.cpp #include "widget.h" #inclu ...

  10. Python 入门之 Python三大器 之 迭代器

    Python 入门之 Python三大器 之 迭代器 1.迭代器 (1)可迭代对象: <1> 只要具有__ iter __()方法就是一个可迭代对象 (我们可以通过dir()方法去判断一个 ...