0分求助

P3391 【模板】文艺平衡树

Dog_King_chen @ 2022-12-24 10:09:19

#include<bits/stdc++.h>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<queue>
#define inf 0x5F5F5F5F
#define il inline
#define ll long long
#define For(i,j,k) for(int i=(j); i<=(k); ++i)
#define ForD(i,j,k) for(int i=(j); i>=(k); --i)
#define pb push_back
#define mkp make_pair
#define pii pair<int, int>
//#define init(FILENAME) freopen(FILENAME ".in", "r", stdin);freopen(FILENAME ".out", "w", stdout)
#define mod 998244353
#define init(FILENAME) freopen(FILENAME ".in", "r", stdin);
using namespace std;
int n,m,root;
struct T{
    int lc,rc,fa,is;
}a[100010];
void out(){
    For(i,1,n+2) cout<<"    "<<i<<" "<<a[i].lc<<" "<<a[i].rc<<" "<<a[i].fa<<endl;
}
int build(int l,int r,int fa){
    if(l>r)return 0;
    int mid=(l+r)>>1;
    if(fa==0)root=mid;
    a[mid].fa=fa;
    a[mid].lc=build(l,mid-1,mid);
    a[mid].rc=build(mid+1,r,mid);
    return mid;
}
void pushdown(int node){
    if(a[node].is){
        a[a[node].lc].is^=1,a[a[node].rc].is^=1;
        int aba=a[node].lc;
        a[node].lc=a[node].rc;
        a[node].rc=aba;
    }
    return;
}
bool son(int node){return node==a[a[node].fa].lc?true:false;}//true:left;false:right
void rotate(int node){
    int f=a[node].fa,gf=a[f].fa;
    pushdown(node),pushdown(f);
    if(son(node)){
        int aba=a[node].rc;
        a[node].rc=f;
        a[f].lc=aba;
        a[aba].fa=f;
    }else{
        int aba=a[node].lc;
        a[node].lc=f;
        a[f].rc=aba;
        a[aba].fa=f;
    }
    if(son(f))a[gf].lc=node;
    else a[gf].rc=node;
    a[f].fa=node,a[node].fa=gf;
    return;
}
void splay(int node,int g){
    if(node==g)return;
//  cout<<root<<" "<<node<<" "<<g<<endl;
//  out();
    while(1){
        rotate(node);
        if(a[node].lc==g||a[node].rc==g)break;
    }
    if(g==root)root=node;
    return;
}
void solve(int l,int r){
    splay(l-1,root);
    splay(r+1,a[root].rc);
    a[a[a[root].rc].lc].is^=1;
    return;
}
void dfs(int node){
    if(!node) return;
    //cout<<node<<endl;
    pushdown(node);
    dfs(a[node].lc);
    if(node==1||node==n+2);
    else cout<<node-1<<" ";
    dfs(a[node].rc);
    return;
}
int main(){
    //init("splay");
    cin>>n>>m;
    build(1,n+2,0);
    For(i,1,m){
        int l,r;
        //cout<<i<<endl;
        cin>>l>>r;
        solve(l+1,r+1);
//      dfs(root);
//      cout<<endl;
//      out();
    }
    //out();
    dfs(root);
    return 0;
}

样例过了,但全部TLE

哪位dalao帮我看看哪里写挂了


|