[BZOJ3506]CQOI2014排序机械臂

    挺有意思的。。看到区间翻转好像很容易想到splay,想到之后就没什么难度了。。每个节点维护一个子树最小值,没次删去一个点,该打标记打标记就是。。为了处理同大小的数要以初始序号为第二关键字。。

#include
#include
#define N 100005
#define lc c[x][0]
#define rc c[x][1]
#define inf 2000000005
using namespace std;
struct Num{
	int n,xu;
}num[N],minn[N];
int n,i,nd=0,root=0,rk,t,q[N],c[N][2],size[N],pre[N],rev[N];
bool operator==(Num a,Num b){return a.n==b.n&&a.xu==b.xu;}
bool operator<(Num a,Num b)
{
	if (a.n!=b.n) return a.nr) return;
	int mid=(l+r)>>1;
	x=++nd;
	num[x].n=q[mid];num[x].xu=mid;pre[x]=fa;
	build(lc,l,mid-1,x);build(rc,mid+1,r,x);
	update(x);
}
void rot(int x,int kind)
{
	int y=pre[x],z=pre[y];down(y);
	pre[c[x][kind]]=y;c[y][!kind]=c[x][kind];
	pre[y]=x;c[x][kind]=y;
	pre[x]=z;if (z) c[z][c[z][1]==y]=x;
	update(y);update(x); 
}
void splay(int x,int goal)
{
	int y,z,kind;
	while (pre[x]!=goal)
		if (pre[y=pre[x]]==goal) rot(x,c[y][0]==x);
		else
		{
			int z=pre[y],kind=(c[z][0]==y);
			down(z);
			if (c[y][kind]==x) rot(x,!kind);else rot(y,kind);
			rot(x,kind);
		}
	if (goal==0) root=x;
}
int merge(int s1,int s2)
{
	int x=s1;down(x);
	while (c[x][1]) x=c[x][1],down(x);
	splay(x,pre[s1]);
	c[x][1]=s2;pre[s2]=x;
	update(x);
	return x;
}
void del(int x)
{
	splay(x,0);down(x);
	if (lc*rc==0) root=lc+rc;
	else root=merge(lc,rc);
	pre[root]=0;
}
int get(int x)
{
	down(x);
	if (minn[x]==num[x]){rk+=size[lc];return x;}
	if (minn[lc]


你可能感兴趣的:(BZOJ,splay)