求助无旋Treap
查看原帖
求助无旋Treap
541069
SuperCowHorse楼主2022/8/26 20:15

RT,第44行总是报错不知道为什么。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,Q,rt;
int tot,ch[maxn][2],siz[maxn],val[maxn],wei[maxn];
bool tag[maxn];
void pushdown(int p){
	swap(ch[p][0],ch[p][1]);
	if(ch[p][0]) tag[ch[p][0]]^=1;
	if(ch[p][1]) tag[ch[p][1]]^=1;
	tag[p]=0;
}
int newnode(int v){
	wei[++tot]=rand();
	val[tot]=v;
	siz[tot]=1;
	return tot;
}
void pushup(int x){siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}
int merge(int x,int y){
	if(!x||!y) return x|y;
	if(wei[x]<wei[y]){
		if(tag[x]) pushdown(x);
		ch[x][1]=merge(ch[x][1],y);
		pushup(x);
		return x;
	}
	else{
		if(tag[y]) pushdown(y);
		ch[y][0]=merge(x,ch[y][0]);
		pushup(y);
		return y;
	}
}
void split(int p,int v,int &x,int &y){
	if(!p){x=y=0;return;}
	if(tag[p]) pushdown(p);
	if(siz[ch[p][0]]+1<=v){
		x=p;
		split(ch[p][1],v-siz[ch[p][0]]-1,ch[p][1],y);
	}
	else{
		y=p;
		split(ch[p][0],v,x,ch[p][0]);//here
	}
	pushup(p);
}
void print(int p){
	if(!p) return;
	if(tag[p]) pushdown(p);
	if(ch[p][0]) print(ch[p][0]);
	printf("%d ",val[p]);
	if(ch[p][1]) print(ch[p][1]);
}
signed main(){
	scanf("%d%d",&n,&Q);
	for(int i=1;i<=n;++i)
		rt=merge(rt,newnode(i));
	while(Q--){
		int l,r,x,y,z;
		scanf("%d%d",&l,&r);
		split(rt,r,x,z);
		split(rt,l-1,x,y);
		tag[y]^=1;
		rt=merge(merge(x,y),z);
	}
	print(rt);
	return 0;
}
2022/8/26 20:15
加载中...