蒟蒻fhqtreap,样例过,全WA,求调!
查看原帖
蒟蒻fhqtreap,样例过,全WA,求调!
856459
yangjunhan1楼主2023/8/17 14:14
#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
int h[N],sum[N],tot,sz[N],v[N],ls[N],rs[N],n,m,root;
bool lazy[N];
void szup(int x){
	sz[x]=sz[ls[x]]+sz[rs[x]]+1;
}
void lazydown(int x){
	if(!lazy[x])	return ;
	swap(ls[x],rs[x]);
	lazy[ls[x]]=!lazy[ls[x]];
	lazy[rs[x]]=!lazy[rs[x]];
	lazy[x]=0;
}
int hb(int l,int r){
	if(!l || !r)	return l+r;
	if(h[l]<h[r]){
		lazydown(l);
		rs[l]=hb(rs[l],r);
		szup(l);
		return l;
	}
	else{
		lazydown(r);
		ls[r]=hb(l,ls[r]);
		szup(r);
		return r;
	}
}
void jd(int x){
	tot++;
	v[tot]=x;
	h[tot]=rand();
	if(!root)	root=tot;
	else	root=hb(root,x);
	szup(tot);
}
void fl(int rt,int &l,int &r,int x){
	if(!rt){
		l=r=0;
		return ;
	}
	lazydown(rt);
	if(v[rt]<=x){
		l=rt;
		fl(rs[rt],rs[l],r,x);
		szup(l);	
	} 
	else{
		r=rt;
		fl(ls[rt],l,ls[r],x);
		szup(r);
	}
}
void prin(int rt){//中序 
	if(!rt)	return ;
	lazydown(rt);
	prin(ls[rt]);
	cout<<v[rt]<<" ";
	prin(rs[rt]);
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)	jd(i);
	while(m--){
		int l,r,x,y,z;
		cin>>l>>r;
		fl(root,x,y,l-1);//1~r r+1~n x=r,y=n
		fl(y,y,z,r);//l~r r+1~n
		lazy[y]=!lazy[y];
		root=hb(x,hb(y,z));
	}
	prin(root);
	return 0;
}
2023/8/17 14:14
加载中...