bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
来源:互联网 发布:日式收纳知乎 编辑:程序博客网 时间:2024/04/27 05:02
题目:
http://www.lydsy.com/JudgeOnline/problem.php?id=2002
题意:
Description
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input
第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
思路:
之前用分块做过这题。用
#include <bits/stdc++.h>using namespace std;const int N = 200000 + 10, INF = 0x3f3f3f3f;int son[N][2], fat[N], siz[N], lazy[N], nxt[N];int top, st[N];bool is_root(int x){ return son[fat[x]][0] != x && son[fat[x]][1] != x;}void push_up(int x){ siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;}void push_down(int x){ if(lazy[x]) { swap(son[x][0], son[x][1]); lazy[son[x][0]] ^= 1, lazy[son[x][1]] ^= 1; lazy[x] ^= 1; }}void Rotate(int x){ int y = fat[x], p = son[y][0] == x; son[y][!p] = son[x][p], fat[son[x][p]] = y; if(! is_root(y)) son[fat[y]][son[fat[y]][1]==y] = x; fat[x] = fat[y]; son[x][p] = y, fat[y] = x; push_up(y);}void splay(int x){ top = 0; st[++top] = x; for(int i = x; !is_root(i); i = fat[i]) st[++top] = fat[i]; for(int i = top; i >= 1; i--) push_down(st[i]); while(! is_root(x)) { int y = fat[x], z = fat[y]; if(is_root(y)) Rotate(x); else { if((x == son[y][0]) ^ (y == son[z][0])) Rotate(x), Rotate(x); else Rotate(y), Rotate(x); } } push_up(x);}void access(int x){ int y = 0; while(x) { splay(x); son[x][1] = y; push_up(x);//在splay过程中其实会维护到,加不加无所谓,为了心安还是加上吧。。。 y = x, x = fat[x]; }}void make_root(int x){ access(x); splay(x); lazy[x] ^= 1;}void link(int x, int y){ make_root(x); fat[x] = y; splay(x);}void cut(int x, int y){ make_root(x); access(y); splay(y); son[y][0] = fat[x] = 0;}int main(){ int n, m, opt, x, y; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &x); fat[i] = i + x <= n ? i+x : n+1; nxt[i] = fat[i]; siz[i] = 1; } siz[n+1] = 1; scanf("%d", &m); for(int i = 1; i <= m; i++) { scanf("%d", &opt); if(opt == 1) { scanf("%d", &x); x++; make_root(n+1); access(x); splay(x); printf("%d\n", siz[son[x][0]]); } else { scanf("%d%d", &x, &y); x++; int t = x+y <= n ? x+y : n+1; cut(x, nxt[x]); link(x, t); nxt[x] = t; } } return 0;}
另外一种不太一样的写法,主要是没有把
#include <bits/stdc++.h>using namespace std;const int N = 200000 + 10, INF = 0x3f3f3f3f;int son[N][2], fat[N], siz[N];void push_up(int x){ siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;}bool is_root(int x) //判断x是不是所在辅助树的根{ return (son[fat[x]][0] != x && son[fat[x]][1] != x);}void Rotate(int x){ int y = fat[x], p = son[y][0] == x; son[y][!p] = son[x][p], fat[son[x][p]] = y; if(!is_root(y)) son[fat[y]][son[fat[y]][1]==y] = x; fat[x] = fat[y]; son[x][p] = y, fat[y] = x; push_up(y);}void splay(int x){ while(! is_root(x)) { int y = fat[x], z = fat[y]; if(is_root(y)) Rotate(x); else { if((x == son[y][0]) ^ (y == son[z][0])) Rotate(x), Rotate(x); else Rotate(y), Rotate(x); } } push_up(x);}void access(int x){ int y = 0; while(x) { splay(x); son[x][1] = y; push_up(x); y = x, x = fat[x]; }}void cut(int x){//把x旋转到splay的根,其父节点即是x的左儿子,直接切掉即可 access(x); splay(x); son[x][0] = fat[son[x][0]] = 0;}void link(int x, int y){ cut(x); fat[x] = y; push_up(x);}int main(){ int n, m, x, y; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &x); //link(i, i + x <= n ? i+x : 0); fat[i] = i + x <= n ? i+x : n+1; siz[i] = 1; } siz[n+1] = 1; scanf("%d", &m); int opt; for(int i = 1; i <= m; i++) { scanf("%d", &opt); if(opt == 1) { scanf("%d", &x); x++; access(x); splay(x); printf("%d\n", siz[son[x][0]]); } else { scanf("%d%d", &x, &y); x++; link(x, x + y <= n ? x+y : n+1); } } return 0;}
阅读全文
0 0
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 (LCT)
- 【BZOJ】2002 [Hnoi2010]Bounce 弹飞绵羊 LCT入门题
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ 2002([Hnoi2010]Bounce 弹飞绵羊-LCT)
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 (LCT)
- 【LCT】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 lct模板
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- 【BZOJ 2002】 [Hnoi2010]Bounce 弹飞绵羊 LCT
- [BZOJ 2002][Hnoi2010]Bounce 弹飞绵羊:分块|LCT
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)
- [LCT]BZOJ 2002——[Hnoi2010]Bounce 弹飞绵羊
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
- UVA 10305 Ordering Tasks(拓扑排序)
- ubuntu16.04LTS备忘录
- [模板]-AC自动机
- OPENCA源代码阅读
- JavaEE之Servlet
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- 初学Bootstrap
- Tomcat的安装配置
- 2017.8.26 noip模拟赛 总结
- 操作系统填空题
- python weakref模块
- 常见的http请求头和响应头
- WINPCAP源码阅读
- bzoj1022 [SHOI2008]小约翰的游戏John