求助,在线IDE能过题交TLE

P3391 【模板】文艺平衡树

chengxx @ 2021-07-31 15:48:33

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#define ll long long 
using namespace std;
struct Splay{
    ll l,r,dad;
    ll rev,siz;
}tree[100010];
ll n,m,L,R;
ll build(ll ,ll ,ll );
void print(ll );
void rot(ll );
void splay(ll ,ll );
void updata(ll );
void pushdown(ll );
ll find(ll );
int main(){
    scanf("%lld %lld",&n,&m);
    tree[n+2].l=build(0,n+1,n+2);
    tree[n+2].r=tree[n+2].dad=tree[n+2].rev=-1;
    while(m--){
        scanf("%lld %lld",&L,&R);
        L=find(L);
        R=find(R+2);
        splay(L,n+2);
        splay(R,L);
        tree[tree[R].l].rev=-tree[tree[R].l].rev;
    }
//  for(ll i=0;i<=n+2;i++){
//      printf("%-3lld%-3lld%-3lld%-3lld\n",tree[i].l,tree[i].r,tree[i].dad,tree[i].rev);
//  } 
    print(n+2);
    printf("\n");
    return 0;
}
void pushdown(ll dian){
    if(tree[dian].rev==-1)return;
    tree[dian].rev=-1;
    swap(tree[dian].l,tree[dian].r);
    if(tree[dian].l!=-1)tree[tree[dian].l].rev=-tree[tree[dian].l].rev;
    if(tree[dian].r!=-1)tree[tree[dian].r].rev=-tree[tree[dian].r].rev;
    return;
}
void splay(ll dian,ll to){
    ll fa=tree[dian].dad,gr=tree[tree[dian].dad].dad;
    ll A,B;
    while(gr!=to&&fa!=to){
        if(tree[gr].l==fa)A=-1;
        else A=1;
        if(tree[fa].l==dian)B=-1;
        else B=1;
        if(A==B)rot(fa);
        else rot(dian);
        rot(dian);
        fa=tree[dian].dad,gr=tree[tree[dian].dad].dad;
    }
    if(fa!=to)rot(dian);
    return;
}
void rot(ll dian){
    if(tree[dian].dad==n+2)return;
    ll fa=tree[dian].dad,gr=tree[tree[dian].dad].dad;
    if(dian==tree[fa].l){
        tree[fa].l=tree[dian].r;
        tree[tree[dian].r].dad=fa;
        tree[dian].r=fa;
    }else{
        tree[fa].r=tree[dian].l;
        tree[tree[dian].l].dad=fa;
        tree[dian].l=fa;
    }
    tree[fa].dad=dian;
    tree[dian].dad=gr;
    if(tree[gr].l==fa){
        tree[gr].l=dian;
    }else{
        tree[gr].r=dian;
    }
    updata(fa);
    updata(dian);
    return;
}
ll find(ll to){
    ll dian=tree[n+2].l;
    ll shul=0;
    while(true){
        pushdown(dian);
        shul=0;
        if(tree[dian].l!=-1)shul+=tree[tree[dian].l].siz;
        if(to<=shul){
            dian=tree[dian].l;
        }else if(to==shul+1){
            return dian;
        }else{
            to-=shul+1;
            dian=tree[dian].r;
        }
    }
}
void print(ll dian){
    if(dian<0)return;
    pushdown(dian);
    print(tree[dian].l);
    if(dian>0&&dian<=n)printf("%lld ",dian);
    print(tree[dian].r);
}
ll build(ll l,ll r,ll fa){
    if(l>r)return -1;
    if(l==r){
        tree[l].dad=fa;
        tree[l].rev=-1;
        tree[l].l=tree[l].r=-1;
        tree[l].siz=1;
        return l;
    }
    ll mid=(l+r)>>1;
    tree[mid].dad=fa;
    tree[mid].rev=-1;
    tree[mid].l=build(l,mid-1,mid);
    tree[mid].r=build(mid+1,r,mid);
    updata(mid);
    return mid;
}
void updata(ll dian){
    tree[dian].siz=1;
    if(tree[dian].l!=-1)tree[dian].siz+=tree[tree[dian].l].siz;
    if(tree[dian].r!=-1)tree[dian].siz+=tree[tree[dian].r].siz;
    return;
}

|