文艺平衡树(区间翻转)

哇,只会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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值