splay囚条,阳历没过
查看原帖
splay囚条,阳历没过
703115
d909RCA楼主2024/2/28 18:48
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct node
{
	int lson,rson,fa,v,nm,tun;
	void addn(int x)
	{
		v=x;
		lson=rson=fa=tun=0;
		nm=1;
	}
}t[100009];
int n,m,rt,tot;
void upd(int x)
{
	if(!x) return ;
	t[x].nm=t[t[x].lson].nm+t[t[x].rson].nm+1;
}
void pushdown(int x)
{
	if(!x) return ;
	if(t[x].tun)
	{
		t[t[x].lson].tun^=1;
		t[t[x].rson].tun^=1;
		t[x].tun=0;
		swap(t[x].lson,t[x].rson);
	}
}
bool son(int x)
{
	return t[t[x].fa].rson==x;
}
void adde(int x,int y,bool rt_or_lt)
{
	if(rt_or_lt) t[x].rson=y;
	else t[x].lson=y;
	t[y].fa=x;
}
void rot(int x)
{
	int fa=t[x].fa,fa_fa=t[fa].fa,rt_or_lt=son(x);
	adde(fa_fa,x,son(fa));
	(rt_or_lt)?adde(fa,t[x].lson,rt_or_lt):adde(fa,t[x].rson,rt_or_lt);
	adde(x,fa,!rt_or_lt);
	upd(fa);
}
void splay(int x,int fa)
{
	pushdown(x);
	if(fa==0) rt=x;
	while(t[t[x].fa].fa!=fa)
	{
		if(t[t[x].fa].fa==fa)
		{
			rot(x);
			break ;
		}
		if(son(x)==son(t[x].fa)) rot(t[x].fa);
		else rot(x);
		rot(x);
		break ;
	}
}
int build(int l,int r)
{
	if(l>r) return 0;
	int x=++tot,mid=(l+r)>>1;
	t[x].addn(mid);
	t[x].lson=build(l,mid-1);
	t[x].rson=build(mid+1,r);
	t[t[x].lson].fa=x;
	t[t[x].rson].fa=x;
	upd(x);
	return x;
} 
int find(int x,int y)
{
	pushdown(x);
	if(t[t[x].lson].nm+1<y) return find(t[x].rson,y-t[t[x].lson].nm-1);
	else if(t[t[x].lson].nm+1==y) return x;
	else return find(t[x].lson,y);
}
void dfs(int x)
{
	pushdown(x);
	if(t[x].lson) dfs(t[x].lson);
	if(t[x].v<=n&&t[x].v>=1) cout<<t[x].v<<" ";
	if(t[x].rson) dfs(t[x].rson);
} 
signed main()
{
	cin>>n>>m;
	rt=build(0,n+1);
	while(m--)
	{
		int l,r;
		cin>>l>>r;
		int x1=find(rt,l),x2=find(rt,r+2);
		splay(x1,0);
		splay(x2,x1);
		upd(rt);
		t[t[x2].lson].tun^=1;
		upd(t[x2].lson);
		upd(x2);
		upd(rt);
	}
	dfs(rt);
	return 0;
}
2024/2/28 18:48
加载中...