平衡树那写错了?
查看原帖
平衡树那写错了?
162196
伟大的王夫子楼主2020/4/29 15:44
#include<bits/stdc++.h>		
using namespace std;
template<class item>
inline item read() {
	item res = 0;
	bool negative = 0;
	char ch = getchar();
	while (!isdigit(ch)) negative |= ch == '-', ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return negative ? -res : res;
}
template<class item>
inline item read(item & t) {
	t = read<item>();
	return t;
}
#define max(a, b)  (!((a) < (b)) ? (a) : (b))
#define min(a, b)  ((a) < (b) ? (a) : (b))
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
	read(t), read(args...);
}	
struct{
	int l, r;
	int val, dat;
	int cnt, size;
}a[300010];
int tot, root;
const int oo = (1 << 30);
int New(int val){
	a[++tot].val = val;
	a[tot].dat = rand();
	a[tot].size = a[tot].cnt = 1;
	return tot;
}
void update(int p){
	a[p].size = a[a[p].l].size + a[a[p].r].size + a[p].cnt;
}
void build(){
	root = New(-oo),a[1].r = New(oo);
	update(root);
}
void zig(int &p){
	int q = a[p].l;
	a[p].l = a[q].r, a[q].r = p, p = q;
	update(p),update(a[p].r);
}
void zag(int &p){
	int q = a[p].r;
	a[p].r = a[q].l, a[q].l = p, p = q;
	update(p),update(a[p].l);
}
void insert(int &p,int val){
	if(!p) {
		p = New(val);
		return;
	}
	if(val == a[p].val){
		return;
	}
	if(val < a[p].val){
		insert(a[p].l, val);
		if(a[a[p].l].dat > a[p].dat) zig(p);
	}
	else{
		insert(a[p].r, val);
		if(a[a[p].r].dat > a[p].dat) zag(p);
	}
	update(p);
}
int getpre(int val){
	int ans = 1, p = root;
	while(p){
		if(val == a[p].val){
			if(a[p].l) {
				p = a[p].l;
				while(a[p].r) p = a[p].r;
				ans = p;
			}
			break;
		}
		if(a[p].val < val && a[p].val > a[ans].val) ans = p;
		p = val < a[p].val ? a[p].l : a[p].r;
	}
	return a[ans].val;
}
int getnext(int val){
	int ans = 2, p = root;
	while(p){
		if(val == a[p].val){
			if(a[p].r) {
				p = a[p].r;
				while(a[p].l) p = a[p].l;
				ans = p;
			}
			break;
		}
		if(a[p].val > val && a[p].val < a[ans].val) ans = p;
		p = val < a[p].val ? a[p].l : a[p].r;
	}
	return a[ans].val;
}
int getrankbyval(int p,int val){
	if(!p) return 0;
	if(val == a[p].val) return a[a[p].l].size + 1;
	if(val < a[p].val) return getrankbyval(a[p].l, val);
	return getrankbyval(a[p].r, val) + a[a[p].l].size + a[p].cnt;
}
int getvalbyrank(int p, int rank){
	if(!p) return 0;
	if(a[a[p].l].size >= rank) return getvalbyrank(a[p].l, rank);
	if(a[a[p].l].size + a[p].cnt >= rank) return a[p].val;
	return getvalbyrank(a[p].r, rank - a[a[p].l].size - a[p].cnt);
}
void remove(int &p,int val){
	if(!p) return;
	if(val == a[p].val){
		if(a[p].cnt > 1){
			--a[p].cnt, update(p);
			return;
		}
		else{
			if(a[p].l || a[p].r){
				if(a[p].r == 0 || a[a[p].l].dat > a[a[p].r].dat)
					zig(p), remove(a[p].r, val);
				else
					zag(p), remove(a[p].l, val);
				update(p);
			}
			else p = 0;
			return;
		}
	}
	remove(val < a[p].val ? a[p].l : a[p].r, val);
	update(p);
}
int n, m, b[200010], u[200010];
int main(){
	build();
	//freopen("data.out", "w", stdout);
	read(n, m);
	int j = 1, cnt = 1;
	for (register int i = 1; i <= n; ++i) read(b[i]);
	for (register int i = 1; i <= m; ++i) read(u[i]);
	sort(u + 1, u + m + 1);
	for (register int i = 1; i <= m; ++i) {
		while (j <= u[i]) insert(root, b[j++]);
		printf("%d\n", getvalbyrank(root, ++cnt));
	}
}

模板都过了啊

2020/4/29 15:44
加载中...