萌新初学OI,求调Splay

P3391 【模板】文艺平衡树

AKKKKKKKKKK @ 2022-04-30 20:13:03

RT,样例超时了,求各位大佬帮调……

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
inline int R(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x){
    if(x<0){x=-x;putchar('-');}
    int y=0;char z[70];
    while(x||!y){z[y++]=x%10+48;x/=10;}
    while(y--)putchar(z[y]);putchar(32);
}
inline char read(){
    char ch=getchar();
    while(1) ch=getchar();
    return ch;
}
const int N=1e5+5,inf=0x7fffffff;
int n,m,root,a[N],tmp;
int fa[N],son[N][2],siz[N],val[N],laz[N],cnt[N];
void pushup(int p){
    if(!p)
        siz[p]=siz[son[p][0]]+siz[son[p][1]]+cnt[p];
}
void pushdown(int p){
    if(p&&laz[p]){
        laz[son[p][0]]^=1,laz[son[p][1]]^=1;
        swap(son[p][0],son[p][1]);
        laz[p]=0;
    }
}
int get(int x){
    return x==son[fa[x]][1];
}
int kth(int x){
    int now=root;
    while(1){
        pushdown(now);
        if(x<=siz[son[now][0]]) now=son[now][0];
        else if(x==siz[son[now][0]]+1) return now;
        else{
            x-=siz[son[now][0]]+1;
            now=son[now][1];
        }
    }
}
int build(int l,int r,int rt){
    if(l>r) return 0;
    int mid=l+r>>1,p=++tmp;
    fa[p]=rt,siz[p]++,cnt[p]++,val[p]=a[mid];
    son[p][0]=build(l,mid-1,p);
    son[p][1]=build(mid+1,r,p);
    pushup(p);
    return p;
}
void rotate(int x){
    int y=fa[x],z=fa[y],op=get(x);
    pushdown(x);
    pushdown(y);
    son[y][op]=son[x][op^1];
    if(son[x][op^1]) fa[son[x][op^1]]=y;
    son[x][op^1]=y;
    fa[y]=x;
    fa[x]=z;
    son[x][op^1]=y;
    if(z) son[z][y==son[z][1]]=x;
    pushup(y);
    pushup(x);
}
void splay(int x,int sign){
    int cnt=fa[x];
    for(;(cnt=fa[x])!=sign;rotate(x)) 
        if(fa[cnt]!=sign) 
            rotate(get(x)==get(cnt)?cnt:x);
    if(!sign) root=x;
}
void Reverse(int l,int r){
    l--,r++;
    l=kth(l),r=kth(r);
    splay(l,0);
    splay(r,l);
    int cnt=son[root][1];
    cnt=son[cnt][0];
    laz[cnt]^=1;
}
void dfs(int p){
    pushdown(p);
    if(son[p][0]) dfs(son[p][0]);
    if(val[p]!=-inf&&val[p]!=inf) write(p);
    if(son[p][1]) dfs(son[p][1]);
}
int main(){
    n=R(),m=R();
    a[1]=-inf,a[n+2]=inf;
    for(int i=1;i<=n;i++) a[i+1]=i;
    root=build(1,n+2,root);
    for(int _=1,l,r;_<=m;_++){
        l=R(),r=R();
        Reverse(l+1,r+1);
    }
    dfs(root);
}

by AKKKKKKKKKK @ 2022-05-03 11:06:46

运行到第二个操作的时候超时了


by 听取MLE声一片 @ 2022-05-03 11:10:02

8成是RE或死循环,仔细检查一下数组


|