【bz2002】弹飞绵羊
题意:
给出n个节点 及其父亲 和m个指令1:表示求节点i到根节点(n+1)的距离2:表示将节点i的父亲更换为j
题解:
动态树link、cut、access模板题 貌似没什么难度- -
代码:
#include <cstdio>
struct info{
int fat,root,lc,rc,num;
info(const int a=,const int b=,const int c=,const int d=,const int e=):
fat(a),root(b),lc(c),rc(d),num(e){}
};
int n,m;
info tree[];
void clean(){
tree[]=info(,,,,);
}
void makenum(int t){
tree[t].num=+tree[tree[t].lc].num+tree[tree[t].rc].num;
}
void left(int t){
int fa=tree[t].fat,r=tree[t].rc;
tree[t].rc=tree[r].lc;
tree[tree[r].lc].fat=t;
tree[r].lc=t;
tree[t].fat=r;
if (tree[t].root){
tree[t].root=;
tree[r].root=;
}else if (tree[fa].lc==t) tree[fa].lc=r;
else tree[fa].rc=r;
tree[r].fat=fa;
clean();
makenum(t);
makenum(r);
}
void right(int t){
int fa=tree[t].fat,l=tree[t].lc;
tree[t].lc=tree[l].rc;
tree[tree[l].rc].fat=t;
tree[l].rc=t;
tree[t].fat=l;
if (tree[t].root){
tree[t].root=;
tree[l].root=;
}else if (tree[fa].lc==t) tree[fa].lc=l;
else tree[fa].rc=l;
tree[l].fat=fa;
clean();
makenum(t);
makenum(l);
}
void splay(int t){
while (!tree[t].root){
int fa=tree[t].fat,gr=tree[fa].fat;
if (tree[fa].root){
if (tree[fa].lc==t) right(fa);
else left(fa);
}else if (tree[gr].lc==fa){
if (tree[fa].lc==t) right(gr),right(fa);
else left(fa),right(gr);
}else if (tree[fa].rc==t) left(gr),left(fa);
else right(fa),left(gr);
}
}
void access(int x){
int y=x;
x=;
do{
splay(y);
tree[tree[y].rc].root=;
tree[y].rc=x;
tree[x].root=;
clean();
makenum(y);
x=y;
y=tree[x].fat;
}while (y);
}
void change(int x,int y){
access(x);
splay(x);
tree[tree[x].lc].fat=;
tree[tree[x].lc].root=;
tree[x].lc=;
tree[x].fat=y;
clean();
access(x);
}
int main(){
freopen("bz2002.in","r",stdin);
freopen("bz2002.out","w",stdout);
int i,x,y,z;
scanf("%d\n",&n);
for (i=;i<=n;i++){
scanf("%d",&x);
x+=i;
if (x<=n) tree[i].fat=x;
else tree[i].fat=n+;
}
scanf("%d\n",&m);
for (i=;i<=m;i++){
scanf("%d%d",&x,&y);
++y;
if (x==){
scanf("%d",&z);
change(y,y+z);
}else{
access(y);
splay(y);
printf("%d\n",tree[tree[y].lc].num);
}
}
fclose(stdin);
fclose(stdout);
}
【bz2002】弹飞绵羊的更多相关文章
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 link-cut-tree
2016-05-30 11:51:59 用一个next数组,记录点x的下一个点是哪个 查询时,moveroot(n+1),access(x),splay(x) ,输出size[ch[x][0]]即为答 ...
- 【BZOJ 2002】【Hnoi 2010】弹飞绵羊 分块||Link Cut Tree 两种方法
ShallWe,Yveh,hmy,DaD3zZ,四人吃冰糕从SLYZ超市出来后在马路上一字排开,,,吃完后发现冰糕棍上写着:“向狮子座表白:愿做你的小绵羊”,,, 好吧在这道题里我们要弹飞绵羊,有分块 ...
- BZOJ-2002 弹飞绵羊 Link-Cut-Tree (分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 6801 Solved: 3573 [Submi ...
- 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块
[bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...
- c++之路进阶——codevs2333(弹飞绵羊)
2333 弹飞绵羊 2010年省队选拔赛湖南 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description Lostmonk ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 4055 Solved: 2172[Submi ...
- Bounce 弹飞绵羊
Bounce 弹飞绵羊 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 分块 将整个大区间分成若干块,每个点维护到下一个块需要跳的次 ...
随机推荐
- 10个最佳的PHP图像操作库
Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ...
- WCf的理解
从 .NET 3.5 开始 WCF 已经支持用 WebHttpBinding 构建 RESTful Web 服务,基于 WCF 框架的 RESTful Web 服务还是建立在 WCF Message ...
- NFC(3)Android上的NFC,开启NFC,3种NDEF数据
Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...
- HDU 1677
Nested Dolls Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 5311 Hidden String (暴力)
题意:今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下 ...
- ffmpeg的内部Video Buffer管理和传送机制
ffmpeg的内部Video Buffer管理和传送机制 本文主要介绍ffmpeg解码器内部管理Video Buffer的原理和过程,ffmpeg的Videobuffer为内部管理,其流程大致为:注册 ...
- 入门视频采集与处理(学会分析YUV数据)
做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角度来说,一般的视频采集芯片输出的码流一般都是YUV数据流的形式,而从视频处理(例如H.264.MPEG视频编解码)的角度来说,也是在原始 ...
- Mysql读写分离(mysql-proxy)
MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载平衡,故障.查询分析,查询过滤 ...
- Vim学习总结
Vim 目前还没感觉到比在Mac下使用Sublime Text高效到哪 安装 sudo apt-get install vim 常用配置 在Linux环境下Vim的初始化配置文件为.vimrc,通常有 ...