[题解]LuoGu2343:宝石管理系统

原题传送门

法1 Splay

一眼树状数组水过,
然后发现可以用 s p l a y splay splay
就来练手啦

十分基本的insert与kth操作
然后因为查询的是第k大,所以我改了一下kth
本来是先跟左儿子的size比较,现在我先跟右儿子的size比较

因为“左中右”的遍历出来结果是从小到大的
那么我“右中左”出来就是从大到小的

Code:

#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
int rt, sz, n, m, key[maxn], f[maxn], size[maxn], recy[maxn], son[maxn][2];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

void clear(int x){ f[x] = size[x] = recy[x] = key[x] = son[x][0] = son[x][1] = 0; }

int get(int x){ return son[f[x]][1] == x; }

void update(int x){
	if (x){
		size[x] = recy[x];
		if (son[x][0]) size[x] += size[son[x][0]];
		if (son[x][1]) size[x] += size[son[x][1]];
	}
}

void connect(int x, int y, int z){
	if (x) f[x] = y;
	if (y) son[y][z] = x;
}

void rotate(int x){
	int fa = f[x], ffa = f[fa], m = get(x), n = get(fa);
	connect(son[x][m ^ 1], fa, m);
	connect(fa, x, m ^ 1);
	connect(x, ffa, n);
	update(fa); update(x);
}

void splay(int x, int goal){
	while (f[x] != goal){
		int fa = f[x];
		if (f[fa] != goal) rotate(get(x) == get(fa) ? fa : x);
		rotate(x);
	}
	if (!goal) rt = x;
}

void insert(int x){
	int now = rt, fa = 0;
	while (1){
		if (!now){
			now = ++sz;
			key[now] = x, f[now] = fa, size[now] = recy[now] = 1;
			son[now][0] = son[now][1] = 0;
			if (fa) son[fa][x > key[fa]] = now;
			update(fa); splay(now, 0); return;
		}
		if (x == key[now]){
			++recy[now];
			update(now); update(fa);
			splay(now, 0); return;
		}
		fa = now, now = son[now][x > key[now]];
	}
}

int kth(int x){
	int now = rt;
	while (1){
		if (x <= size[son[now][1]]) now = son[now][1]; else//先与右儿子比较
		if (x <= size[son[now][1]] + recy[now]) return key[now]; else
		x -= size[son[now][1]] + recy[now], now = son[now][0];
	}
}

int main(){
	n = read(), m = read();
	for (int i = 1; i <= n; ++i){
		int x = read();
		insert(x);
	}
	while (m--){
		int opt = read(), x = read();
		if (opt == 1) printf("%d\n", kth(x)); else insert(x);
	}
	return 0;
}

法2 fhq Treap

fhq Treap裸题~~~

Code:

#include <bits/stdc++.h>
#define maxn 200010
using namespace std;
int rt, sz, son[maxn][2], val[maxn], key[maxn], size[maxn], n, m;

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int addnode(int a){
	++sz;
	val[sz] = a, key[sz] = rand() * rand(), size[sz] = 1;
	return sz;
}

void pushup(int x){ size[x] = size[son[x][0]] + size[son[x][1]] + 1; }

void split(int now, int w, int &u, int &v){
	if (!now) u = v = 0; else{
		if (val[now] <= w) u = now, split(son[now][1], w, son[u][1], v); else
		v = now, split(son[now][0], w, u, son[v][0]);
		pushup(now);
	}
}

int merge(int u, int v){
	if (!u || !v) return u + v;
	if (key[u] <= key[v]){
		son[v][0] = merge(u, son[v][0]);
		pushup(v);
		return v;
	} else{
		son[u][1] = merge(son[u][1], v);
		pushup(u);
		return u;
	}
}

int kth(int now, int k){
	while (1){
		if (size[son[now][0]] >= k) now = son[now][0]; else
		if (size[son[now][0]] + 1 >= k) return now; else
		k -= size[son[now][0]] + 1, now = son[now][1];
	}
}

int main(){
	srand(time(0));
	n = read(), m = read();
	for (int i = 1; i <= n; ++i){
		int a = read(), x, y;
		split(rt, a, x, y);
		rt = merge(merge(x, addnode(a)), y);
	}
	while (m--){
		int opt = read(), a = read();
		if (opt == 1) printf("%d\n", val[kth(rt, size[rt] + 1 - a)]); else{
			int x, y;
			split(rt, a, x, y);
			rt  = merge(merge(x, addnode(a)), y);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值