萌新代码MLE求调
查看原帖
萌新代码MLE求调
614939
YQY0518楼主2023/3/31 19:53

rt,代码如下:

#include <bits/stdc++.h>
using namespace std;
struct node{
	int key,cnt;//cnt表示当前值的数字有多少个 
	int lc;
	int rc;
	int ht;//ht表示以当前节点为根的子树的最大深度 
	int size;//当前节点为根的子树的总数字数 
	node(int x=0):key(x),lc(0),rc(0),ht(1),size(1),cnt(1){};
};
node bst[100001];
int tail=1;
int root=0;
int n;
int LLwrong(int);//维护LL型的失衡
int RRwrong(int);//维护RR型的失衡
int LRwrong(int);//维护LR型的失衡
int RLwrong(int);//维护RL型的失衡
void preorder(int);//用前序遍历输出
void htcount(int);//(最好改成update)更新节点的子树的最大深度 
void sizecount(int);//更新节点的排名 
int insertnode(int,int);//插入节点 
int deletenode(int,int);//删除节点 
void myswap(node &,node &);//交换两个节点 
int getprenode(int);//获取前驱节点
int getsucnode(int);//获取后继节点 
int fix(int);//自动维护当前节点的平衡 
int queryRank(int,int);//查找x节点的排名 
int queryKey(int,int);//查找x排名的节点 
int queryPre(int,int);//查找x节点的前驱 
int querySuc(int,int);//查找x节点的后继 
int main(){
	bst[0].ht=0;
	bst[0].size=0;
	bst[0].cnt=0;
	cin>>n;
	for(int i=0,x,c;i<n;i++){
		cin>>x>>c;
		switch(x){
			case 1:{
				root=insertnode(root,c);
				//cout<<"成功"<<endl;
				//preorder(root);
				//cout<<endl;
				break;
			}
			case 2:{
				root=deletenode(root,c);//root要更新! 
				//cout<<"成功"<<endl;
				//preorder(root);
				//cout<<endl;
				break;
			}
			case 3:{
				cout<<queryRank(c,root)<<endl;
				//cout<<"成功"<<endl;
				break;
			}
			case 4:{
				cout<<queryKey(c,root)<<endl;
				//cout<<"成功"<<endl;
				break;
			}
			case 5:{
				cout<<bst[queryPre(root,c)].key<<endl;
				//cout<<"成功"<<endl;
				break;
			}
			case 6:{
				cout<<bst[querySuc(root,c)].key<<endl;
				//cout<<"成功"<<endl;
				break;
			}
		}
	}
	/*
	root=insertnode(root,90);
	preorder(root);cout<<endl;
	root=insertnode(root,80);
	preorder(root);cout<<endl;
	root=insertnode(root,70);
	preorder(root);cout<<endl;
	root=insertnode(root,60);
	preorder(root);cout<<endl;
	root=insertnode(root,110);
	preorder(root);cout<<endl;
	root=insertnode(root,120);
	preorder(root);cout<<endl;
	root=insertnode(root,130);
	preorder(root);cout<<endl;
	root=insertnode(root,140);
	preorder(root);cout<<endl;
	//deletenode(root,100);
	preorder(root);cout<<endl;
	cout<<queryKey(4,root)<<endl;
	cout<<queryRank(90,root)<<endl;
	//cout<<bst[getprenode(bst[root].lc)].key<<endl;
	//cout<<bst[getsucnode(bst[root].rc)].key<<endl;
	cout<<bst[queryPre(root,85)].key<<endl;
	cout<<bst[querySuc(root,85)].key<<endl;*/
return 0;
}
void preorder(int tn){
	if(tn==0) return;
	cout<<"("<<bst[tn].key;
	preorder(bst[tn].lc);
	preorder(bst[tn].rc);
	cout<<")";
	return;
}
int LLwrong(int root){
	int oldlc=bst[root].lc;
	bst[root].lc=bst[oldlc].rc;
	bst[oldlc].rc=root;
	htcount(root);
	sizecount(root);
	htcount(oldlc);
	sizecount(oldlc);
	return oldlc;
}
int RRwrong(int root){
	int oldrc=bst[root].rc;
	bst[root].rc=bst[oldrc].lc;
	bst[oldrc].lc=root;
	htcount(root);
	sizecount(root);
	htcount(oldrc);
	sizecount(oldrc);
	return oldrc;
}
int LRwrong(int root){
	bst[root].lc=RRwrong(bst[root].lc);
	return LLwrong(root);
}
int RLwrong(int root){
	bst[root].rc=LLwrong(bst[root].rc);
	return RRwrong(root);
}
int insertnode(int curroot,int x){
	if(curroot==0){
		bst[tail]=node(x);
		tail++;
		return tail-1;
	}
	if(x==bst[curroot].key){
		bst[curroot].cnt++;
	}
	else if(x<bst[curroot].key){
		bst[curroot].lc=insertnode(bst[curroot].lc,x);
		if(bst[bst[curroot].lc].ht-bst[bst[curroot].rc].ht==2){
			if(bst[bst[bst[curroot].lc].lc].ht>bst[bst[bst[curroot].lc].rc].ht)
				curroot=LLwrong(curroot);
			else
				curroot=LRwrong(curroot); 
		} 
	}
	else{
		bst[curroot].rc=insertnode(bst[curroot].rc,x);
		if(bst[bst[curroot].rc].ht-bst[bst[curroot].lc].ht>=2){
			if(bst[bst[bst[curroot].rc].rc].ht>bst[bst[bst[curroot].rc].lc].ht){
				curroot=RRwrong(curroot);	
			}	 
			else
				curroot=RLwrong(curroot); 
		} 
	}
	htcount(curroot);
	sizecount(curroot);
	return curroot;
}
int deletenode(int curroot,int key){
	if(bst[curroot].key==0) return curroot;
	if(bst[curroot].key==key){
		if(bst[curroot].cnt>1){
			bst[curroot].cnt--;
		}
		else if(bst[curroot].lc==0) return bst[curroot].rc;
		else{
			swap(bst[getprenode(bst[curroot].lc)].key,bst[curroot].key);
			bst[curroot].lc=deletenode(bst[curroot].lc,key);
		}
	}
	else if(bst[curroot].key>key) bst[curroot].lc=deletenode(bst[curroot].lc,key);
	else bst[curroot].rc=deletenode(bst[curroot].rc,key);
	htcount(curroot);//调整参数,维护 
	sizecount(curroot);
	curroot=fix(curroot);
	return curroot;
}
void htcount(int root){
	bst[root].ht=1+max(bst[bst[root].lc].ht,bst[bst[root].rc].ht);
}
void sizecount(int root){
	bst[root].size=bst[root].cnt+bst[bst[root].lc].size+bst[bst[root].rc].size;
}
void myswap(node &a,node &b){
	node x;
	x=a;
	a=b;
	b=x;
	return;
}
int getprenode(int root){
	if(bst[root].rc==0) return root;
	return getprenode(bst[root].rc);
}
int getsucnode(int root){
	if(bst[root].lc==0) return root;
	return getsucnode(bst[root].lc);
}
int fix(int curroot){
	if(bst[bst[curroot].lc].ht-bst[bst[curroot].rc].ht==2){
		if(bst[bst[bst[curroot].lc].lc].ht>bst[bst[bst[curroot].lc].rc].ht)
			curroot=LLwrong(curroot);
		else
			curroot=LRwrong(curroot); 
	} 
	if(bst[bst[curroot].rc].ht-bst[bst[curroot].lc].ht>=2){
		if(bst[bst[bst[curroot].rc].rc].ht>bst[bst[bst[curroot].rc].lc].ht){
			curroot=RRwrong(curroot);	
		}	 
		else
			curroot=RLwrong(curroot); 
	}
	htcount(curroot);
	sizecount(curroot);
	return curroot;
}
int queryRank(int key,int root){
	if(bst[root].key==key) return bst[bst[root].lc].size+1;
	else if(bst[root].key>key) return queryRank(key,bst[root].lc);
	else return queryRank(key,bst[root].rc)+bst[root].cnt+bst[bst[root].lc].size;
}
int queryKey(int rank,int root){
	if(bst[bst[root].lc].size<rank){
		rank-=bst[bst[root].lc].size;
		if(rank<=bst[root].cnt) return bst[root].key;
		else return queryKey(rank-bst[root].cnt-bst[bst[root].lc].size,bst[root].rc); 
	}
	else return queryKey(rank,bst[root].lc);
}
int queryPre(int root,int key){
	if(bst[root].key==key) return getprenode(bst[root].lc);
	else if(bst[root].key>key) return queryPre(bst[root].lc,key);
	else if(bst[root].rc==0||bst[getsucnode(bst[root].rc)].key>=key) return root;
	else return queryPre(bst[root].rc,key);
}
int querySuc(int root,int key){
	//cout<<root<<" "<<bst[root].key<<" "<<key<<bst[root].lc<<" "<<bst[root].rc<<endl;
	if(bst[root].key==key) return getsucnode(bst[root].rc);
	else if(bst[root].key<key) return querySuc(bst[root].rc,key);
	else if(bst[root].lc==0||bst[getprenode(bst[root].lc)].key<=key) return root;
	else return querySuc(bst[root].lc,key);
}

只有36分,只过了前两个和后两个点

2023/3/31 19:53
加载中...