【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 分块 将整个大区间分成若干块,每个点维护到下一个块需要跳的次 ...
随机推荐
- NSDate & NSDateFormatter
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasep ...
- python3.4安装suds
使用suds访问webservice十分方便 python3.x安装suds会报错“No module named client” 在stackoverflow上找到了替代方法,安装suds-jurk ...
- nyoj-257 郁闷的C小加(一) 前缀表达式变后缀
郁闷的C小加(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...
- Java泛型 通配符? extends与super
Java 泛型 关键字说明 ? 通配符类型 <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 T的子类 <? super T> 表示类型下界(Java ...
- HTML 中的meta标签中的http-equiv与name属性使用介绍
meta是html语言head区的一个辅助性标签.也许你认为这些代码可有可无.其实如果你能够用好meta标签,会给你带来意想不到的效果,meta标签的作用有:搜索引擎优化(SEO),定义页面使用语言, ...
- drop.delete.trauncat的区别
delete删除数据,保留表结构,可以回滚,如果数据量大,很慢,回滚是因为备份了删除的数据(删除数据时有两个动作,删除和备份) truncate删除所有数据,保留表结构,不可以回滚,一次全部删除所有数 ...
- bzoj2829
裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...
- SQL查看数据库所用用户表数量和使用的空间
SQL Server数据库管理员通常硬盘空间奋斗,不断努力清理“表”,撰写许多查询,发现该表使用的硬盘空间. 本文介绍了如何查询系统表的空间使用情况,帮助数据库管理员识别正在使用最多的空间,以便存档旧 ...
- ASP.NET MVC 学习8、Controller中的Detail和Delete方法
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...