P3703 [SDOI2017]树点涂色
P3703 [SDOI2017]树点涂色
分析:
首先对于询问,感觉是线段树维护dfs序,每个点记录到根的颜色个数。第二问差分,第三问区间取max。
那么考虑修改,每次将一个点的颜色变成和父节点的颜色一样的过程中,这个点的子树内都会-1。
这个修改的过程我们可以认为是修改边的过程,将一些边设为1,一些边设为0,那么一次修改对于一个点就是将原来1的边设为0,现在的边设为1。
1和0类似lct中实边与虚边,所以可以lct维护当前那些边是1,那些是0。
感觉跟个暴力似的,但是lct中access的操作是log的,所以修改的复杂度是log的,线段树中再有一个log,总复杂度是$O(nlog^2)$
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , Log = ;
struct Edge{ int to, nxt; } e[N << ];
int head[N], f[N][], siz[N], pos[N], deth[N], Index, En, n, m; inline void add_edge(int u,int v) {
++En; e[En].to = v, e[En].nxt = head[u]; head[u] = En;
++En; e[En].to = u, e[En].nxt = head[v]; head[v] = En;
}
void dfs(int u) {
pos[u] = ++Index; siz[u] = ; deth[u] = deth[f[u][]] + ;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == f[u][]) continue;
f[v][] = u;
dfs(v);
siz[u] += siz[v];
}
}
int LCA(int u,int v) {
if (deth[u] < deth[v]) swap(u, v);
int d = deth[u] - deth[v];
for (int j = Log; ~j; --j)
if ((d >> j) & ) u = f[u][j];
if (u == v) return u;
for (int j = Log; ~j; --j)
if (f[u][j] != f[v][j]) u = f[u][j], v = f[v][j];
return f[u][];
}
struct SegmentTree{
int mx[N << ], tag[N << ];
inline void pushup(int rt) { mx[rt] = max(mx[rt << ], mx[rt << | ]); }
inline void pushdown(int rt) {
mx[rt << ] += tag[rt]; mx[rt << | ] += tag[rt];
tag[rt << ] += tag[rt]; tag[rt << | ] += tag[rt];
tag[rt] = ;
}
void update(int l,int r,int rt,int L,int R,int v) {
if (L <= l && r <= R) { tag[rt] += v; mx[rt] += v; return ; }
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (L <= mid) update(lson, L, R, v);
if (R > mid) update(rson, L, R, v);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return mx[rt];
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (R <= mid) return query(lson, L, R);
else if (L > mid) return query(rson, L, R);
else return max(query(lson, L, R), query(rson, L, R));
}
void pr(int l,int r,int rt) {
if (l == r) { cout << mx[rt] << " "; return ; }
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
pr(lson); pr(rson);
}
}T;
struct LCT{
int fa[N], ch[N][];
inline bool isroot(int x) { return ch[fa[x]][] != x && ch[fa[x]][] != x; }
inline int son(int x) { return ch[fa[x]][] == x; }
inline void rotate(int x) {
int y = fa[x], z = fa[y], c = son(y), b = son(x), a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x; fa[x] = z;
ch[x][!b] = y; fa[y] = x;
ch[y][b] = a; if (a) fa[a] = y;
}
void splay(int x) {
while (!isroot(x)) {
int y = fa[x];
if (isroot(y)) rotate(x);
else {
if (son(x) == son(y)) rotate(y), rotate(x);
else rotate(x), rotate(x);
}
}
}
int find(int x) {
while (ch[x][]) x = ch[x][];
return x;
}
void access(int x) {
for (int last = , t; x; last = x, x = fa[x]) {
splay(x);
if (ch[x][]) t = find(ch[x][]), T.update(Root, pos[t], pos[t] + siz[t] - , ); // 这里找到原树上的位置
ch[x][] = last;
if (ch[x][]) t = find(ch[x][]), T.update(Root, pos[t], pos[t] + siz[t] - , -);
}
}
}lct;
int main() {
n = read(), m = read();
for (int i = ; i < n; ++i) {
int u = read(), v = read();
add_edge(u, v);
}
dfs();
for (int j = ; j <= Log; ++j)
for (int i = ; i <= n; ++i) f[i][j] = f[f[i][j - ]][j - ];
for (int i = ; i <= n; ++i) {
T.update(Root, pos[i], pos[i], deth[i]);
lct.fa[i] = f[i][];
}
while (m --) {
int opt = read(), x = read();
if (opt == ) lct.access(x);
else if (opt == ) {
int y = read(), z = LCA(x, y);
printf("%d\n", T.query(Root, pos[x], pos[x]) + T.query(Root, pos[y], pos[y]) - T.query(Root, pos[z], pos[z]) * + );
}
else {
printf("%d\n", T.query(Root, pos[x], pos[x] + siz[x] - ));
}
}
return ;
}
P3703 [SDOI2017]树点涂色的更多相关文章
- P3703 [SDOI2017]树点涂色 LCT维护颜色+线段树维护dfs序+倍增LCA
\(\color{#0066ff}{ 题目描述 }\) Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点 ...
- Luogu P3703 [SDOI2017]树点涂色
比较有趣的综合树上问题,刷LCT题单时做的但是发现后面LCT只是起了辅助作用233 首先我们分析每一个操作,\(1\)的定义就让我们联想到了access,我们回忆一下LCT的性质: LCT中每一个sp ...
- 洛谷P3703 [SDOI2017]树点涂色(LCT,dfn序,线段树,倍增LCA)
洛谷题目传送门 闲话 这是所有LCT题目中的一个异类. 之所以认为是LCT题目,是因为本题思路的瓶颈就在于如何去维护同颜色的点的集合. 只不过做着做着,感觉后来的思路(dfn序,线段树,LCA)似乎要 ...
- 并不对劲的bzoj4817:loj2001:p3703:[SDOI2017]树点涂色
题目大意 有一棵\(n\)(\(n\leq10^5\))个节点的树,每个点有颜色\(c\),一开始所有颜色互不相同 要进行\(m\)(\(m\leq10^5\))次操作,每次操作是以下三种中的一种: ...
- [Sdoi2017]树点涂色 [lct 线段树]
[Sdoi2017]树点涂色 题意:一棵有根树,支持x到根染成新颜色,求x到y颜色数,求x子树里点到根颜色数最大值 考场发现这个信息是可减的,但是没想到lct 特意设计成lct的形式! 如何求颜色数? ...
- 【LG3703】[SDOI2017]树点涂色
[LG3703][SDOI2017]树点涂色 题面 洛谷 题解 更博辣,更博辣!!! 猪年的第一篇博客 一次只能染根到\(x\),且染的颜色未出现过 这句话是我们解题的关键. 设\(x\)到根的颜色数 ...
- [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 692 Solved: 408[Submit][Status ...
- 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树
[BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...
- [Bzoj4817] [Sdoi2017]树点涂色 (LCT神题)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 629 Solved: 371[Submit][Status ...
随机推荐
- Orcal数据库,使用EF的自增处理
1.定义EF拦截器,截获执行命令前的操作.修改执行sql.还需要定义orcal序列,供自增使用 using System; using System.Collections.Generic; usin ...
- python面试十题
问题1: 请问如何修改以下python代码,使得下面的代码调用类A的show方法? class A(): def show(self): print("base show") cl ...
- JDBC 连接mysql获取中文时的乱码问题
前段时间学习JDBC,要连接mysql获取数据.按照老师的样例数据,要存一些名字之类的信息,用的都是英文名,我当时就不太想用英文,就把我室友的名字存了进去,嘿嘿,结果,出问题了. 连接数据库语句: s ...
- 我遇到的问题:耗时久/效率低 ---> 应对方案: 行动-结果指向
这一篇打的时候,时间都挺靠后的了, 当时出现错误,很慌了,一个是时间比较久,5点多了,一个是陈果已经做了很多题了,这些是事实. 导致我慌张的原因,简单来说是比较,长久以来,我都爱去和别人比较.如果赢了 ...
- 个人技术博客二之apk反编译与加密
根据原文郭霖大神的博客Android安全攻防战,反编译与混淆技术完全解析 本人亲测反编译真的没有什么卵用,个人纯属好奇就去搜了一下,偷窃有罪,抄袭可耻. 1.手机上的apk都是打包好的,直接安装使用. ...
- CSS盒子模型之CSS3可伸缩框属性(Flexible Box)
CSS盒子模型(下) 一.CSS3可伸缩框(Flexible Box) 可伸缩框属性(Flexible Box)是css3新添加的盒子模型属性,有人称之为弹性盒模型,它的出现打破了我们经常使用的浮动布 ...
- memcached迁移方案——记一次memcached session服务的迁移
背景: (1)由于机房调整,需要迁移memcached: (2)需要在短期内迁移完成(一周以内): (3)该memcached 保存了用户的登录数据,非常重要,一旦出问题将导致大量的用户被踢出: (4 ...
- 学记笔记 $\times$ 巩固 · 期望泛做$Junior$
最近泛做了期望的相关题目,大概\(Luogu\)上提供的比较简单的题都做了吧\(233\) 好吧其实是好几天之前做的了,不过因为太颓废一直没有整理-- \(Task1\) 期望的定义 在概率论和统计学 ...
- 1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)
1553: Good subsequence Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Subm ...
- 【vue】npm run mock & npm run dev 无法同时运行的解决
[关于系统,没注明的都是windows系统,若以后用的是mac系统则会另外备注] 当项目数据是通过mock搭建而成(参照:[vue]本地开发mock数据支持)时,运行mock服务器和项目的命令 就参照 ...