[luoguP3203][HNOI2010]BOUNCE 弹飞绵羊(LCT)
每个点都会跳到另一个点,连边就是一棵树。
更改弹力就是换边。
求一个点跳多少次跳到终点就是求这个点的深度,那么只需要维护 size 域,access(n + 1) 然后 splay(x),求 size[son[x][0]] 即可,
因为 splay 是以深度为关键字的,所以左端点就是深度比它小的。
——代码
#include <cstdio>
#include <iostream>
#define N 200010
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
#define get(x) (son[f[x]][1] == (x))
#define isroot(x) (son[f[x]][0] ^ (x) && son[f[x]][1] ^ (x)) int n, k;
int a[N], f[N], size[N], rev[N], son[N][], s[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void update(int x)
{
if(x)
{
size[x] = ;
if(son[x][]) size[x] += size[son[x][]];
if(son[x][]) size[x] += size[son[x][]];
}
} inline void pushdown(int x)
{
if(x && rev[x])
{
swap(son[x][], son[x][]);
if(son[x][]) rev[son[x][]] ^= ;
if(son[x][]) rev[son[x][]] ^= ;
rev[x] = ;
}
} inline void rotate(int x)
{
int old = f[x], oldf = f[old], wh = get(x); if(!isroot(old))
son[oldf][old == son[oldf][]] = x;
f[x] = oldf; son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; son[x][wh ^ ] = old;
f[old] = x; update(old);
update(x);
} inline void splay(int x)
{
int i, fa, t = ;
s[++t] = x;
for(i = x; !isroot(i); i = f[i]) s[++t] = f[i];
for(i = t; i >= ; i--) pushdown(s[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fa = f[x]))
rotate(get(x) ^ get(fa) ? x : fa);
} inline void access(int x)
{
for(int t = ; x; t = x, x = f[x]) splay(x), son[x][] = t, update(x);
} inline void reverse(int x)
{
access(x);
splay(x);
rev[x] ^= ;
} inline void cut(int x, int y)
{
reverse(x);
access(y);
splay(y);
son[y][] = f[x] = ;
update(y);
} inline void link(int x, int y)
{
reverse(x);
f[x] = y;
access(x);
} inline int query(int x)
{
reverse(n + );
access(x);
splay(x);
return size[son[x][]];
} int main()
{
int i, x, y, z;
n = read();
for(i = ; i <= n; i++)
{
x = read();
a[i] = f[i] = min(i + x, n + );
size[i] = ;
}
size[n + ] = ;
k = read();
for(i = ; i <= k; i++)
{
z = read();
x = read() + ;
if(z == ) printf("%d\n", query(x));
else
{
y = read();
cut(x, a[x]);
link(x, a[x] = min(x + y, n + ));
}
}
return ;
}
[luoguP3203][HNOI2010]BOUNCE 弹飞绵羊(LCT)的更多相关文章
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- luoguP3203 [HNOI2010]BOUNCE 弹飞绵羊
P3203 [HNOI2010]BOUNCE 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonke ...
- [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 lct 动态树 splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 http://blog.csdn.net/frods/article/details/5224 ...
- [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (LCT维护深度)
要维护深度,就维护一下size就行了.access一下x,那么从根->x这一条链就独立成为一棵splay,那么splay的size节点数就是x的深度. 删边的时候直接access一下,splay ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
- [BZOJ2002][洛谷P3203][Hnoi2010]Bounce 弹飞绵羊(LCT维护链长)
luogu传送门 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 16082 Solved: ...
随机推荐
- Avito Code Challenge 2018 A~E
A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...
- 怎样去思考问题 解决问题 zkc学长的福利
题目描述 又是一年ACM集训的时刻,zkc学长邀请n位同学来玩一个有奖游戏.首先,他让每个同学在左.右手上面分别写下一个整数,zkc学长自己也在左.右手上各写一个整数.然后,让这n位同学排成一排,zk ...
- 有符号char转无符号short
; cout<<(int)ch<<endl; //-1 unsigned short d = ch; short dd = ch; cout<<d<<e ...
- xcode 制作静态库文件(.a)
参考: http://www.jb51.net/article/37853.htm 摘要: 1. 获取.a文件的信息 lipo -info /Users/pjk1129/De ...
- virtualwrapper使用
1.创建: mkvirtualenv 虚拟环境目录 2.查看虚拟环境 workon lsvirtualenv 3.进入虚拟环境 workon 虚拟环境目录 4.退出虚拟环境 deactivate 5. ...
- JS获取到时间转换成字符串类型
JS获取到时间格式为:/Date(1479279579047)/ 提供一个函数可将其转换为“yyyy-MM-dd”和“yyyy-MM-dd hh:mm:ss” //时间日期转换成string func ...
- mybatis使用中类属性名和数据库表字段名问题
起初我以为上述二者必须一致,后来发现也是可以像Hibernate那样在xml文件中进行映射的. <mapper namespace="com.tenghu.mybatis.model. ...
- PHP无符号右移与旋转右移
# PHP 无符号右移 仅用于int形, PHP 的int为32位 # // 右移旋转 function rightRoate($int,$n){ $min = intval(PHP_INT_MAX ...
- 仿ofo单车做一个轮播效果
github地址 首先我是利用swiper.js做的,因为这个很强大,哈哈~~,上代码 html很简单 <body> <div class="swiper-containe ...
- Chromium浏览器编译成功庆祝
1.什么是Chromium Chromium 是Google公司的开源项目 Google浏览器 最新版360浏览器 都是在Chromium的基础上重新编译的. 2.什么是双核浏览器 ...