用指针写的Treap,题目室p3369,一直RE,调不出来,求助(弱弱)
查看原帖
用指针写的Treap,题目室p3369,一直RE,调不出来,求助(弱弱)
1241385
licongzhong2022楼主2024/8/9 14:34
#include<bits/stdc++.h>
using namespace std;
struct tnode {
	tnode* left;
	tnode* right;
	int val,fix,weight,size,lsize,rsize;
};
void up(tnode* root) {
	if(root->left==NULL&&root->right==NULL) {
		root->size=1;
		return;
	} else if(root->left==NULL&&root->right!=NULL) {
		root->size=root->right->size+1;
		return ;
	} else if(root->right==NULL&&root->left!=NULL) {
		root->size=root->left->size+1;
		return;
	}
	root->size=root->left->size+root->right->size+1;
}
tnode* left_rotate(tnode* root) {
	tnode* tmp=root->right;
	root->right=tmp->left;
	tmp->left=root;
	up(root);
	return tmp;
}
tnode* right_rotate(tnode* root) {
	tnode* tmp=root->left;
	root->left=tmp->right;
	tmp->right=root;
	up(root);
	return tmp;
}
tnode* insert(tnode* root,int val) {
	if(root==NULL) {
		tnode* t=new tnode();
		t->val=val;
		t->fix=rand()%19620817;
		t->lsize=0;
		t->rsize=0;
		t->size=1;
		return t;
	}
	if(val<root->val) {
		root->left=insert(root->left,val);
		root->lsize++;
	} else {
		root->right=insert(root->right,val);
		root->rsize++;
	}
	if(root->left!=NULL&&root->left->fix<root->fix) {
		root=right_rotate(root);
	}
	if(root->right&&root->right->fix<root->fix) {
		root=left_rotate(root);
	}
	up(root);
	return root;
}
void test(tnode* root) {
	if(root==NULL) {
		cout<<"lef"<<endl;
		return;
	}
	cout<<root->val<<endl;
	test(root->left);
	test(root->right);
}
tnode* delete_node(tnode* root,int val) {
	if(val<root->val) {
		root->left=delete_node(root->left,val);
	} else if(val>root->val) {
		root->right=delete_node(root->right,val);
	} else {
		if(root->left==NULL&&root->right==NULL) {
			return NULL;
		}
		if(root->left!=NULL&&root->right==NULL) {
			return root->left;
		}
		if(root->right!=NULL&&root->left==NULL) {
			return root->right;
		}
		if(root->right!=NULL&&root->left!=NULL) {
			if(root->right->fix<root->left->fix) {
				tnode* tmp=left_rotate(root);
				tmp->right=delete_node(tmp->left,val);
				return tmp;
			} else {
				tnode* tmp=right_rotate(root);
				tmp->left=delete_node(tmp->right,val);
				return tmp;
			}
		}
	}
}
int pre(tnode* root,int val) {
	if(root==NULL) {
		return -INT_MAX;
	}
	if(root->val>val) {
		return pre(root->left,val);
	} else {
		return max(root->val,pre(root->right,val));
	}
}
int beh(tnode* root,int val) {
	if(root==NULL) {
		return INT_MAX;
	}
	if(root->val<val) {
		return beh(root->right,val);
	} else {
		return min(root->val,beh(root->left,val));
	}
}
int find(tnode* root,int val) {
	if(root==NULL) {
		return 1;
	}
	if(root->val>=val) {
		return find(root->left,val);
	} else {
		if(root->left==NULL) {
			return find(root->right,val)+1;
		}
		return find(root->right,val)+root->left->lsize+1;
	}
}
int ask(tnode* root,int val) {
	if(root->left==NULL&&root->right==NULL) {
		return root->val;
	}
	if(root->left->lsize==val-1) {
		return root->val;
	}
	if(root->left->lsize>=val) {
		return ask(root->left,val);
	} else {
		return ask(root->right,val-root->left->lsize-1);
	}
}
int n;
int main() {
	cin>>n;
	int x,y;
	tnode* root=new tnode();
	root=NULL;
	for(int i=1; i<=n; i++) {
		scanf("%d%d",&x,&y);
		if(x==1) {
			root=insert(root,y);
		}
		if(x==2) {
			root=delete_node(root,y);
		}
		if(x==3) {
			cout<<find(root,y)<<'\n';
		}
		if(x==4) {
			cout<<ask(root,y)<<'\n';
		}
		if(x==5) {
			cout<<pre(root,y)<<'\n';
		}
		if(x==6) {
			cout<<beh(root,y)<<'\n';
		}
	}
	//test(root);
}

2024/8/9 14:34
加载中...