[BZOJ2733][HNOI2012] 永无乡(splay)

题意:给出N个小岛,每个小岛有一个固定的优先级,最开始有M座桥连接其中的某些小岛。给出Q次操作,操作有两种:在两个小岛之间连一座桥,或者询问x所属的连通块内优先级第k的岛屿编号,不存在就输出-1。

这道题涉及以权值为关键字的splay的合并,开始想了一会,没想出可以很快合并的方法,然后看了别人的做法,叫做启发式合并(一开始吓哭了),但看了代码之后就无语了,就是手动把size更小的那个splay的所有节点依次插入另一颗splay(这暴力得),均摊下来好像复杂度的确不高,因为这样合并的话好像不那么容易构造可以卡死的数据。

这道题维护的不是一整棵树,而是很多个分散的splay,我以前处理不止一颗splay都是保存下每一棵树的根,但看了别人的代码,发现不需要。要处理一个节点的话,直接把它伸展到不能再向上的地方,然后它就是根了,或者不用splay直接向上找父亲就可以找到他的根。这样牺牲一点常数,少维护一些信息,好写得多。至于判断连通块,我开始想的是取一个splay中具有标志性(比如说最小或最大)的点来作为他所属连通块的信息,可以logn时间内查询,但用splay来维护连通信息太奢侈了,在外面开一个并查集即可。

不过这道题合并既然是暴力合并的,那么就用不着splay的特质了,用SBT就行了,快得多。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 100010;

inline void get(int &r) {
	char c, b=0; r=0;
	do{c=getchar();if(c=='-')b=1;}while (c<'0'||c>'9');
	do{r=r*10+c-'0';c=getchar();}while (c>='0'&&c<='9');
	if (b) r = -r;
}

int N, M;
int be[MAXN];
int bcj(int x) {
	int r = x, t;
	while (be[r] != r) r = be[r];
	while (x != be[x]) t = be[x], be[x] = r, x = t;
	return r;
}

#define lch(x) x->ch[0]
#define rch(x) x->ch[1]
struct Node {
	int sz, v, id;
	Node*fa, *ch[2];
} nil, *NIL = &nil;

Node nds[MAXN];

inline void pushup(Node*x) {
	x->sz = lch(x)->sz + rch(x)->sz + 1;
}

inline void rotate(Node*x)
{
	Node *y = x->fa;
	int d = (x==lch(y));
	y->ch[!d] = x->ch[d];
	if (x->ch[d] != NIL)
		x->ch[d]->fa = y;
	x->fa = y->fa;
	if (y->fa != NIL)
		y->fa->ch[ y->fa->ch[1]==y ] = x;
	x->ch[d] = y;
	y->fa = x;
	pushup(y);
}

void splay(Node*x, Node*to = NIL)
{
	for (Node *y, *z; x->fa != to; rotate(x)) {
		y = x->fa; z = y->fa;
		if (z != to) rotate((y==lch(z))^(x==lch(y)) ? x : y);
	}
	pushup(x);
}

int getkth(Node*r, int k)
{
	if (k < 0 || k > r->sz) return -1;
	Node*t = r;
	while (k != lch(t)->sz + 1)
		if (k <= lch(t)->sz) t = lch(t);
		else k -= lch(t)->sz + 1, t = rch(t);
	splay(t);
	return t -> id;
}

void ins(Node*r, Node*x)
{
	splay(r);
	Node *t = r, *f = r;
	while (t != NIL)
		t->sz++, f = t, t = t->ch[x->v > t->v];
	f->ch[x->v > f->v] = x;
	x->fa = f;
	lch(x) = rch(x) = NIL;
	splay(x);
}

Node*Q[MAXN];
void merge(int a, int b)
{
	Node*x = &nds[a], *y = &nds[b];
	splay(x);
	splay(y);
	if (x->sz > y->sz) swap(x, y);
	int l = 1, r = 1;
	Q[1] = x;
	while (l <= r) {
		if (lch(Q[l]) != NIL) Q[++r] = lch(Q[l]);
		if (rch(Q[l]) != NIL) Q[++r] = rch(Q[l]);
		l ++;
	}
	for (l = 1; l<=r; ++l)
		ins(y, Q[l]);
}

int main()
{
	nil.fa = nil.ch[0] = nil.ch[1] = NIL;
	get(N), get(M);
	int qn, a, b;
	for (int i = 1; i<=N; ++i) {
		get(nds[i].v), nds[i].sz = 1, nds[i].id = i;
		nds[i].fa = nds[i].ch[0] = nds[i].ch[1] = NIL;
		be[i] = i;
	}
	for (int i = 1; i<=M; ++i) {
		get(a), get(b);
		if (bcj(a) == bcj(b)) continue;
		merge(a, b);
		be[bcj(b)] = bcj(a);
	}
	get(qn);
	char c;
	while (qn --) {
		do c=getchar(); while (c!='Q' && c!='B');
		get(a), get(b);
		if (c == 'B') {
			if (bcj(a) == bcj(b)) continue;
			be[bcj(b)] = bcj(a);
			merge(a, b);
		}
		else {
			splay(nds + a);
			printf("%d\n", getkth(nds+a, b));
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值