MLE求助
查看原帖
MLE求助
457431
Li_wenjie楼主2022/3/22 22:18
#include<bits/stdc++.h>
using namespace std;
const int N=101;
struct Node
{
	int value,size,left,right,num;
	Node(int l,int r,int s,int v)
	: left(l),right(r),size(s),value(v),num(1){}
	Node(){}
}a[N];
int cnt=0,q,opt;
void charu(int x,int r)//½«x²åÈëËÑË÷Ê÷µÄ×ÓÊ÷£¨ÒÔa[r]Ϊ¸ù£© 
{
	if(x>a[r].value)
	{
		a[r].size++;
		if(a[r].right!=0)
		{
			charu(x,a[r].right);
		}
		else
		{
			cnt++;
			a[r].right=cnt;
			a[cnt]=Node(0,0,0,x);
			return;
		}
	}
	else if(x==a[r].value)
	{
		a[r].num++;
		return;		
	}
	else
	{
		a[r].size++;
		if(a[r].left!=0)
		{
			charu(x,a[r].left);
		}
		else
		{
			cnt++;
			a[r].left=cnt;
			a[cnt]=Node(0,0,0,x);
			return;
		}
	}
}
int kth(int x,int root)//²éѯÒÔrootΪ¸ùµÄ×ÓÊ÷ÖÐÅÅÃûΪxµÄÊý 
{
	int root_rank=a[a[root].left].size+a[a[root].left].num+1;
	if(x==root_rank) return a[root].value;
	else if(x<root_rank)
	{
		return kth(x,a[root].left);
	}	
	else
	{
		return kth(x-a[a[root].left].size-a[root].num,a[root].right);
	}
}
int rak(int x,int root)//²éѯxÊýÔÚÒÔrootΪ¸ù×ÓÊ÷µÄÅÅÃû 
{
	if(!root) return 1;
	if(x>a[root].value)
	{
		return rak (x,a[root].right)+a[a[root].left].size+a[root].num;
	}
	else if(x<a[root].value) return rak(x,a[root].left);
	else return a[a[root].left].size+1;
} 
int root; 
int main()
{
	cin>>q;
	a[root=++cnt]=Node(0,0,0,2147483647);
	for(int i=1;i<=q;i++)
	{
		int x;
		cin>>opt>>x;
		if(opt==1)
		{
			cout<<rak(x,root)<<endl;
		}
		else if(opt==2)
		{
			cout<<kth(x,root)<<endl;
		}
		else if(opt==3)
		{
			cout<<kth(rak(x,root)-1,root)<<endl;
		}
		else if(opt==4)
		{
			cout<<kth(rak(x,root)+1,root)<<endl;
		}
		else charu(x,root);
	}
}
2022/3/22 22:18
加载中...