【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在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点 ...
随机推荐
- GIt+jenkins代码自动上线
代码自动上线功能 企业部署代码上线是件比较麻烦的事情,还好我们有jenkins这个持续集成的软件可以帮助我们做很多的事情,现在我们就 来测试用jenkins推送代码上线. 我们这里测试的是一个html ...
- flex布局设置min-width
在一个flex布局中,对于一个设置了flex属性设置为1的div容器,再对其设置min-width:0,保证内容不超出外层容器
- Mina 组件介绍之 IoAcceptor 与 IoConnector
在网络通信中,Socket通信的双方分为服务端与客户端,在Java NIO 的实现中采用Socket/ServerSocket, SocketChannel/ServerSocketChannel分别 ...
- spring-运行时值注入
在项目中经常使用连接数据库的配置,如下所示 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDa ...
- 11个简单实用技巧--Java性能调优
多数开发人员认为性能优化是个比较复杂的问题,需要大量的经验和知识.是的,这并不没有错.诚然,优化应用程序以获得最好的性能并不是一件容易的事情,但这并不意味着你在没有获得这些经验和知识之前就不能做任何事 ...
- xss挑战赛小记 0x01(xsstest)
0x00 今天在先知社区看到了一个xss挑战赛 结果发现比赛已经结束 服务器也关了 百度找了个xss挑战赛来玩一下 正好印证下xss的学习--- 地址 http://test.xss.tv/ ...
- 【python3.X】python学习中排雷过程^_^
问题一:python读取文件时报错:“UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 52: illegal mu ...
- 史上最全的PHP正则表达式
首先看下正则表达式思维导图: 一.校验数字的表达式 1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头 ...
- Get Error when restoring database in Sql Server 2008 R2
When I restored a database I got an error: "The backup set holds a backup of a database ot ...
- 【目录】Spring 源码学习
[目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...