玄关,Treap 40pts求调
查看原帖
玄关,Treap 40pts求调
970132
Hero_Broom楼主2024/8/13 14:30

Record

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e6;
struct node{
	node *l,*r;
	int val,fix,weight;
	int size,lsize,rsize;
};
void Update_Size(node*& root){
	if(root==NULL) return;
	int ans=root->weight;
	if(root->l!=NULL) ans+=root->l->size;
	if(root->r!=NULL) ans+=root->r->size;
	root->size=ans;
}
void Left_Rotate(node*& root){
	node* tmp=root->r;
	root->r=tmp->l;
	tmp->l=root;
	Update_Size(root);
	Update_Size(tmp);
	root=tmp;
}
void Right_Rotate(node*& root){
	node* tmp=root->l;
	root->l=tmp->r;
	tmp->r=root;
	Update_Size(root);
	Update_Size(tmp);
	root=tmp;
}
void Treap_Insert(node*& root,int v){
	if(root==NULL){
		root=new node();
		root->l=NULL,root->r=NULL;
		root->size=1;
		root->val=v;
		root->weight=1;
		root->fix=rand();
		return;
	}
	if(v<root->val){
		Treap_Insert(root->l,v);
		if(root->l->fix<root->fix) Right_Rotate(root);
		Update_Size(root);
	}
	else if(v>root->val){
		Treap_Insert(root->r,v);
		if(root->r->fix<root->fix) Left_Rotate(root);
		Update_Size(root);
	}else{
		root->size++;
		root->weight++;
		return;
	}
}
void Treap_Delete(node*& root,int v){
	if(root==NULL) return;
	if(root->val!=v){
		if(v<root->val) Treap_Delete(root->l,v);
		else Treap_Delete(root->r,v);
		Update_Size(root);
		return;
	}
	if(root->weight>1){
		root->weight--;
		root->size--;
		return;
	}
	node* tmp=root;
	if(root->l==NULL&&root->r==NULL){
		root=NULL;
		return;
	}
	if(root->l==NULL&&root->r!=NULL){
		root=tmp->r;
		return;
	}
	if(root->l!=NULL&&root->r==NULL){
		root=tmp->l;
		return;
	}
	if(root->l!=NULL&&root->r!=NULL){
		if(root->l->fix<root->r->fix){
			Right_Rotate(root);
			Treap_Delete(root->r,v);
		}else{
			Left_Rotate(root);
			Treap_Delete(root->l,v);
		}
		Update_Size(root);
		return;
	}
}
int prev_tmp,next_tmp;
int Find_Prev(node* root,int v){
	if(root==NULL) return -1;
	if(v<=root->val){
		if(root->l!=NULL) return Find_Prev(root->l,v);
	}else{
		prev_tmp=root->val;
		if(root->r!=NULL) Find_Prev(root->r,v);
		return prev_tmp;
	}
  return -1;
}
int Find_Next(node* root,int v){
	if(root==NULL) return -1;
	if(v>=root->val){
		if(root->r!=NULL) return Find_Next(root->r,v);
	}else{
		next_tmp=root->val;
		if(root->l!=NULL) Find_Next(root->l,v);
		return next_tmp;
	}
  return -1;
}
int Get_The_Kth_Node(node* root,int k){
	if(k==0) return 0;
	int tmp=root->l==NULL?0:root->l->size;
	if(k<=tmp) return Get_The_Kth_Node(root->l,k);
	else if(k<=tmp+root->weight) return root->val;
	else return Get_The_Kth_Node(root->r,k-(tmp+root->weight));
	return -1;
}
int Get_Rank(node* root,int v){
	int tmp=root->l==NULL?0:root->l->size;
	if(v==root->val) return tmp+1;
	else if(v<root->val) return (root->l==NULL?1:Get_Rank(root->l,v));
	else return (root->r==NULL?root->size+1:tmp+root->weight+Get_Rank(root->r,v));
}
int n,cmd,t,ans,_prev,_next;
node *cost,*pet;
signed main(){
	srand(time(0));
	scanf("%lld",&n);
	while(n--){
		scanf("%lld%lld",&cmd,&t);
		if(cmd==0){
			Treap_Insert(pet,t);
			prev_tmp=next_tmp=0;
			_prev=Find_Prev(cost,t),_next=Find_Next(cost,t);
//			printf("p=%d\tn=%d\n",_prev,_next);
			if(_prev==-1&&_next==-1) continue;
			if(abs(_prev-t)<=abs(_next-t)){
				ans=(ans+abs(_prev-t))%mod;
				Treap_Delete(cost,_prev);
				Treap_Delete(pet,t);
			}else{
				ans=(ans+abs(_next-t))%mod;
				Treap_Delete(cost,_next);
				Treap_Delete(pet,t);
			}
		}else{
			Treap_Insert(cost,t);
			prev_tmp=next_tmp=0;
			_prev=Find_Prev(pet,t),_next=Find_Next(pet,t);
			if(_prev==-1&&_next==-1) continue;
			if(abs(_prev-t)<=abs(_next-t)){
				ans=(ans+abs(_prev-t))%mod;
				Treap_Delete(cost,t);
				Treap_Delete(pet,_prev);
			}else{
				ans=(ans+abs(_next-t))%mod;
				Treap_Delete(cost,t);
				Treap_Delete(pet,_next);
			}
		}
	}
	printf("%lld\n",ans);
}
2024/8/13 14:30
加载中...