玄学MLE文艺平衡树Splay求调
  • 板块学术版
  • 楼主_Dolphin_
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/1/7 11:56
  • 上次更新2024/1/7 14:50:16
查看原帖
玄学MLE文艺平衡树Splay求调
638206
_Dolphin_楼主2024/1/7 11:56

link

#include<bits/stdc++.h>
#define afor(x,y,z) for(int x=y;x<=z;x++)
#define bfor(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
typedef long long ll;
typedef const int cint;
cint N=100010;
struct Splay{
	int root,tot,lson[N],rson[N],siz[N],mark[N];
	void pushup(int x) {
		siz[x]=siz[lson[x]]+siz[rson[x]]+1;
	}
	void pushdown(int x) {
		if(mark[x]) {
			mark[lson[x]]^=1;
			mark[rson[x]]^=1;
			mark[x]=0;
			swap(lson[x],rson[x]);
		}
	}
	void zig(int &x) {
		int y=lson[x];
		lson[x]=rson[y],rson[y]=x,x=y;
		pushup(rson[x]),pushup(x);
	}
	void zag(int &x) {
		int y=rson[x];
		rson[x]=lson[y],lson[y]=x,x=y;
		pushup(lson[x]),pushup(x);
	}
	void splay(int x,int &y) {
		if(x==y) return;
		if(x==lson[y]) zig(y);
		else if(x==rson[y]) zag(y);
		else {
			if(x<y) {
				if(x<lson[y])
					splay(x,lson[lson[y]]),zig(y),zig(y);
				else splay(x,rson[lson[y]]),zag(lson[y]),zig(y);
			}
			else {
				if(x>rson[y])
					splay(x,rson[rson[y]]),zag(y),zag(y);
				else splay(x,lson[rson[y]]),zig(rson[y]),zag(y);
			}
		}
	}
	void insert(int &x,int y) {
		if(!x) x=y,siz[x]=1,splay(x,root);
		else if(x>y) insert(lson[x],y);
		else if(x<y) insert(rson[x],y);
		else siz[x]++,splay(x,root);
	}
	int find(int x,int y) {
		if(!x) return 0;
		pushdown(x);
		if(siz[lson[x]]+1==y) return x;
		else if(siz[lson[x]]>=y) return find(lson[x],y);
		return find(rson[x],y-siz[lson[x]]-1);
	}
	void reverse(int l,int r) {
		int x=find(root,l),y=find(root,r+2);
		splay(x,root),splay(y,rson[x]);
		mark[lson[y]]^=1;
	}
}tr;
int n,m,l,r;
int main() {
	scanf("%d%d",&n,&m);
	afor(i,1,n+2) tr.insert(tr.root,i);
	afor(i,1,m) {
		scanf("%d%d",&l,&r);
		tr.reverse(l,r);
	}
	afor(i,2,n+1) printf("%d ",tr.find(tr.root,i)-1);
	return 0;
}
2024/1/7 11:56
加载中...