码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1588 营业额统计

时间:2018-09-06 22:57:36      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:turn   amp   class   for   val   while   scan   ror   string   

裸的平衡树题,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 rorate(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;rorate(x)){
        if(tr[y].fa){
            rorate((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\n",ans);
    return 0;
}

 

bzoj1588 营业额统计

标签:turn   amp   class   for   val   while   scan   ror   string   

原文地址:https://www.cnblogs.com/lnxcj/p/9601039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!