您的位置:首页 > 其它

[BZOJ2002][Hnoi2010]Bounce 弹飞绵羊 && LCT

2015-04-07 23:25 375 查看
对于每一个节点 维护他的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]);
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: