萌新刚学 OI,0pts输出随机fhq求助
查看原帖
萌新刚学 OI,0pts输出随机fhq求助
922691
Hisy楼主2024/7/4 11:14
#include<cstdlib>
#include<cstdio>
#include<ctime>
#define mod 1000000
struct node{
	int left,right,size,randkey,value;
};
namespace treap1{
	node tree[1000001];
	int root,top;
	inline int newnode(int value){
		tree[++top].value=value;
		tree[top].size=1;
		tree[top].randkey=rand();
		return top;
	};
	inline void pushup(int root){
		tree[root].size=tree[tree[root].left].size+tree[tree[root].right].size+1;
		return;
	};
	inline void split(int root,int value,int &first,int &second){
		if(!root){
			first=second=0;
			return;
		};
		if(tree[root].value<=value){
			first=root;
			split(tree[root].right,value,tree[root].right,second);
			return;
		};
		second=root;
		split(tree[root].left,value,first,tree[root].left);
		return;
	};
	inline int merge(int first,int second){
		if(!first||!second){
			return first+second;
		};
		if(tree[first].randkey<tree[second].randkey){
			tree[first].right=merge(tree[first].right,second);
			pushup(first);
			return first;
		};
		tree[second].left=merge(first,tree[second].left);
		pushup(second);
		return second;
	};
	inline int find(int root,int value){
		if(value<=tree[tree[root].left].size){
			return find(tree[root].left,value);
		};
		if(value==tree[tree[root].left].size+1){
			return root;
		};
		return find(tree[root].right,value-tree[tree[root].left].size-1);
	};
	inline void insert(int value){
		int first,second;
		split(root,value,first,second);
		root=merge(merge(first,newnode(value)),second);
		return;
	};
	inline void erase(int value){
		int first,second,third;
		split(root,value,first,third);
		split(first,value-1,first,second);
		second=merge(tree[second].left,tree[second].right);
		root=merge(merge(first,second),third);
		return;
	};
	inline int prev(int value){
		int first,second,result;
		split(root,value-1,first,second);
		result=tree[find(first,tree[first].size)].value;
		root=merge(first,second);
		return result;
	};
	inline int next(int value){
		int first,second,result;
		split(root,value,first,second);
		result=tree[find(second,1)].value;
		root=merge(first,second);
		return result;
	};
	inline void init(void){
		insert(1073741823);
		insert(-1073741824);
	};
	inline int size(void){
		return tree[root].size-2;
	};
};
namespace treap2{
	node tree[1000001];
	int root,top;
	inline int newnode(int value){
		tree[++top].value=value;
		tree[top].size=1;
		tree[top].randkey=rand();
		return top;
	};
	inline void pushup(int root){
		tree[root].size=tree[tree[root].left].size+tree[tree[root].right].size+1;
		return;
	};
	inline void split(int root,int value,int &first,int &second){
		if(!root){
			first=second=0;
			return;
		};
		if(tree[root].value<=value){
			first=root;
			split(tree[root].right,value,tree[root].right,second);
			return;
		};
		second=root;
		split(tree[root].left,value,first,tree[root].left);
		return;
	};
	inline int merge(int first,int second){
		if(!first||!second){
			return first+second;
		};
		if(tree[first].randkey<tree[second].randkey){
			tree[first].right=merge(tree[first].right,second);
			pushup(first);
			return first;
		};
		tree[second].left=merge(first,tree[second].left);
		pushup(second);
		return second;
	};
	inline int find(int root,int value){
		if(value<=tree[tree[root].left].size){
			return find(tree[root].left,value);
		};
		if(value==tree[tree[root].left].size+1){
			return root;
		};
		return find(tree[root].right,value-tree[tree[root].left].size-1);
	};
	inline void insert(int value){
		int first,second;
		split(root,value,first,second);
		root=merge(merge(first,newnode(value)),second);
		return;
	};
	inline void erase(int value){
		int first,second,third;
		split(root,value,first,third);
		split(first,value-1,first,second);
		second=merge(tree[second].left,tree[second].right);
		root=merge(merge(first,second),third);
		return;
	};
	inline int prev(int value){
		int first,second,result;
		split(root,value-1,first,second);
		result=tree[find(first,tree[first].size)].value;
		root=merge(first,second);
		return result;
	};
	inline int next(int value){
		int first,second,result;
		split(root,value,first,second);
		result=tree[find(second,1)].value;
		root=merge(first,second);
		return result;
	};
	inline void init(void){
		insert(1073741823);
		insert(-1073741824);
	};
	inline int size(void){
		return tree[root].size-2;
	};
};
int main(int argc,char **argv){
	srand(time(0));
	treap1::init();
	treap2::init();
	int action,result=0;
	scanf("%d",&action);
	do{
		int operate,value;
		scanf("%d %d",&operate,&value);
		if(operate==0){
			if(treap1::size()==0){
				treap2::insert(value);
			}else{
				int prev=treap1::prev(value+1),next=treap1::next(value-1);
				if(value-prev<=next-value&&next!=1073741823&&prev!=-1073741824){
					(result+=value-prev)%=mod;
					treap1::erase(prev);
				}else{
					(result+=next-value)%=mod;
					treap1::erase(next);
				};
			};
		}else{
			if(treap2::size()==0){
				treap1::insert(value);
			}else{
				int prev=treap2::prev(value+1),next=treap2::next(value-1);
				if(value-prev<=next-value&&next!=1073741823&&prev!=-1073741824){
					(result+=value-prev)%=mod;
					treap2::erase(prev);
				}else{
					(result+=next-value)%=mod;
					treap2::erase(next);
				};
			};
		};
	}while(--action);
	printf("%d",result);
	return 0;
};
2024/7/4 11:14
加载中...