[BZOJ2002][Hnoi2010]Bounce 弹飞绵羊 && LCT
2015-04-07 23:25
375 查看
对于每一个节点 维护他的size
每一个弹射器 由它即将到达的弹射器向他连边
虚拟一个n+1号节点 进行最后一次弹射
每次直接查询对应位置的size即可
每一个弹射器 由它即将到达的弹射器向他连边
虚拟一个n+1号节点 进行最后一次弹射
每次直接查询对应位置的size即可
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<queue> #include<map> #define SF scanf #define PF printf using namespace std; typedef long long LL; const int MAXN = 200000; struct LCT { int ch[MAXN+10][2], fa[MAXN+10]; int st[MAXN+10], sz[MAXN+10]; bool rev[MAXN+10]; bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } void up(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; } void pushdown(int x) { int &ls = ch[x][0], &rs = ch[x][1]; if(rev[x]) { rev[x] ^= 1; rev[ls] ^= 1; rev[rs] ^= 1; swap(ls, rs); } } void Rotate(int x) { int y = fa[x], z = fa[y]; bool d = x == ch[y][0]; if(!isroot(y)) ch[z][y == ch[z][1]] = x; fa[x] = z; fa[y] = x; fa[ch[x][d]] = y; ch[y][!d] = ch[x][d]; ch[x][d] = y; up(y); } void splay(int x) { int top = 0; st[++top] = x; for(int i = x; !isroot(i); i = fa[i]) st[++top] = fa[i]; for(int i = top; i; i--) pushdown(st[i]); while(!isroot(x)) { int y = fa[x], z = fa[y]; if(!isroot(y)) { if(ch[y][0] == x ^ ch[z][0] == y) Rotate(x); else Rotate(y); } Rotate(x); } up(x); } void access(int x) { int t = 0; while(x) { splay(x); ch[x][1] = t; t = x; x = fa[x]; } } void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; } void link(int x, int y) { makeroot(x); fa[x] = y; splay(x); } void cut(int x, int y) { makeroot(x); access(y); splay(y); ch[y][0] = fa[x] = 0; } int find(int x) { access(x); splay(x); int y = x; while(ch[y][0]) y = ch[y][0]; return y; } } sp; int n, m; int Nex[MAXN+10]; int main() { SF("%d", &n); for(int i = 1; i <= n; i++) { int x; SF("%d", &x); sp.fa[i] = i+x; sp.sz[i] = 1; if(i+x > n+1) sp.fa[i] = n+1; Nex[i] = sp.fa[i]; } sp.sz[n+1] = 1; SF("%d", &m); for(int i = 1; i <= m; i++) { int op, x, y; SF("%d", &op); if(op == 1) { SF("%d", &x); x++; sp.makeroot(n+1); sp.access(x); sp.splay(x); PF("%d\n", sp.sz[sp.ch[x][0]]); } else { SF("%d%d", &x, &y); x++; int New = min(n+1, x+y); sp.cut(x, Nex[x]); Nex[x] = New; sp.link(x, Nex[x]); } } }
相关文章推荐
- 【Hnoi2010】Bzoj2002 Bounce & Codevs2333 弹飞绵羊
- BZOJ2002 HNOI2010 Bounce 弹飞绵羊 题解&代码
- [LCT]BZOJ 2002——[Hnoi2010]Bounce 弹飞绵羊
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【LCT】
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 (LCT)
- 【分块】【LCT】bzoj2002 [Hnoi2010]Bounce 弹飞绵羊
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 lct模板
- 【bzoj2002】【Hnoi2010】【Bounce 弹飞绵羊】【lct】
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 (LCT)
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
- [LCT] BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊( LCT )
- 初识lct——洛谷p3203 bzoj2002 [HNOI2010]BOUNCE 弹飞绵羊
- 【LCT】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT