时间问题!
查看原帖
时间问题!
786127
chrispang楼主2024/7/8 11:29

以下代码过不了hack数据,比如这个,所以求调。

#include <bits/stdc++.h>
using namespace std;

int cnt;
struct node{
	int left, right, size, value, num;
	node(int l, int r, int s, int v) {
		left = l, right = r, size = s, value = v, num = 1;
	}
	node() {} 
}t[100010];

void update(int root) {
	t[root].size = t[t[root].left].size + t[t[root].right].size + t[root].num;
}

int ran(int x, int root) {
	if(root) {
		if(x < t[root].value) return ran(x, t[root].left);
		if(x > t[root].value) return ran(x, t[root].right) + t[t[root].left].size + t[root].num;
		return t[t[root].left].size + t[root].num;
	}
	return 1;
}

int kth(int x, int root) {
	if(x <= t[t[root].left].size) return kth(x, t[root].left);
	if(x <= t[t[root].left].size + t[root].num) return t[root].value;
	return kth(x - t[t[root].left].size - t[root].num, t[root].right); 
}

void insert(int x, int root) {
	if(x < t[root].value)
		if(!t[root].left) {
			t[root].left = ++cnt;
			t[t[root].left] = node{0, 0, 1, x};
		}
		else insert(x, t[root].left);
	else if(x > t[root].value)
		if(!t[root].right) {
			t[root].right = ++cnt;
			t[t[root].right] = node{0, 0, 1, x};
		}
		else insert(x, t[root].right);
	else t[root].num++;
	update(root);
}

int main() {
	int n = 0, root = 0, opt = 0, x = 0;
	scanf("%d", &n);
	root = ++cnt; 
	t[root] = node(0, 0, 1, 2147483647);
	while(n--) {
		scanf("%d%d", &opt, &x);
		if(opt == 1) printf("%d\n", ran(x, root));
		else if(opt == 2) printf("%d\n", kth(x, root));
		else if(opt == 3) printf("%d\n", kth(ran(x, root) - 1, root));
		else if(opt == 4) printf("%d\n", kth(ran(x + 1, root), root));
		else if(opt == 5) insert(x, root);
	}
    return 0;
}
2024/7/8 11:29
加载中...