【BZOJ】【P1552&3506】【Cqoi2014】【排序机械臂】【题解】【Treap】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3506 http://www.lydsy.com/JudgeOnline/problem.php?id=1552

区间min,区间reverse

注意要以原序列的位置为第二关键字

Code:

#include
#include
#include
#include
#include
using namespace std;
struct node{
	int val,key,rev,size,id;
	node *c[2],*mn;
	void split(int ned,node *&p,node *&q);
	node(int _val=0,int _id=0,node *C=0){
		key=rand();val=_val;rev=0;size=1;
		c[0]=c[1]=C;mn=this;id=_id;
	}
	void makerev(){
		rev^=1;
		swap(c[0],c[1]);
		if(mn!=this&&c[0]->mn->val<=c[1]->mn->val){
			if(c[0]->mn->idmn->id)mn=c[0]->mn;
		}
	}
	void pushdown(){
		if(rev){
			c[0]->makerev();
			c[1]->makerev();
			rev^=1;
		}
	}
	node* rz(){
		size=c[0]->size+1+c[1]->size;
		if(c[0]->mn->valmn->val
		||(c[0]->mn->val==c[1]->mn->val&&c[0]->mn->idmn->id))
		mn=c[0]->mn;else mn=c[1]->mn;
		if(valval||(val==mn->val&&idid))
		mn=this;
		return this;
	}
}*root,*Null;
node *merge(node *p,node *q){
	if(p==Null)return q->rz();
	if(q==Null)return p->rz();
	p->pushdown();q->pushdown();
	if(p->keykey){
		p->c[1]=merge(p->c[1],q);
		return p->rz();
	}else{
		q->c[0]=merge(p,q->c[0]);
		return q->rz();
	}
}
void node::split(int ned,node *&p,node *&q){
	if(this==Null){p=q=Null;return;}
	pushdown();
	if(c[0]->size>=ned){
		c[0]->split(ned,p,q);
		c[0]=Null;rz();
		q=merge(q,this);
	}else{
		c[1]->split(ned-c[0]->size-1,p,q);
		c[1]=Null;rz();
		p=merge(this,p);
	}
}
node *p,*q,*r,*s;
int n;
int getint(){
	int res=0;char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))res=res*10+c-'0',c=getchar();
	return res;
}
int main(){
	Null=new node(INT_MAX,0);
	Null->key=INT_MAX;Null->size=0;
	Null->c[0]=Null->c[1]=Null;
	root=Null;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		root=merge(root,new node(x,i,Null));
	}
	for(int i=1;i<=n;i++){
		root->split(i-1,p,q);
		int ps=i;r=q;
		while(r->mn!=r){
			r->pushdown();
			if(r->c[0]->mn->id==r->mn->id)r=r->c[0];
			else ps+=r->c[0]->size+1,r=r->c[1];
		}ps+=r->c[0]->size;
		printf("%d%c",ps," \n"[i==n]);
		q->split(ps-i+1,r,s);
		r->makerev();
		root=merge(p,merge(r,s));
	}
	return 0;
}


你可能感兴趣的:(OI)