【bzoj4817】[Sdoi2017]树点涂色 LCT+LCA+线段树
题目描述
输入
输出
样例输入
5 6
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5
样例输出
3
4
2
2
题解
LCT+线段树
这不是和 bzoj3779重组病毒 一样的吗。。。还没有换根操作。。。
使用线段树维护DFS序中区间最大值,然后按照那道题的思路使用LCT即可解决1、3操作。
对于2操作,由$x$到根、$y$到根与$lca(x,y)$到根得到。具体答案为$f[x]+f[y]-2*f[lca]+1$。相当于$i$到根中有$f[i]-1$个颜色分界边,于是$x$到$y$中的颜色分界边数目即为$(f[x]-1)+(f[y]-1)-2*(f[lca]-1)$。所以颜色段数目为分界边数目+1=$f[x]+f[y]-2*f[lca]+1$。
时间复杂度$O(n\log^2 n)$
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
using namespace std;
int n , head[N] , to[N << 1] , next[N << 1] , cnt , pre[N][20] , deep[N] , log[N] , pos[N] , last[N] , tot , mx[N << 2] , tag[N << 2] , fa[N] , c[2][N];
inline void add(int x , int y)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
inline void pushup(int x)
{
mx[x] = max(mx[x << 1] , mx[x << 1 | 1]);
}
void pushdown(int x)
{
if(tag[x])
{
mx[x << 1] += tag[x] , mx[x << 1 | 1] += tag[x];
tag[x << 1] += tag[x] , tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
}
void update(int b , int e , int a , int l , int r , int x)
{
if(b <= l && r <= e)
{
mx[x] += a , tag[x] += a;
return;
}
pushdown(x);
int mid = (l + r) >> 1;
if(b <= mid) update(b , e , a , lson);
if(e > mid) update(b , e , a , rson);
pushup(x);
}
int query(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e) return mx[x];
pushdown(x);
int mid = (l + r) >> 1 , ans = 0;
if(b <= mid) ans = max(ans , query(b , e , lson));
if(e > mid) ans = max(ans , query(b , e , rson));
return ans;
}
void dfs(int x)
{
int i;
pos[x] = ++tot;
for(i = 1 ; (1 << i) <= deep[x] ; i ++ ) pre[x][i] = pre[pre[x][i - 1]][i - 1];
for(i = head[x] ; i ; i = next[i])
if(to[i] != pre[x][0])
pre[to[i]][0] = fa[to[i]] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]);
last[x] = tot , update(pos[x] , last[x] , 1 , 1 , n , 1);
}
inline int lca(int x , int y)
{
int i;
if(deep[x] < deep[y]) swap(x , y);
for(i = log[deep[x] - deep[y]] ; ~i ; i -- )
if(deep[x] - deep[y] >= (1 << i))
x = pre[x][i];
if(x == y) return x;
for(i = log[deep[x]] ; ~i ; i -- )
if(deep[x] >= (1 << i) && pre[x][i] != pre[y][i])
x = pre[x][i] , y = pre[y][i];
return pre[x][0];
}
inline bool isroot(int x)
{
return x != c[0][fa[x]] && x != c[1][fa[x]];
}
inline void rotate(int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(!isroot(y)) c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
}
inline void splay(int x)
{
int y , z;
while(!isroot(x))
{
y = fa[x] , z = fa[y];
if(!isroot(y))
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
inline void modify(int x , int a)
{
if(!x) return;
while(c[0][x]) x = c[0][x];
update(pos[x] , last[x] , a , 1 , n , 1);
}
inline void access(int x)
{
int t = 0;
while(x) splay(x) , modify(c[1][x] , 1) , c[1][x] = t , modify(t , -1) , t = x , x = fa[x];
}
int main()
{
int m , i , opt , x , y , z;
scanf("%d%d" , &n , &m);
for(i = 2 ; i <= n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x) , log[i] = log[i >> 1] + 1;
dfs(1);
while(m -- )
{
scanf("%d%d" , &opt , &x);
if(opt == 1) access(x);
else if(opt == 2)
{
scanf("%d" , &y) , z = lca(x , y);
printf("%d\n" , query(pos[x] , pos[x] , 1 , n , 1) + query(pos[y] , pos[y] , 1 , n , 1) - 2 * query(pos[z] , pos[z] , 1 , n , 1) + 1);
}
else printf("%d\n" , query(pos[x] , last[x] , 1 , n , 1));
}
return 0;
}
【bzoj4817】[Sdoi2017]树点涂色 LCT+LCA+线段树的更多相关文章
- 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
题目大意 Bob有一棵\(n\)个点的有根树,其中\(1\)号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜 ...
- LOJ2001 SDOI2017 树点涂色 LCT、线段树
传送门 注意到每一次\(1\ x\)操作相当于一次LCT中的access操作.由LCT复杂度证明可以知道access的总次数不会超过\(O(nlogn)\),我们只需要模拟这个access的过程并在其 ...
- 【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树
[BZOJ4817][Sdoi2017]树点涂色 Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路 ...
- [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 692 Solved: 408[Submit][Status ...
- 【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可能会进 ...
- P3703 [SDOI2017]树点涂色 LCT维护颜色+线段树维护dfs序+倍增LCA
\(\color{#0066ff}{ 题目描述 }\) Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点 ...
随机推荐
- Keepalived搭建主从架构、主主架构实例
实例拓扑图: DR1和DR2部署Keepalived和lvs作主从架构或主主架构,RS1和RS2部署nginx搭建web站点. 注意:各节点的时间需要同步(ntpdate ntp1.aliyun.co ...
- ECSHOP和SHOPEX快递单号查询EMS插件V8.6专版
发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...
- wamp环境下安装imagick扩展
先上图,如下是安装成功后的phpinfo()界面: 安装步骤: 1.先确定安装版本,比如我的的php : php7.0.12 x86 ts 那么就需要三方版本 要一致:imagick软件本身( 如x ...
- YII2.O学习三 前后台用户数据表分离
之前我们完成了Advanced 模板安装,也完成了安装adminlte 后台模板,这一步是针对前端和后台用户使用不同的数据库表来管理,做到前后台用户分离的效果: 复制一张user数据表并重命名为adm ...
- (杭电 1097)A hard puzzle
A hard puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- [Cracking the Coding Interview] 4.6 Successor 后继节点
Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary sear ...
- Java 基础------16进制转2进制
我们知道,数字8用二进制表示为:1000 用16进制表示为:8 那么我给你一个16进制的数字,0x7f,他的二进制是什么呢? 一个16进制的位数,用4位表示.比如,0x 7 f 其中: 7用4位二进制 ...
- abtest-system后台系统设计与搭建
本文来自网易云社区 作者:刘颂 1 项目背景: 2017年5月:客户端提出增加https&dns以及双cdn业务功能 后台配合实现使用disconf配置 针对不同的域名或者请求配置不同的htt ...
- FlaskWeb开发从入门到放弃(二)
第5章 章节五 01 内容概要 02 内容回顾 03 面向对象相关补充:metaclass(一) 04 面向对象相关补充:metaclass(二) 05 WTforms实例化流程分析(一) 06 WT ...
- 剑指offer-树的子结构17
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) class Solution: def issubTree(self,pRoot1,pRoot2) ...