哇,只会fhq treap的我瑟瑟发抖,不能旋转,那怎么区间翻转啊。
QAQ
仔细一想(看了博客)发现十分简单,也还是暴力拆,只不过用tag标记,交换子树就好了还是很简单的吗
至于不会fhq treap的同学可以先点这里或上 Patrickpwq大佬的博客
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std;
const int maxn=100010;
int n,m,val[maxn],rnd[maxn],son[maxn][3],size[maxn],sum_p;
int tag[maxn],root=0;
inline void read(int &x)
{
x=0;
int f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
x*=f;
}
inline void update(int x)
{
size[x]=size[son[x][1]]+size[son[x][2]]+1;
if(x&&tag[x])
{
tag[x]^=1;
swap(son[x][1],son[x][2]);
tag[son[x][1]]^=1,tag[son[x][2]]^=1;
}
}
inline int newnode(int x)
{
++sum_p;size[sum_p]=1;
val[sum_p]=x;rnd[sum_p]=rand();
tag[sum_p]=0;
return sum_p;
}
int merge(int x,int y)
{
if(x==0||y==0) return x+y;
update(x),update(y);
if(rnd[x]<rnd[y])
{
son[x][2]=merge(son[x][2],y);
update(x);
return x;
}
else
{
son[y][1]=merge(x,son[y][1]);
update(y);
return y;
}
}
inline void split(int &x,int &y,int k,int pos)
{
if(!pos)
x=y=0;
else
{
update(pos);
if(k<=size[son[pos][1]])
{
y=pos;
split(x,son[pos][1],k,son[pos][1]);
}
else
{
x=pos;
split(son[pos][2],y,k-size[son[pos][1]]-1,son[pos][2]);
}
update(pos);
}
}
void rev(int l,int r)
{
int a,b,c,d;
split(a,b,r+1,root);
split(c,d,l,a);
tag[d]=1;
root=merge(merge(c,d),b);
}
void print(int x)
{
if(!x) return;
update(x);
print(son[x][1]);
if (val[x]>=1&&val[x]<=n) printf("%d ",val[x]);
print(son[x][2]);
}
int main()
{
srand((unsigned)time(NULL));
scanf("%d%d",&n,&m);
root=build(1,n+2);
for(register int i=1;i<=m;i++)
{
register int l,r;
scanf("%d%d",&l,&r);
rev(l,r);
}
print(root);
}