BZOJ[1901]Zju2112 Dynamic Rankings 树套树(线段树套Splay)

传送门ber~

少了几个操作的二逼平衡树
简化了我的Delete函数

emmm行行行过几天打一遍主席树版本的

(18/1/29UPD):线段树套树状数组看这里

代码如下:

#include<algorithm>
#include<ctype.h>
#include<cstdio>
#define INF 2147483647
#define N 10020
using namespace std;
inline int read(){
    int x=0,f=1;char c;
    do c=getchar(),f=c=='-'?-1:f; while(!isdigit(c));
    do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c));
    return x*f;
}
int n,m,x,y,k,a[N];
char c[5];
struct Node{
    int cnt,siz,x;
    Node *ch[2],*fa;
    Node(int);
    inline int dir(){
        if(fa->ch[0]==this) return 0;
        if(fa->ch[1]==this) return 1;
        return -1;
    }
    inline int cmp(int k){
        if(k==x) return -1;
        return k<x?0:1;
    }
    inline void maintain(){
        siz=cnt+ch[0]->siz+ch[1]->siz;
        return;
    }
}*null,*tmp;
Node::Node(int _):x(_){
    siz=cnt=1;
    fa=ch[0]=ch[1]=null;
}
struct Seg{
    int l,r;
    Seg *ls,*rs;
    Node *Splay;
    Seg():Splay(null){}
}*Seg_Root;
inline void init(){
    null=new Node(-1);
    null->cnt=null->siz=0;
    null->fa=null->ch[0]=null->ch[1]=null;
    Seg_Root=new Seg;Seg_Root->Splay=null;
    return;
}
void Insert(int k,Node *&x,Node *fa){
    if(x==null){
        x=new Node(k);
        x->fa=fa;
        tmp=x;
        return;
    }
    int d=x->cmp(k);
    if(!~d){
        x->cnt++,x->siz++;
        tmp=x;
    }
    else Insert(k,x->ch[d],x);
    x->maintain();
    return;
}
inline void Rotate(Node *x,int d){
    Node *k=x->ch[d^1];
    x->ch[d^1]=k->ch[d];
    if(x->ch[d^1]!=null) x->ch[d^1]->fa=x;
    k->ch[d]=x;
    if(x->fa!=null) x->fa->ch[x->dir()]=k;
    k->fa=x->fa;x->fa=k;
    x->maintain();k->maintain();
    return;
}
inline void Splay(Node *x,Node *y,Seg *k){
    while(x->fa!=y){
        if(x->fa->fa!=y && x->fa->dir()==x->dir())
            Rotate(x->fa->fa,x->dir()^1);
        Rotate(x->fa,x->dir()^1);
    }
    if(y==null) k->Splay=x;
    return;
}
inline void AddNew(int k,Seg *x){
    Insert(k,x->Splay,null);
    Splay(tmp,null,x);
}
int Rank(int k,Node *x){
    if(x==null) return 0;
    int d=x->cmp(k);
    if(!~d) return x->ch[0]->siz;
    return Rank(k,x->ch[d])+(d?x->cnt+x->ch[0]->siz:0);
}
bool Find(int k,Node *x){
    if(x==null) return false;
    int d=x->cmp(k);
    if(!~d) return true;
    return Find(k,x->ch[d]);
}
int Lower(int k,Node *x){
    if(x==null) return -INF;
    if(x->x>=k) return Lower(k,x->ch[0]);
    return max(x->x,Lower(k,x->ch[1]));
}
Node *Lower_Pointer(int k,Node *x){
    if(x==null) return null;
    if(x->x>=k) return Lower_Pointer(k,x->ch[0]);
    Node *t=Lower_Pointer(k,x->ch[1]);
    return t==null?x:t;
}
Node *Upper_Pointer(int k,Node *x){
    if(x==null) return null;
    if(x->x<=k) return Upper_Pointer(k,x->ch[1]);
    Node *t=Upper_Pointer(k,x->ch[0]);
    return t==null?x:t;
}
inline void Delete(int k,Seg *x){
    Node *a=Lower_Pointer(k,x->Splay),*b=Upper_Pointer(k,x->Splay);
    if(a==null && b==null){
        if(x->Splay->cnt>1) x->Splay->cnt--,x->Splay->siz--;
        else x->Splay=null;
        return;
    }
    if(a==null){
        Splay(b,null,x);
        if(x->Splay->ch[0]->cnt>1) x->Splay->ch[0]->cnt--,x->Splay->ch[0]->siz--;
        else x->Splay->ch[0]=null;
        x->Splay->maintain();
        return;
    }
    if(b==null){
        Splay(a,null,x);
        if(x->Splay->ch[1]->cnt>1) x->Splay->ch[1]->cnt--,x->Splay->ch[1]->siz--;
        else x->Splay->ch[1]=null;
        x->Splay->maintain();
        return;
    }
    Splay(a,null,x);Splay(b,a,x);
    if(x->Splay->ch[1]->ch[0]->cnt>1) x->Splay->ch[1]->ch[0]->cnt--,x->Splay->ch[1]->ch[0]->siz--;
    else x->Splay->ch[1]->ch[0]=null;
    x->Splay->ch[1]->maintain();
    x->Splay->maintain();
    return;
}
void maketree(int l,int r,Seg *x){
    x->l=l;x->r=r;
    for(int i=l;i<=r;i++) AddNew(a[i],x);
    if(l==r) return;
    int mid=l+r>>1;
    maketree(l,mid,x->ls=new Seg);maketree(mid+1,r,x->rs=new Seg);
    return;
}
void Change(int x,int v,Seg *k){
    Delete(a[x],k);
    AddNew(v,k);
    if(k->l==k->r) return;
    int mid=k->l+k->r>>1;
    if(x<=mid) Change(x,v,k->ls);
    else Change(x,v,k->rs);
    return;
}
int Query_Rank(int x,int y,int v,Seg *k){
    if(k->l>=x && k->r<=y){
        return Rank(v,k->Splay);
    }
    int mid=k->l+k->r>>1;
    if(y<=mid) return Query_Rank(x,y,v,k->ls);
    else if(x>mid) return Query_Rank(x,y,v,k->rs);
    else return Query_Rank(x,y,v,k->ls)+Query_Rank(x,y,v,k->rs);
}
int Query_Lower(int x,int y,int v,Seg *k){
    if(k->l>=x && k->r<=y){
        return Lower(v,k->Splay);
    }
    int mid=k->l+k->r>>1;
    if(y<=mid) return Query_Lower(x,y,v,k->ls);
    else if(x>mid) return Query_Lower(x,y,v,k->rs);
    else return max(Query_Lower(x,y,v,k->ls),Query_Lower(x,y,v,k->rs));
}
bool Query_Find(int x,int y,int v,Seg *k){
    if(k->l>=x && k->r<=y){
        return Find(v,k->Splay);
    }
    int mid=k->l+k->r>>1;
    if(y<=mid) return Query_Find(x,y,v,k->ls);
    else if(x>mid) return Query_Find(x,y,v,k->rs);
    else return Query_Find(x,y,v,k->ls)|Query_Find(x,y,v,k->rs);
}
inline void Query(int x,int y,int k){
    int l=0,r=INF,tmp;
    while(l<=r){
        int mid=l+r>>1;
        int t=Query_Rank(x,y,mid,Seg_Root)+1;
        if(t>k) r=mid-1;
        else tmp=mid,l=mid+1;
    }
    if(Query_Find(x,y,tmp,Seg_Root)) printf("%d\n",tmp);
    else printf("%d\n",Query_Lower(x,y,tmp,Seg_Root));
    return;
}
int main(){
    init();
    n=read();m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    maketree(1,n,Seg_Root);
    for(int i=1;i<=m;i++){
        scanf("%s",c+1);
        x=read();y=read();
        if(c[1]=='Q'){
            k=read();
            Query(x,y,k);
        }
        else Change(x,y,Seg_Root),a[x]=y;
    }
return 0;
}
发布了165 篇原创文章 · 获赞 127 · 访问量 11万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览