FHQ 8-12TLE求调
查看原帖
FHQ 8-12TLE求调
537763
chrisgr_pretty_girl楼主2023/11/26 10:56

稍微有一点改动 1.一个节点还要存当前数的数量(一个权只用一个节点) 2.用一个栈存储空节点的下标,每次new的时候从栈顶取节点下标 T的点是8-12

#include<iostream>
#include<stack>
#include<ctime>
using namespace std;
const int N = 1e5 + 5;
int root, n;
struct node {
	int son[2];
	int val;
	int rd;
	int size;
	int sum;
}tree[N];
stack<int> stk;
void init() { for(int i = n; i; --i) stk.push(i); }
void maintain(int x) { tree[x].size = tree[tree[x].son[0]].size + tree[tree[x].son[1]].size + tree[x].sum; }
void split(int now, int k, int &x, int &y) {
	if(!now) x = y = 0;
	else {
		if(tree[now].val <= k) {
			x = now;
			split(tree[x].son[1], k, tree[x].son[1], y);
		} else {
			y = now;
			split(tree[y].son[0], k, x, tree[y].son[0]);
		}
		maintain(now);
	}
}
int merge(int x, int y) {
	if(!x || !y) return x + y;
	if(tree[x].rd < tree[y].rd) {
		tree[x].son[1] = merge(tree[x].son[1], y);
		maintain(x);
		return x;
	} else {
		tree[y].son[0] = merge(x, tree[y].son[0]);
		maintain(y);
		return y;
	}
}
int mknew (int x) {
	int a = stk.top();
	stk.pop();
	tree[a].rd = rand();
	tree[a].val = x;
	//cout<<tree[a].val<<" "<<x<<" "<<a<<endl;
	tree[a].son[1] = 0;
	tree[a].son[0] = 0;
	tree[a].size = 1;
	tree[a].sum = 1;
    //cout<<x<<" "<<tree[a].val<<" "<<tree[a].rd<<endl;
	return a;
}
void update(int x) {
	int a, b, c;
	split(root, x, a, b);
	split(a, x - 1, a, c);
    //cout<<a<<" "<<b<<" "<<c<<endl;
	if(c) ++tree[c].sum;
	else c = mknew(x);
    //cout<<c<<" "<<tree[c].val<<endl;
	root = merge(merge(a, c), b);
}
void delet(int x) {
	int a, b, c;
	split(root, x, a, b);
	split(a, x - 1, a, c);
	if(--tree[c].sum) root = merge(merge(a, c), b);
	else {
		stk.push(c);
		root = merge(a, b);
	}
}
int find1(int x) {
	int a, b;
	split(root, x - 1, a, b);
	int rt = tree[a].size + 1;
	root = merge(a, b);
	return rt;
}
int find2(int x, int now) {
	int sm = 0;
	while(1) {
        //cout << tree[now].val << " " << tree[now].sum << " " << tree[now].son[1] << " " << tree[now].son[2] << " " << sm <<endl;
		if(tree[now].sum + tree[tree[now].son[0]].size + sm < x) sm += tree[now].sum + tree[tree[now].son[0]].size, now = tree[now].son[1];
		else if(tree[now].sum + tree[tree[now].son[0]].size + sm > x) now = tree[now].son[0];
		else return tree[now].val;
	}
}
int qq(int x) {
    int a, b, as;
	split(root, x - 1, a, b);
	as = find2(tree[a].size, a);
	root = merge(a, b);
	return as;
}
int hj(int x) {
	int a, b, as;
	split(root, x + 1, a, b);
    //cout<<b<<endl;
	as = find2(1, b);
	root = merge(a, b);
	return as;
}
int main() {
	srand(time(NULL));
	//std::ios::sync_with_stdio(false);
	//std::cout.tie(0);
	cin >> n;
	init();
	for(int i = n; i; --i) {
		int opt, x;
		cin >> opt >> x;
		if(opt == 6) cout << hj(x) << endl;
		else if(opt == 5) cout << qq(x) << endl;
		else if(opt == 4) cout << find2(x, root) << endl;
		else if(opt & 2) delet(x);
		else if(opt ^ 1) cout << find1(x) << endl;
		else update(x);
        //cout<<root<<endl;
	}
	return 0;
}
2023/11/26 10:56
加载中...