bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4817
题解
可以发现这个题就是 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 + 线段树的更多相关文章
- 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树
[BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...
- 【BZOJ4817】【SDOI2017】树点涂色 [LCT][线段树]
树点涂色 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Bob有一棵n个点的有根树,其中1 ...
- [Sdoi2017]树点涂色 [lct 线段树]
[Sdoi2017]树点涂色 题意:一棵有根树,支持x到根染成新颜色,求x到y颜色数,求x子树里点到根颜色数最大值 考场发现这个信息是可减的,但是没想到lct 特意设计成lct的形式! 如何求颜色数? ...
- [SDOI2017][bzoj4817] 树点涂色 [LCT+线段树]
题面 传送门 思路 $LCT$ 我们发现,这个1操作,好像非常像$LCT$里面的$Access$啊~ 那么我们尝试把$Access$操作魔改成本题中的涂色 我们令$LCT$中的每一个$splay$链代 ...
- BZOJ4817[Sdoi2017]树点涂色——LCT+线段树
题目描述 Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色.Bob可能会进 ...
- BZOJ 4817 [SDOI2017]树点涂色 (LCT+线段树维护dfs序)
题目大意:略 涂色方式明显符合$LCT$里$access$操作的性质,相同颜色的节点在一条深度递增的链上 用$LCT$维护一个树上集合就好 因为它维护了树上集合,所以它别的啥都干不了了 发现树是静态的 ...
- 【bzoj4817】树点涂色 LCT+线段树+dfs序
Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...
- BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树
同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...
- BZOJ 4817: [Sdoi2017]树点涂色(lct+线段树)
传送门 解题思路 跟重组病毒这道题很像.只是有了一个询问\(2\)的操作,然后询问\(2\)的答案其实就是\(val[x]+val[y]-2*val[lca(x,y)]+1\)(画图理解).剩下的操作 ...
随机推荐
- IntelliJ IDEA2018破解教程
破解方法:下载破解补丁→修改配置文件→输入激活码→激活成功 由于JetBrains封杀,大部分激活服务器已经不能使用,使用下面的比较麻烦的方法也可以进行破解,但是有效期是到2100年(emmmm,也算 ...
- Python 使用 PyQt5 开发的关机小工具
前两天简单认识了一下PyQt5,通过练习开发了一款在Window下自定义关机的小工具,代码如下 import os,sys,time from PyQt5 import QtCore,QtWidget ...
- 浏览器默认样式及reset
写在前面 首先纠正一个易错概念.div并非生来就是块元素,而是每个浏览器都有一套默认的css样式(优先级最低),默认样式里会把div设置成display: block;还有margin,padding ...
- java数字加密算法
数字加密在项目中时常会遇到,如手机号,身份证号信息等,下面小白将自己手写的数字加密算法分享给大家,可在项目中直接运用.加密规则,入参时传递一个字段时间戳 time:* 1.以字母代替数字,0-9分别为 ...
- Spring mvc注解说明
编号 注解 说明 位置 备注 1 @Controller 将类变成Spring Bean 类 现阶段 @Controller . @Service 以及 @Repository 和 @Componen ...
- lazarus 给应用程序创建 配置文件哈哈
lazarus 给应用程序创建 配置文件哈哈procedure TForm1.Button2Click(Sender: TObject);beginForceDirectoriesUTF8(GetAp ...
- linux--vm安装
网络排错图解 https://www.linuxidc.com/Linux/2017-03/141863.htm net模式 https://www.linuxidc.com/Linux/2017-0 ...
- [19/10/16-星期三] Python中的模块和包、异常、操作文件
一.模块 # 模块(module) # 模块化,模块化指将一个完整的程序分解为一个一个小的模块 # 通过将模块组合,来搭建出一个完整的程序 # 不采用模块化,统一将所有的代码编写到一个文件中 # 采用 ...
- 【烦人的字符集】linux字符集问题,中文乱码
[1]快速修改命令 [2]locale 查看现在服务器的字符 [root@Master ~]# localeLANG=en_US.UTF-8LC_CTYPE="zh_CN.UTF-8&quo ...
- uva-796.critical links(连通图的桥)
本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...