TLE on #1 93pts求助
查看原帖
TLE on #1 93pts求助
733757
VERACITY楼主2023/10/4 10:16
#include<bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 5;
int root,tot,f[mx],ch[mx][2],val[mx],cnt[mx],siz[mx];
int n,opt,x;
inline int read()
{
	int ch = getchar(),res = 0,f = 0;
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch))  res = (res << 3) + (res << 1) + (ch ^ 48),ch = getchar();
	return !f ? res : -res;
}
void upd(int x)
{
	siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x];
}
int getdir(int x)
{
	return x == ch[f[x]][1];
}
void clear(int x)
{
	ch[x][0] = ch[x][1] = f[x] = val[x] = siz[x] = cnt[x] = 0;
}
void rotate(int x)
{
	int p = f[x],g = f[p],dir = getdir(x);
	ch[p][dir] = ch[x][!dir];
	if(ch[x][!dir]) f[ch[x][!dir]] = p;
	ch[x][!dir] = p;
	f[p] = x,f[x] = g;
	if(g) ch[g][p == ch[g][1]] = x;
	upd(p);upd(x);
}
void Splay(int x)
{
	for(int fa = f[x];fa = f[x],fa;rotate(x))
		if(f[fa])
			rotate(getdir(x) == getdir(fa) ? fa : x);
	root = x;
}
void insert(int x)
{
	if(!root)
	{
		val[++tot] = x,cnt[tot]++,root = tot;
		upd(root);
		return;
	}
	int cur = root,fa = 0;
	while(1)
	{
		if(val[cur] == x)
		{
			cnt[cur]++;
			upd(cur);upd(fa);
			Splay(cur);
			break;
		}
		fa = cur;
		cur = ch[cur][val[cur] < x];
		if(!cur)
		{
			val[++tot] = x,cnt[tot]++,f[tot] = fa,ch[fa][val[fa] < x] = tot;
			upd(tot);upd(fa);
			Splay(tot);
			break;
		}
	}
}
int find_rank(int x)
{
	int res = 0,cur = root;
	while(1)
	{
		if(x < val[cur]) cur = ch[cur][0];
		else
		{
			res += siz[ch[cur][0]];
			if(x == val[cur])
			{
				Splay(cur);
				return res + 1;
			}
			res += cnt[cur],cur = ch[cur][1];
		}
	}
}
int find_val(int x)
{
	int cur = root;
	while(1)
	{
		if(ch[cur][0] && x <= siz[ch[cur][0]])  cur = ch[cur][0];
		else
		{
			x -= cnt[cur] + siz[ch[cur][0]];
			if(x <= 0)
			{
				Splay(cur);
				return val[cur];
			}
			cur = ch[cur][1];
		}
	}
}
int find_pre()
{
	int cur = ch[root][0];
	if(!cur) return cur;
	while(ch[cur][1]) cur = ch[cur][1];
	Splay(cur);
	return cur;
}
int find_nxt()
{
	int cur = ch[root][1];
	if(!cur) return cur;
	while(ch[cur][0]) cur = ch[cur][0];
	Splay(cur);
	return cur;
}
void dele(int x)
{
	find_rank(x);
	if(cnt[root] > 1)
	{
		cnt[root]--;
		upd(root);
		return;
	}
	if(!ch[root][0] && !ch[root][1])
	{
		clear(root);
		root = 0;
		return;
	}
	if(!ch[root][0])
	{
		int cur = root;
		root = ch[root][1],f[root] = 0;
		clear(cur);
		return;
	}
	if(!ch[root][1])
	{
		int cur = root;
		root = ch[root][0],f[root] = 0;
		clear(cur);
		return;
	}
	int cur = root,k = find_pre();
	f[ch[cur][1]] = k,ch[k][1] = ch[cur][1];
	clear(cur);
	upd(root);
}
int main()
{
	n = read();
	while(n--)
	{
		opt = read(),x = read();
		if(opt == 1) insert(x);
		if(opt == 2) dele(x);
		if(opt == 3) cout << find_rank(x) << "\n";
		if(opt == 4) cout << find_val(x) << "\n";
		if(opt == 5)
		{
			insert(x);
			cout << val[find_pre()] << "\n";
			dele(x);
		}
		if(opt == 6)
		{
			insert(x);
			cout << val[find_nxt()] << "\n";
			dele(x);
		}
	}
	return 0;
}
2023/10/4 10:16
加载中...