BZOJ[1588][HNOI2002]营业额统计 Treap

题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=1588

裸的平衡树,找一个类似于前驱后继的东西,但可以是自己
链表代码见zP1nG的博客

代码如下:

#include
#include
#include
#define INF 1000000000
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;
}
struct Node{
    int cnt,siz,pri,x;
    Node *ch[2];
    Node(int);
    inline void maintain(){
        siz=cnt+ch[0]->siz+ch[1]->siz;
        return;
    }
    inline int cmp(int k){
        if(x==k) return -1;
        return k0:1;
    }
}*null,*root;
Node::Node(int _):x(_){
    pri=rand();
    siz=cnt=1;
    ch[0]=ch[1]=null;
}
inline void Rotate(Node *&x,int d){
    Node *k=x->ch[d^1];
    x->ch[d^1]=k->ch[d];
    k->ch[d]=x;
    x->maintain();k->maintain();
    x=k;
}
void Insert(Node *&x,int k){
    if(x==null){
        x=new Node(k);
        return;
    }
    int d=x->cmp(k);
    if(!~d) x->siz++,x->cnt++;
    else{
        Insert(x->ch[d],k);x->maintain();
        if(x->ch[d]->pri < x->pri) Rotate(x,d^1);
    }
}
int Lower(Node *x,int k){
    if(x==null) return -INF;
    if(x->x>k) return Lower(x->ch[0],k);
    return max(x->x,Lower(x->ch[1],k));
}
int Upper(Node *x,int k){
    if(x==null) return INF;
    if(x->xreturn Upper(x->ch[1],k);
    return min(x->x,Upper(x->ch[0],k));
}
int n,ans,t,t1,x;
int main(){
    null=new Node(0);
    null->siz=null->cnt=0;
    null->ch[0]=null->ch[1]=null;
    root=null;
    n=read();
    ans=read();Insert(root,ans);
    for(int i=1;iprintf("%d",ans);
return 0;
}

你可能感兴趣的:(BZOJ,Treap,各省省选)