有没有大佬帮忙看一下,在编译器上能过,但在这编译失败了
查看原帖
有没有大佬帮忙看一下,在编译器上能过,但在这编译失败了
1404537
xiaominghao楼主2025/7/24 22:44
#include<bits/stdc++.h>
using namespace std;
int n,root,cnt,opt,x;
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];
inline void update(int root){
	t[root].size=t[t[root].left].size+t[t[root].right].size+t[root].num;
	//更新结点信息 
}
int rank(int x,int root){//查找数的排名 
	if(root){
		if(x<t[root].value) //右子树所有数都比X小,故进入左子树
			return rank(x,t[root].left);
		if(x>t[root].value) 
			//左子树所有数都比X小,故进入右子树并且加上左子树的size 
			return rank(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)
		//排名为x的数在左子树,故进入左子树
		return kth(x,t[root].left); 
	if(x<=t[t[root].left].size+t[root].num)
		//当前根结点就是排名为x的数,返回当前根结点的值 
		return t[root].value; 
	return kth(x-t[t[root].left].size-t[root].num,t[root].right);
	/*排名为x的数在右子树,故进入右子树,并把x减去左子树size+t[root].num(根据点)*/
}	
void insert(int x,int & root){
	if(x<t[root].value) //插入到左子树中 
		if(!t[root].left) 
			//左儿子不存在,则新建一个权值为x的结点作为左儿子 
			t[t[root].left=++cnt]=Node(0,0,1,x); 
		else //左儿子存在,则递归插入 
			insert(x,t[root].left); 
	else if(x>t[root].value) //插入到右子树中 
		if(!t[root].right)  
			//右儿子不存在,则新建一个权值为x的结点作为右儿子 
			t[t[root].right=++cnt]=Node(0,0,1,x);
		else //	右儿子存在,则递归插入 
			insert(x,t[root].right); 
	else //x的结点已经存在,把结点大小加一 
		t[root].num++; 
	update(root);
}
int main(){
	cin>>n;
	int num=0;
	t[root=++cnt]=Node(0,0,1,2147483647);
	while(n--){
		cin>>opt>>x;
		num++;
		if(opt==1) cout<<rank(x,root)<<"\n";
		else if(opt==2)cout<<kth(x,root)<<"\n";
		else if(opt==3)cout<<kth(rank(x,root)-1,root)<<"\n";
		else if(opt==4) cout<<kth(rank(x+1,root),root)<<"\n";
		else num--,insert(x,root);
	}
	return 0;
}
2025/7/24 22:44
加载中...