【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树
【BZOJ4817】[Sdoi2017]树点涂色
Description
Input
Output
Sample Input
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5
Sample Output
4
2
2
题解:做过重组病毒那题再做这题就水了。
发现1操作可以看成LCT的access操作,而一个点到根路径的权值就是该点在LCT中到根路径上的虚边数量。所以我们用LCT模拟这个过程,在access的时候顺便改一下子树内的虚边数量即可。可以用线段树实现。
那么已知了一个点到根路径上的权值,如何求一条路径的权值呢?如果a,b的lca是c,那么自己画画就知道,答案是:a路径的权值+b路径的权值-2*c路径的权值+1。
#include <cstdio>
#include <cstring>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=100010; struct LCT
{
int ch[2],fa,L;
}s[maxn];
int n,m,cnt;
int fa[19][maxn],to[maxn<<1],next[maxn<<1],head[maxn],Log[maxn],dep[maxn],p[maxn],q[maxn],Q[maxn],sm[maxn<<2],ts[maxn<<2];
inline void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
p[x]=++q[0],Q[q[0]]=x;
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa[0][x]) fa[0][to[i]]=x,dep[to[i]]=dep[x]+1,dfs(to[i]);
q[x]=q[0];
}
inline int lca(int a,int b)
{
if(dep[a]<dep[b]) swap(a,b);
for(int i=Log[dep[a]-dep[b]];i>=0;i--) if(dep[fa[i][a]]>=dep[b]) a=fa[i][a];
if(a==b) return a;
for(int i=Log[dep[a]];i>=0;i--) if(fa[i][a]!=fa[i][b]) a=fa[i][a],b=fa[i][b];
return fa[0][a];
}
inline bool isr(int x) {return s[s[x].fa].ch[0]!=x&&s[s[x].fa].ch[1]!=x;}
inline void pushup(int x)
{
if(s[x].ch[0]) s[x].L=s[s[x].ch[0]].L;
else s[x].L=x;
}
inline void rotate(int x)
{
int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
s[x].ch[d^1]=y;
pushup(y),pushup(x);
}
inline void splay(int x)
{
while(!isr(x))
{
int y=s[x].fa,z=s[y].fa;
if(!isr(y))
{
if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
else rotate(y);
}
rotate(x);
}
}
inline void pushdown(int x)
{
if(ts[x]) sm[lson]+=ts[x],ts[lson]+=ts[x],sm[rson]+=ts[x],ts[rson]+=ts[x],ts[x]=0;
}
void build(int l,int r,int x)
{
if(l==r)
{
sm[x]=dep[Q[l]];
return ;
}
int mid=(l+r)>>1;
build(l,mid,lson),build(mid+1,r,rson);
sm[x]=max(sm[lson],sm[rson]);
}
void updata(int l,int r,int x,int a,int b,int val)
{
if(a<=l&&r<=b)
{
sm[x]+=val,ts[x]+=val;
return ;
}
pushdown(x);
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,val);
if(b>mid) updata(mid+1,r,rson,a,b,val);
sm[x]=max(sm[lson],sm[rson]);
}
int query(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return sm[x];
pushdown(x);
int mid=(l+r)>>1;
if(b<=mid) return query(l,mid,lson,a,b);
if(a>mid) return query(mid+1,r,rson,a,b);
return max(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
}
inline void sumup(int x,int val) {if(x) updata(1,n,1,p[x],q[x],val);}
inline void access(int x)
{
for(int y=0;x;)
{
splay(x);
sumup(s[s[x].ch[1]].L,1),sumup(s[y].L,-1),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
}
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd();
int i,j,a,b,c,op;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
dep[1]=1,dfs(1);
for(i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
for(j=1;(1<<j)<=n;j++) for(i=1;i<=n;i++) fa[j][i]=fa[j-1][fa[j-1][i]];
for(i=1;i<=n;i++) s[i].fa=fa[0][i],s[i].L=i;
build(1,n,1);
for(i=1;i<=m;i++)
{
op=rd(),a=rd();
if(op==1) access(a),splay(a);
if(op==2)
{
b=rd(),c=lca(a,b);
printf("%d\n",query(1,n,1,p[a],p[a])+query(1,n,1,p[b],p[b])-2*query(1,n,1,p[c],p[c])+1);
}
if(op==3) printf("%d\n",query(1,n,1,p[a],q[a]));
}
return 0;
}//5 3 1 2 2 3 3 4 3 5 1 4 1 5 2 4 5
【BZOJ4817】[Sdoi2017]树点涂色 LCT+线段树的更多相关文章
- 【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可能会进 ...
- bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4817 https://loj.ac/problem/2001 题解 可以发现这个题就是 bzo ...
- 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\)(画图理解).剩下的操作 ...
随机推荐
- ngrinder安装
1.源码编译和部署 官网:http://naver.github.io/ngrinder/ 下载源码后,存在部分依赖库不在maven的远程仓库中,这是可以用下载jar包后,用以下命令打包到本地仓库: ...
- Request.Cookies使用方法分析
本文章介绍了Request.Cookies的基本的语法和使用方法. 而且通过演示样例分析了Request.Cookies的使用过程. Request.Cookies方法能够检索Cookies 集合中的 ...
- LoadRunner测试AJAX
什么是AJAX? Ajax, shorthand for Asynchronous JavaScript and XML, is a web development technique for cre ...
- android 类似微信的摇一摇实现
一.在 AndroidManifest.xml 中添加操作权限 <uses-permission android:name="android.permission.VIBRATE&qu ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(三)—— 简单登录页面+sqlite+动软代码生成器的使用
上一节讲到利用easyui的layout.tree.tab和datagrid创建用户管理的页面,注意利用到easyui的页面一定要按顺序添加jQuery和easyUI的.js和.css样式,灵活查看e ...
- laravel框架查看执行过的sql语句
1.在routes.php中添加如下语句 Event::listen('illuminate.query', function($sql,$param) { file_put_contents ...
- 深入分析JavaWeb Item22 -- 国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同一时候应对世界不同地区和国家的訪问,并针对不同地区和国家的訪问.提供对应的.符合来訪者阅读习惯的页面或数据. 国际化(international ...
- Hive 练习 简单任务处理
1.2018年4月份的用户数.订单量.销量.GMV (不局限与这些统计量,你也可以自己想一些) -- -- -- 2018年4月份的用户数量 select count(a.user_id) as us ...
- memcached 命令行举例
1.启动Memcache 常用参数 memcached 1.4.3 -p <num> 设置端口号(默认不设置为: 11211) -U <num> UDP监听端口 (默 ...
- char device
/** * alloc_chrdev_region() - register a range of char device numbers * @dev: output parameter for f ...