萌新求助,为啥后3个点都被卡T了,加了一堆优化都没用

P3391 【模板】文艺平衡树

Register @ 2020-01-09 20:35:22

#include <cstdio>
int t,n,m,rt;
inline int read(){
    char ch=getchar();int res=0,w=1;
    while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {res=res*10+ch-'0';ch=getchar();}
    return res*w;
}
struct node{
    int v,fa,sz,ad,c[2];
}a[100001];
inline void Swap(int&x,int&y) {int te=x;x=y;y=te;}
inline void pushup(int pos) {a[pos].sz=a[a[pos].c[0]].sz+a[a[pos].c[1]].sz+1;}
inline void pushdown(int pos){
    if(a[pos].ad) {a[pos].ad=0;a[a[pos].c[0]].ad^=1;a[a[pos].c[1]].ad^=1;Swap(a[pos].c[0],a[pos].c[1]);}
}
inline void add(int f,int v) {a[++n].v=v;a[n].sz=1;a[n].sz=1;a[n].fa=f;a[f].c[v>a[f].v]=n;}
inline void connect(int f,int s,int x) {a[f].c[s]=x;a[x].fa=f;}
inline void rotate(int x) {int y=a[x].fa,z=a[y].fa;int u=(a[y].c[0]==x)?0:1,v=(a[z].c[0]==y)?0:1;connect(y,u,a[x].c[u^1]);connect(x,u^1,y);connect(z,v,x);pushup(x);pushup(y);}
inline void splay(int x,int to){
    int p=a[to].fa;
    while(a[x].fa!=p)
    {
        int y=a[x].fa,z=a[y].fa;
        if(a[x].fa!=to) ((a[y].c[0]==x)^(a[z].c[0]==y))?rotate(x):rotate(y);
        rotate(x);
    }
    if(!p) rt=x;
}
void ins(int now,int x,int f){
    if(!n) {add(0,x);rt=n;return;}
    if(!now) {add(f,x);splay(n,rt);return;}
    a[now].sz++;ins(a[now].c[x>a[now].v],x,now);
}
inline int kth(int x){
    int now=rt;
    while(1)
    {
        pushdown(now);
        if(a[a[now].c[0]].sz>=x) now=a[now].c[0];
        else if(a[a[now].c[0]].sz+1==x) return now;
        else {x-=a[a[now].c[0]].sz+1;now=a[now].c[1];}
    }
}
inline void write(int now){
    pushdown(now);
    if(a[now].c[0]) write(a[now].c[0]);
    if(a[now].v>1&&a[now].v<t+2) printf("%d ",a[now].v-1);
    if(a[now].c[1]) write(a[now].c[1]);
}
int main(){
    t=read();m=read();
    for(register int i=1;i<=t+2;i++) ins(rt,i,0);
    while(m--) {int l=read(),r=read();l=kth(l);r=kth(r+2);splay(l,rt);splay(r,a[l].c[1]);a[a[r].c[0]].ad^=1;}
    write(rt);
    return 0;
}

by Register @ 2020-01-09 20:37:18

好了,我发现数组开小了


|