FHQ,样例也不对,求助

P3391 【模板】文艺平衡树

KohaD_SEGA @ 2023-11-04 22:38:30

#include <bits/stdc++.h>
#define N 100007
#define L(x)    tree[(x)].left
#define R(x)    tree[(x)].right
using namespace std;

random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dis(0, INT_MAX);
inline int Rdom()
{
    return dis(gen);
}

struct BTree
{
    int left;
    int right;
    int size;
    bitset<1> lazytag;
    int num;
    int val;
}tree[N];

int TOT;
int root;
inline void mkpt(int x)
{
    tree[++TOT].num=x;
    tree[TOT].lazytag.set(0,false);
    tree[TOT].size=1;
    tree[TOT].val=Rdom();
    L(TOT)=0;
    R(TOT)=0;
}

inline void push_up(int p)
{
    tree[p].size=1+tree[L(p)].size+tree[R(p)].size;
}
inline void push_down(int p)
{
    tree[p].lazytag.flip();
    tree[L(p)].lazytag.flip();
    tree[R(p)].lazytag.flip();
    swap(L(p),R(p));
}

void split(int p,int rk,int& lef,int& rig)
{
    if(p==0)
    {
        lef=rig=0;
        return;
    }

    if(tree[p].lazytag.to_ulong())  push_down(p);

    if(tree[L(p)].size+1<rk)
    {
        lef=p;
        rk-=tree[L(p)].size+1;
        split(R(p),rk,lef,rig);
    }
    else
    {
        rig=p;
        split(L(p),rk,lef,rig);
    }
    push_up(p);
}

int merge(int lef,int rig)
{
    int ans;
    if(lef==0 || rig==0)    return lef+rig;
    if(tree[lef].val>=tree[rig].val)
    {
        if(tree[lef].lazytag.to_ulong())    push_down(lef);
        ans=lef;
        merge(lef,L(rig));
    }
    else
    {
        if(tree[rig].lazytag.to_ulong())    push_down(rig);
        ans=rig;
        merge(R(lef),rig);
    }
    push_up(ans);
    return ans;
}

inline void addin(int num)
{
    mkpt(num);
    int lef,rig;
    split(root,num-1,lef,rig);
    root=merge(merge(lef,TOT),rig);
}

inline void build(int n)
{
    for(int i=1;i<=n;i++)   addin(i);
}

inline void printout(int ROOT)
{
    if(!ROOT)   return;
    if(tree[ROOT].lazytag.to_ulong())   push_down(ROOT);
    printout(L(ROOT));
    printf("%d ",tree[ROOT].num);
    printout(R(ROOT));
}
inline void rev(int l,int r)
{
    int lef,rig,pos;
    split(root,l-1,lef,rig);
    split(rig,r-l+1,pos,rig);
    tree[pos].lazytag.flip();
    root=merge(merge(lef, pos), rig);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(n);
    int l,r;
    while(m--)
    {
        scanf("%d%d",&l,&r);
        rev(l,r);
    }
    printout(root);
    return 0;
}

|