初学Splay,板子代码10分求调qaq
查看原帖
初学Splay,板子代码10分求调qaq
670826
NianFeng楼主2023/11/30 16:36

rt。函数加强版的代码直接复制来的。先不管TLE,莫队等会学,我只想解决这堆WA的。

QAQ谢谢好心人们,代码如下:

#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace io{
    inline int read(){
        int x=0;
        bool flag=true;
        char c=getchar();
        while(c<'0'||c>'9'){
            if(c=='-')
                flag=false;
            c=getchar();
        }
        while(c>='0'&&c<='9'){
            x=(x<<1)+(x<<3)+c-'0';
            c=getchar();
        }
        return (flag?x:~(x-1));
    }
}
using namespace io;
const int N=300100;
int n,m,a[N];
struct Ans{
    int l,r,k,id;
    bool operator<(const Ans x)const{
        return (l==x.l?r<x.r:l<x.l);
    }
}in[N/10]; int ans[N/10];
int rt,tot;
struct Splay{
    int fa,ch[2];
    int val,cnt,siz;
}tree[N];
bool son(int x){
    return (x==tree[tree[x].fa].ch[1]);
}
void pushup(int x){
    tree[x].siz=tree[tree[x].ch[0]].siz+tree[tree[x].ch[1]].siz+tree[x].cnt;
}
void rotate(int x){
    int fa=tree[x].fa,k=son(x);
    tree[fa].ch[k]=tree[x].ch[k^1],tree[tree[x].ch[k^1]].fa=fa;
    tree[x].fa=tree[fa].fa,tree[x].ch[k^1]=fa;
    if(tree[fa].fa) tree[tree[fa].fa].ch[son(fa)]=x;
    tree[fa].fa=x;
    pushup(fa),pushup(x);
}
void splay(int x,int pos){
    pos=tree[pos].fa;
    for(int fa=tree[x].fa;fa!=pos;rotate(x),fa=tree[x].fa){
        if(tree[fa].fa!=pos){
            rotate((son(x)^son(fa))?x:fa);
        }
    }
    if(!pos) rt=x;
}
int creat(int x,int fa){
    ++tot;
    tree[tot].fa=fa,tree[tot].val=x;
    tree[tot].siz=tree[tot].cnt=1;
    return tot;
}
void insert(int k){
    if(!rt){
        rt=creat(k,0);
        return;
    }
    int p=rt,fa=0;
    while(p){
        if(k==tree[p].val){
            tree[p].siz++,tree[p].cnt++;
            splay(p,rt);
            return;
        }
        fa=p;
        if(k<tree[p].val) p=tree[p].ch[0];
        else p=tree[p].ch[1];
    }
    p=creat(k,fa);
    tree[fa].ch[k>tree[fa].val]=p;
    splay(p,rt);
}
int find(int k){
    int p=rt,ans=0;
    while(p){
        if(tree[p].val==k){
            int rest=tree[tree[p].ch[0]].siz;
            splay(p,rt);
            return ans+rest+1;
        }
        if(k<tree[p].val) p=tree[p].ch[0];
        else ans+=tree[tree[p].ch[0]].siz+tree[p].cnt,p=tree[p].ch[1];
    }
    return ans+1;
}
int query(int pos){
    int p=rt;
    while(true){
        if(tree[tree[p].ch[0]].siz<pos&&pos<=tree[tree[p].ch[0]].siz+tree[p].cnt){
            splay(p,rt);
            return tree[p].val;
        }
        if(pos<=tree[tree[p].ch[0]].siz) p=tree[p].ch[0];
        else pos-=tree[tree[p].ch[0]].siz+tree[p].cnt,p=tree[p].ch[1];
    }
}
int pre(){
    int p=tree[rt].ch[0];
    if(p){
        while(tree[p].ch[1])
            p=tree[p].ch[1];
        splay(p,rt);
    }
    return p;
}
int nxt(){
    int p=tree[rt].ch[1];
    if(p){
        while(tree[p].ch[0])
            p=tree[p].ch[0];
        splay(p,rt);
    }
    return p;
}
void remove(int k){
    find(k);
    if(tree[rt].cnt>1){
        tree[rt].cnt--;
        tree[rt].siz--;
    } else if(!tree[rt].ch[0]&&!tree[rt].ch[1]){
        rt=0;
    } else if(!tree[rt].ch[0]){
        rt=tree[rt].ch[1];
        tree[rt].fa=0;
    } else if(!tree[rt].ch[1]){
        rt=tree[rt].ch[0];
        tree[rt].fa=0;
    } else{
        int ls=pre();
        tree[tree[tree[rt].ch[1]].ch[1]].fa=rt;
        tree[rt].ch[1]=tree[tree[rt].ch[1]].ch[1];
        pushup(rt);
    }
    return;
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    for(int i=1;i<=m;i++){
        in[i].l=read(),in[i].r=read();
        in[i].k=read(),in[i].id=i;
    }
    sort(in+1,in+m+1);
    for(int i=1;i<=m;i++) cout<<in[i].id<<" "<<endl;
    int l=1,r=1;
    for(int i=1;i<=m;i++){
        while(l<in[i].l) remove(a[l]),l++;
        while(r<=in[i].r) insert(a[r]),r++;
        ans[in[i].id]=query(in[i].k);
    }
    for(int i=1;i<=m;i++){
        printf("%lld\n",ans[i]);
    }
    return 0;
}
2023/11/30 16:36
加载中...