• bzoj1588 营业额统计


    裸的平衡树题,splay做的。。。

    存下板子

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define INF 0x3f3f3f3f
    using namespace std;
    int n,tot,rt,ans;
    int v[100005];
    struct node{
        int son[2];
        int fa;
        int val;
    }tr[100005];
    void rotate(int x){
        int y=tr[x].fa,z=tr[y].fa;
        int typ=(x==tr[y].son[1]);
        tr[y].son[typ]=tr[x].son[typ^1];
        tr[tr[x].son[typ^1]].fa=y;
        tr[x].son[typ^1]=y;tr[y].fa=x;
        tr[x].fa=z;
        if(z){
            if(tr[z].son[1]==y)tr[z].son[1]=x;
            else tr[z].son[0]=x;
        }
    }
    void splay(int x){
        for(int y;y=tr[x].fa;rotate(x)){
            if(tr[y].fa){
                rotate((x==tr[y].son[0])==(y==tr[tr[y].fa].son[0])?y:x);
            }
        }
        rt=x;
    }
    void insert(int x,int y){
        int k;
        while(true){
            k=tr[x].son[tr[x].val<y];
            if(!k){
                k=++tot;
                tr[x].son[tr[x].val<y]=k;
                tr[k].val=y;tr[k].fa=x;
                break;
            }
            x=k;
        }
        splay(k);
    }
    int find_pre(int x){
        int tmp=tr[x].son[0];
        while(tr[tmp].son[1])tmp=tr[tmp].son[1];
        return tr[tmp].val;
    }
    int find_suc(int x){
        int tmp=tr[x].son[1];
        while(tr[tmp].son[0])tmp=tr[tmp].son[0];
        return tr[tmp].val;
    }
    int main(){
        scanf("%d",&n);
        scanf("%d",&v[1]);
        ans=v[1];
        insert(rt,v[1]);
        insert(rt,INF);
        insert(rt,-INF);
        for(int i=2;i<=n;i++){
            scanf("%d",&v[i]);
            insert(rt,v[i]);
            if(i>=2)ans+=min(v[i]-find_pre(v[i]),find_suc(v[i])-v[i]);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    html04
    html03
    html02
    html01
    通过脚本获取form表单的数值而不是submit
    myeclipse自带的数据库查看文件
    如何实现数组和List之间的转换?
    Array和ArrayList有何区别?
    ArrayList和LinkedList的区别是什么?
    如何决定使用HashMap还是TreeMap?
  • 原文地址:https://www.cnblogs.com/lnxcj/p/9601039.html
Copyright © 2020-2023  润新知