QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#671092#21403. 文艺平衡树jager59WA 0ms3804kbC++142.1kb2024-10-24 10:49:142024-10-24 10:49:16

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 10:49:16]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3804kb
  • [2024-10-24 10:49:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}
int n,m,rt;
struct tree{
    int fa,siz,v,son[2],tag;
}t[N];
inline void up(int p){
    t[p].siz=t[t[p].son[0]].siz+1+t[t[p].son[1]].siz;
}
inline int build(int fa,int l,int r){
    if(l>r)return 0;
    int mid=l+r>>1;
    t[mid+1].fa=fa;t[mid+1].v=mid;
    t[mid+1].son[0]=build(mid+1,l,mid-1),t[mid+1].son[1]=build(mid+1,mid+1,r);
    up(mid+1);
    return mid+1;
}
inline void cg(int p){
    swap(t[p].son[0],t[p].son[1]),t[p].tag^=1;
}
inline void spread(int p){
    if(t[p].tag){
        if(t[p].son[0])cg(t[p].son[0]);
        if(t[p].son[1])cg(t[p].son[1]);
        t[p].tag=0;
    }
}
inline void rotate(int x){
    int y=t[x].fa,z=t[y].fa,k=(t[y].son[1]==x);
    spread(y);spread(x);
    t[x].fa=z,t[z].son[y==t[z].son[1]]=x;
    t[y].son[k]=t[x].son[k^1],t[t[x].son[k^1]].fa=y;
    t[x].son[k^1]=y,t[y].fa=x;
    up(y);up(x);
}
inline void splay(int x,int goal){
    while(t[x].fa!=goal){
        // cerr<<x<<' '<<t[x].fa<<' '<<t[t[x].fa].fa<<endl;
        int y = t[x].fa,z = t[y].fa;
        if(z!=goal)(x==t[y].son[1])^(y==t[z].son[1])?rotate(x):rotate(y);
        rotate(x);
    }
    if(!goal)rt=x;
}
inline int kth(int k){
    int p = rt;
    while(1){
        // cerr<<p<<' '<<k<<endl;
        if(k<=t[t[p].son[0]].siz)p=t[p].son[0];
        else{
            k-=t[t[p].son[0]].siz+1;
            if(!k)return p;
            p=t[p].son[1];
        }
    }
    return 1e9;
}
inline void dfs(int p){
    if(!p)return;
    spread(p);
    dfs(t[p].son[0]);
    if(1<=t[p].v&&t[p].v<=n)printf("%d ",t[p].v);
    dfs(t[p].son[1]);
}
int main(){
    n=read(),m=read();
    rt=build(0,0,n+1);
    for(int i = 1,l,r;i<=m;i++){
        l=read(),r=read();
        l=kth(l),r=kth(r+2);
        splay(l,0),splay(r,l);
        cg(t[r].son[0]);
    }
    dfs(rt);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3804kb

input:

30 5
7 26
7 18
5 9
4 15
3 15

output:

1 2 18 4 23 22 21 6 5 24 25 26 20 19 3 17 16 15 14 13 12 11 10 9 8 7 27 28 29 30 

result:

wrong answer 1st lines differ - expected: '1 2 4 17 16 15 6 5 18 19 20 21...4 13 12 11 10 9 8 7 27 28 29 30', found: '1 2 18 4 23 22 21 6 5 24 25 26... 13 12 11 10 9 8 7 27 28 29 30 '