蒟蒻求助fhq treap 文艺平衡树 全WA

P3391 【模板】文艺平衡树

Griseo_nya @ 2021-01-13 16:36:39

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+1;
typedef pair<int,int> pr;
int tot,rt,n;
mt19937 randl(time(0)+(unsigned long long)(new char)); 
struct racism{
    int l,r,w,rank,size,tag;
}tre[maxn];
inline void pushup(int p){
    tre[p].size=tre[tre[p].l].size+tre[tre[p].r].size+1;
}
inline void pushdown(int u){
    swap(tre[u].l,tre[u].r);tre[tre[u].l].tag^=1;tre[tre[u].r].tag^=1;tre[u].tag=0;
}
inline pr split_by_value(int p,int w){
    if(!p)return make_pair(0,0);
    if(tre[p].tag)pushdown(p);
    if(tre[p].w<=w){
        pr s=split_by_value(tre[p].r,w);
        tre[p].r=s.first;
        pushup(p);
        return make_pair(p,s.second);
    }
    else{
        pr s=split_by_value(tre[p].l,w);
        tre[p].l=s.second;
        pushup(p);
        return make_pair(s.first,p);
    }
}
inline pr split_by_rank(int p,int w){
    if(!p)return make_pair(0,0);
    if(tre[p].tag)pushdown(p);
    if(tre[tre[p].l].size+1<=w){
        pr s=split_by_rank(tre[p].r,w-tre[tre[p].l].size-1);
        tre[p].r=s.first;
        pushup(p);
        return make_pair(p,s.second);
    }
    else {
        pr s=split_by_rank(tre[p].l,w);
        tre[p].l=s.second;
        pushup(p);
        return make_pair(s.first,p);
    }
}
inline int merge(int x,int y){
    if(!x||!y)return x+y;
    if(tre[x].rank<=tre[y].rank){
        if(tre[x].tag)pushdown(x);
        tre[x].r=merge(tre[x].r,y);
        pushup(x);
        return x;
    }
    else {
        if(tre[y].tag)pushdown(y);
        tre[y].l=merge(x,tre[y].l);
        pushup(y);
        return y;
    }
}
void print(int u){
    if(tre[u].tag)
        pushdown(u);
    if(tre[u].l)
        print(tre[u].l);
    printf("%d ",tre[u].w);
    if(tre[u].r)
        print(tre[u].r);
}
void insert(int w){
    tot++;
    tre[tot]=(racism){0,0,w,randl(),1,0};
    pr s=split_by_value(rt,w);
    rt=merge(merge(s.first,tot),s.second);
} 

//l,r,w,rank,size,tag
int main(){
    int m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        insert(i);
    }

    for(int i=1;i<=m;i++){
        int y,x;
        scanf("%d%d",&x,&y);
        pr s=split_by_rank(rt,y);
        pr s1=split_by_rank(s.first,x-1);
        swap(tre[s1.second].l,tre[s1.second].r);
        tre[s1.second].tag^=1;
        rt=merge(merge(s1.first,s1.second),s.second);
    }
    print(rt);
    return 0;
}

by feicheng @ 2021-01-14 19:34:24

pair 好丑


|