P3203 [HNOI2010]弹飞绵羊(LCT)

在这里插入图片描述


i , i + k [ i ] i,i + k[i] i,i+k[i] 建边,若 i + k [ i ] > n i + k[i] > n i+k[i]>n,则将 i i i 连边到一个虚点,最后会得到一棵树,问题转化成:
1.询问 i i i 点到 虚点 的路径长度
2.修改树形,将 i i i 接到 i + y i + y i+y

这棵树形可以用 LCT 维护,询问路径长度就是将所求路径连起来,询问对应 splay 的 s i z e size size 大小

在LCT中每个 splay的节点维护一个 s z [ u ] sz[u] sz[u],表示 u u u 所在 splay 中, u u u 的子树的节点个数

在LCT中所有改变splay树形的操作,都需要pushup,以保证维护的 s z [ u ] sz[u] sz[u] 的正确性


代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n,m;
inline int read(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
struct LCT {				//用splay维护原森林的连通,用到了splay的操作以及数组 
	int ch[maxn][2];		//ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
	int f[maxn];			//当前节点的父节点 
	int tag[maxn];
	int top,sta[maxn],sz[maxn];
	inline bool get(int x){
    	return ch[f[x]][1] == x;
	}
	void init() {
		memset(f,0,sizeof f);
		memset(ch,0,sizeof ch);
		memset(tag,0,sizeof tag);
		for (int i = 1; i <= maxn - 10; i++)
			sz[i] = 1;
	}
	inline void pushdown(int rt) {
		if (!tag[rt]) return;
		swap(ch[rt][0],ch[rt][1]);
		if (ch[rt][0]) tag[ch[rt][0]] ^= 1;
		if (ch[rt][1]) tag[ch[rt][1]] ^= 1;
		tag[rt] = 0;
	}
	inline void pushup(int rt) {
		if (rt) {
			sz[rt] = 1;
			if (ch[rt][0]) sz[rt] += sz[ch[rt][0]];
			if (ch[rt][1]) sz[rt] += sz[ch[rt][1]];
		}
	}
	inline bool isroot(int x) {
		return (ch[f[x]][0] != x) && (ch[f[x]][1] != x);
	}
 	inline void rotate(int x) {							//旋转操作,根据 x 在 f[x] 的哪一侧进行左旋和右旋 
	    int old = f[x], oldf = f[old];
		int whichx = get(x);
		if(!isroot(old)) ch[oldf][ch[oldf][1] == old] = x;		//如果 old 不是根节点,就要修改 oldf 的子节点信息
	    ch[old][whichx] = ch[x][whichx ^ 1];
	    ch[x][whichx ^ 1] = old;
	    f[ch[old][whichx]] = old;
	    f[old] = x; f[x] = oldf;
		pushup(old); pushup(x); 
	}
	inline void splay(int x) {								//将 x 旋到所在 splay 的根
		top = 0; sta[++top] = x;
		for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i]; //在 splay 中维护 下推标记 
		while(top) pushdown(sta[top--]);
    	for(int fa = f[x]; !isroot(x); rotate(x), fa = f[x]) {	//再把x翻上来
        	if(!isroot(fa))										//如果fa非根,且x 和 fa是同一侧,那么先翻转fa,否则先翻转x 
            	rotate((get(x) == get(fa)) ? fa : x);
        }
	}
	inline void access(int x) {					//access操作将x 到 根路径上的边修改为重边 
		int lst = 0;
		while(x > 0) {
			splay(x);
			ch[x][1] = lst;
			pushup(x);
			lst = x; x = f[x];
		}
	}
	inline void move_to_root(int x) {			//将 x 移到 x 所在树的根(不是所在splay的根,所在splay只是一条重链) 
		access(x); splay(x); tag[x] ^= 1;		
		//将 x 移到 根之后 x 是深度最低的点,这条重链、这棵splay上所有点的深度颠倒,
		//所有的点的左子树的点应该到右子树,因此要翻转这棵splay的左右子树
	}
	inline int findroot(int x) {
		access(x); 
		splay(x);
		int rt = x;
		while(ch[rt][0]) rt = ch[rt][0];
		return rt;
	}
	inline void link(int x,int y) {
		move_to_root(x); f[x] = y; splay(x);
	}
	inline void cut(int x,int y) {
		move_to_root(x); access(y); 
		splay(y); ch[y][0] = f[x] = 0;
		pushup(y);
	}
}tree;
int x,y,k[maxn],op;
int main() {
	n = read();
	tree.init();
	for (int i = 1; i <= n; i++) {
		k[i] = read();
		if (i + k[i] > n) tree.link(i,n + 1);
		else tree.link(i,i + k[i]);
	}
	m = read();
	while(m--) {
		op = read(); x = read();
		x++;
		if (op == 1) {
			tree.move_to_root(n + 1);
			tree.access(x);
			tree.splay(x);
			printf("%d\n",tree.sz[x] - 1);
		} else {
			y = read();
			tree.cut(x,x + k[x] > n ? n + 1 : x + k[x]);
			k[x] = y;
			tree.link(x,x + k[x] > n ? n + 1 : x + k[x]);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值