本地AC,提交RE8个点?!

P6136 【模板】普通平衡树(数据加强版)

chaotic @ 2021-12-30 12:04:04

#include<bits/stdc++.h>
using namespace std;
const int INF=0x7fffffff;
struct Treap{
    int lson,rson,sum,dat,size,length;
    #define ls(x) c[x].lson
    #define rs(x) c[x].rson
    #define sum(x) c[x].sum
    #define dat(x) c[x].dat
    #define sz(x) c[x].size
    #define ln(x) c[x].length
}c[10000010];
int n,m,tmpx,flag,root,cnt,last,ans;
int New(int val)
{

    cnt++;
    sum(cnt)=val;
    dat(cnt)=rand();
    ln(cnt)=sz(cnt)=1;
    return cnt;
}
void update(int p)
{
    sz(p)=sz(ls(p))+sz(rs(p))+ln(p);
}
void build()
{
    New(-INF);
    New(INF);
    root=1;
    rs(1)=2;
    update(root);
}
void zig(int &p)
{
    int q=ls(p);
    ls(p)=rs(q);
    rs(q)=p;
    p=q;
    update(rs(p));
    update(p);
}
void zag(int &p)
{
    int q=rs(p);
    rs(p)=ls(q);
    ls(q)=p;
    p=q;
    update(ls(p));
    update(p);
}
void insert(int &p,int x)
{
    if(p==0)
    {
        p=New(x);
        return;
    }
    if(x==sum(p))
    {
        ln(p)++;
        update(p);
        return;
    }
    if(x<sum(p))
    {
        insert(ls(p),x);
        if(dat(ls(p))>dat(p)) zig(p);
    }
    else
    {
        insert(rs(p),x);
        if(dat(rs(p))>dat(p)) zag(p);
    }
    update(p);
}
void remove(int &p,int x)
{
    if(p==0) return;
    if(sum(p)==x)
    {
        if(ln(p)>1)
        {
            ln(p)--;
            update(p);
            return;
        }
        if(ls(p)||rs(p))
        {
            if(rs(p)==0||dat(ls(p))>dat(rs(p)))
            {
                zig(p);
                remove(rs(p),x);
            }
            else
            {
                zag(p);
                remove(ls(p),x);
            }
            update(p);
        }
        else p=0;
        return;
    }
    x<sum(p) ? remove(ls(p),x) : remove(rs(p),x);
    update(p);
}
int GetRankByVal(int p,int x)
{
    if(p==0) return 0;
    if(sum(p)==x) return sz(ls(p));
    if(x<sum(p)) return GetRankByVal(ls(p),x);
    return GetRankByVal(rs(p),x)+sz(ls(p))+ln(p);
}
int GetValByRank(int p,int rank)
{
    if(p==0) return INF;
    if(sz(ls(p))>=rank) return GetValByRank(ls(p),rank);
    if(sz(ls(p))+ln(p)>=rank) return sum(p);
    else GetValByRank(rs(p),rank-sz(ls(p))-ln(p));
}
int GetPre(int x)
{
    int ans=1;
    int p=root;
    while(p)
    {
        if(x==sum(p))
        {
            if(ls(p)>0)
            {
                p=ls(p);
                while(rs(p)>0) p=rs(p);
                ans=p;
            }
            break;
        }
        if(sum(p)<x&&sum(p)>sum(ans)) ans=p;
        p=x<sum(p) ? ls(p) : rs(p);
    }
    return sum(ans);
}
int GetNext(int x)
{
    int ans=2;
    int p=root;
    while(p)
    {
        if(sum(p)==x)
        {
            if(rs(p)>0)
            {
                p=rs(p);
                while(ls(p)>0) p=ls(p);
                ans=p;
            }
            break;
        }
        if(sum(p)>x&&sum(p)<sum(ans)) ans=p;
        p=x<sum(p) ? ls(p) : rs(p);
    }
    return sum(ans);
}
int main()
{
    scanf("%d%d",&n,&m);
    build();
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&tmpx);
        insert(root,tmpx);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&flag);
        if(flag==1)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            insert(root,tmpx);
        }
        if(flag==2)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            remove(root,tmpx);
        }
        if(flag==3)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            last=GetRankByVal(root,tmpx);
            ans=ans^last;
        }
        if(flag==4)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            last=GetValByRank(root,tmpx+1);
            ans=ans^last;
        }
        if(flag==5)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            last=GetPre(tmpx);
            ans=ans^last;
        }
        if(flag==6)
        {
            scanf("%d",&tmpx);
            tmpx=tmpx^last;
            last=GetNext(tmpx);
            ans=ans^last;
        }
    }
    printf("%d",ans);
    return 0;
}

by chaotic @ 2021-12-30 17:09:40

那个巨佬帮我康康这treap?

求调!


|