fhqtreap 维护区间翻转

#include<bits/stdc++.h>
using namespace std;
struct node{
	int l;
	int r;
	int val;
	int key;
	int sz;
	int lz; 
}fhq[100010];
int root;
int cnt;
void update(int now)
{
	fhq[now].sz = 1+fhq[fhq[now].l].sz+fhq[fhq[now].r].sz;
}
int newnode(int val)
{
	fhq[++cnt].val=val;
	fhq[cnt].key=rand();
	fhq[cnt].sz=1;
	return cnt;
}
void pushdown(int now)
{
	if(fhq[now].lz)
	{
		swap(fhq[now].l,fhq[now].r); 
		//if(fhq[now].l)
		fhq[fhq[now].l].lz^=1;
		//if(fhq[now].r)
		fhq[fhq[now].r].lz^=1;
		fhq[now].lz=0;
	}
}
void split(int now,int size,int &x,int &y)
{
	if(!now)
	{
		x=0,y=0;
		return ;
	}
	pushdown(now);
	if(size>fhq[fhq[now].l].sz)
	{
		x=now;
		split(fhq[now].r,size-fhq[fhq[now].l].sz-1,fhq[now].r,y);
	}
	else
	{
		y=now;
		split(fhq[now].l,size,x,fhq[now].l);
	}
	update(now);
int merge(int x,int y)
{
	if(!x || !y)
	return x+y;
	if(fhq[x].key<fhq[y].key)
	{
		pushdown(x);
		fhq[x].r=merge(fhq[x].r,y);
		update(x);
		return x;
	}
	else
	{
		pushdown(y);
		fhq[y].l=merge(x,fhq[y].l);
		update(y);
		return y;
	}
}
void dfs(int now)
{
	if(!now)	return ;
	pushdown(now);
	dfs(fhq[now].l);
	cout<<fhq[now].val<<" ";
	dfs(fhq[now].r);
}

void reverse(int l,int r)
{
	int x,y,z;
	split(root,l-1,x,y);
	split(y,r-l+1,y,z);
	fhq[y].lz^=1;
	root=merge(x,merge(y,z));
}
int main()
{
 		srand(time(NULL));
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;	
		cin>>x;
	root=merge(root,newnode(x));
	}
	while(m--)
	{
		int l,r;
		cin>>l>>r;
		reverse(l,r);
	}
	dfs(root);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值