莫名RE

P3391 【模板】文艺平衡树

Farkas_W @ 2021-02-24 09:03:10

调了很久调不出来

#include<iostream>
#include<cstdio>
#define root ch[0][1]
#define re register int
#define il inline
#define inf 1e9+5
using namespace std;
il int read()
{
  int x=0,f=1;char ch=getchar();
  while(!isdigit(ch)&&ch!='-')ch=getchar();
  if(ch=='-')f=-1,ch=getchar();
  while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
  return x*f;
}
il void print(int x)
{
  if(x<0)putchar('-'),x=-x;
  if(x/10)print(x/10);
  putchar(x%10+'0');
}
const int N=1e6+5;
int n,m,fa[N],ch[N][2],val[N],a[N],tot,sum[N];
bool vis[N];
il int identify(int x){return x==ch[fa[x]][1];}
il void connect(int x,int f,int son){fa[x]=f;ch[f][son]=x;}
il void update(int x){sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+1;}
il int build(int l,int r,int f)
{
    if(l>r)return 0;
    int mid=l+r>>1,now=++tot;
    if(tot==1)root=now;
    fa[now]=f;
    sum[now]=1;
    val[now]=a[mid];
    ch[now][0]=build(l,mid-1,now);
    ch[now][1]=build(mid+1,r,now);
    update(now);
    return now;
}
il void pushdown(int x)
{
    if(!vis[x]||!x)return;
    vis[x]=0;
    swap(ch[x][1],ch[x][0]);
    vis[ch[x][0]]^=1;vis[ch[x][1]]^=1;
}
il int rotate(int x)
{
    int f=fa[x],g=fa[f],lx=identify(x),ly=identify(f),s=ch[x][lx^1];
    pushdown(g);pushdown(f);pushdown(x);
    connect(s,f,lx);connect(f,x,lx^1);connect(x,g,ly);
    update(f);update(x);
}
il void splay(int at,int to)
{
    to=fa[to];
    while(fa[at]!=to){
        int up=fa[at];
        if(fa[up]==to){rotate(at);return;}
        if(identify(at)==identify(up))rotate(up),rotate(at);
        else rotate(at),rotate(at);
    }
}
il int find(int x)
{
    int now=root;
    while(1){
        pushdown(now);int t=sum[ch[now][0]]+1;
        if(x<t)now=ch[now][0];
        else if(x==t){splay(now,root);return now;}
        else x-=t,now=ch[now][1];
    }
}
il void dfs(int x)
{
    pushdown(x);
    if(ch[x][0])dfs(ch[x][0]);
    if(val[x]<=n&&val[x]>0)print(val[x]),putchar(' ');
    if(ch[x][1])dfs(ch[x][1]);
}
signed main()
{
    n=read();m=read();
    for(re i=1;i<=n;i++)a[i+1]=i;
    a[1]=-inf;a[n+2]=inf;
    build(1,n+2,0);
    while(m--){
        int x=read()+1,y=read()+1;
        x=find(x-1);y=find(y+1);
        splay(x,root);splay(y,ch[root][1]);
        vis[ch[y][0]]^=1;
    }
    dfs(root);
    return 0;
}

测试点1的数据

本地调试没有RE


by w23c3c3 @ 2021-02-24 09:13:32

@Farkas_W rotate改成void


by Farkas_W @ 2021-02-24 09:14:47

@w23c3c3 过了,谢谢


|