P3203 [HNOI2010]彈飛綿羊(LCT)

在這裏插入圖片描述


i,i+k[i]i,i + k[i] 建邊,若 i+k[i]>ni + k[i] > n,則將 ii 連邊到一個虛點,最後會得到一棵樹,問題轉化成:
1.詢問 ii 點到 虛點 的路徑長度
2.修改樹形,將 ii 接到 i+yi + y

這棵樹形可以用 LCT 維護,詢問路徑長度就是將所求路徑連起來,詢問對應 splay 的 sizesize 大小

在LCT中每個 splay的節點維護一個 sz[u]sz[u],表示 uu 所在 splay 中,uu 的子樹的節點個數

在LCT中所有改變splay樹形的操作,都需要pushup,以保證維護的 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;
}
發佈了326 篇原創文章 · 獲贊 10 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章