传送门

平衡树常规题,给出两种实现算法

Treap版:

 //OJ 1610
 //by Cydiater
 //2016.9.1
 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <iomanip>
 #include <string>
 #include <queue>
 #include <map>
 #include <algorithm>
 #include <cmath>
 #include <ctime>
 using namespace std;
 #define ll long long
 #define up(i,j,n)       for(int i=j;i<=n;i++)
 #define down(i,j,n)     for(int i=j;i>=n;i--)
 ;
 const int oo=0x3f3f3f3f;
 inline int read(){
     ,f=;
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return x*f;
 }
 ,tol=,maxx,minn,delta;
 ll ans;
 struct node{
     int leftt,rightt,rnd,v;
 }t[MAXN];
 namespace solution{
     void lefturn(int &k){
         int tt=t[k].rightt;t[k].rightt=t[tt].leftt;t[tt].leftt=k;k=tt;
     }
     void righturn(int &k){
         int tt=t[k].leftt;t[k].leftt=t[tt].rightt;t[tt].rightt=k;k=tt;
     }
     void insert(int &k,int x){
         ){
             k=++tol;
             t[k].leftt=t[k].rightt=;
             t[k].v=x;t[k].rnd=rand();
             return;
         }
         if(x==t[k].v)return;
         else if(x>t[k].v){
             insert(t[k].rightt,x);
             if(t[t[k].rightt].rnd<t[k].rnd)lefturn(k);
         }
         else if(x<t[k].v){
             insert(t[k].leftt,x);
             if(t[t[k].leftt].rnd<t[k].rnd)righturn(k);
         }
     }
     void query_delta(int k,int x){
         )        return;
         if(x<=t[k].v)maxx=min(maxx,t[k].v);
         if(x>=t[k].v)minn=max(minn,t[k].v);
         if(x>t[k].v)query_delta(t[k].rightt,x);
         else        query_delta(t[k].leftt,x);
     }
     void slove(){
         N=read();
         up(i,,N){
             num=read();
             maxx=oo;minn=-oo;delta=oo;
             query_delta(root,num);
             if(maxx!=-oo)delta=min(delta,abs(maxx-num));
             if(minn!=oo)delta=min(delta,abs(num-minn));
             )delta=num;
             ans+=delta;
             //cout<<num<<' '<<delta<<' '<<ans<<endl;
             insert(root,num);
         }
     }
     void output(){
         cout<<ans<<endl;
     }
 }
 int main(){
     //freopen("input.in","r",stdin);
     //freopen("output.out","w",stdout);
     using namespace solution;
     slove();
     output();
     ;
 }

Splay版:

 //bzoj 1588
 //by Cydiater
 //2016.9.6
 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <string>
 #include <algorithm>
 #include <queue>
 #include <map>
 #include <ctime>
 #include <cmath>
 #include <iomanip>
 using namespace std;
 #define ll long long
 #define up(i,j,n)        for(int i=j;i<=n;i++)
 #define down(i,j,n)        for(int i=j;i>=n;i--)
 ;
 const int oo=0x3f3f3f3f;
 inline int read(){
     ,f=;
     ;ch=getchar();}
     +ch-';ch=getchar();}
     return x*f;
 }
 map<int,int>lable;
 struct SplayTree{
     ],v,siz,cnt,fa;
 }t[MAXN];
 ,tol=,anss=;
 namespace solution{
     ]==x;}
     ]=t[x].son[]=t[x].siz=t[x].cnt=t[x].fa=t[x].v=;}
     void updata(int x){
         if(x){
             t[x].siz=t[x].cnt;
             ])t[x].siz+=t[t[x].son[]].siz;
             ])t[x].siz+=t[t[x].son[]].siz;
         }
     }
     inline void rotate(int x){//rotate now node to root
         int old=t[x].fa,oldf=t[old].fa,which=get(x);
         t[old].son[which]=t[x].son[which^];t[t[old].son[which]].fa=old;
         t[old].fa=x;t[x].son[which^]=old;
         t[x].fa=oldf;
         ]==old]=x;
         updata(old);updata(x);
     }
     void splay(int x){
         for(int fa;(fa=t[x].fa);rotate(x))if(t[fa].fa)
         rotate(get(x)==get(fa)?fa:x);root=x;
     }
     inline void insert(int v){
         ){
             root=++tol;
             t[root].son[]=t[root].son[]=t[root].fa=;
             t[root].v=v;t[root].cnt=t[root].siz=;
             return;
         }
         ;
         ){
             if(t[now].v==v){
                 t[now].cnt++;updata(now);updata(fa);
                 splay(now);break;
             }
             fa=now;now=t[now].son[t[now].v<v];
             ){
                 now=++tol;
                 t[now].son[]=t[now].son[]=;t[now].v=v;
                 t[now].siz=t[now].cnt=;t[now].fa=fa;
                 t[fa].son[t[fa].v<v]=tol;
                 updata(fa);
                 splay(now);
                 break;
             }
         }
     }
     int find(int x){
         ;
         ){
             ];
             else{
                 ans+=(t[now].son[]?t[t[now].son[]].siz:);
                 if(t[now].v==x){
                     splay(now);
                     ;
                 }
                 ans+=t[now].cnt;
                 now=t[now].son[];
             }
         }
     }
     int pre(){
         ];
         ])now=t[now].son[];
         return now;
     }
     int nxt(){
         ];
         ])now=t[now].son[];
         return now;
     }
     void del(int x){
         int whatever=find(x);
         ){
             t[root].cnt--;
             t[root].siz--;
         }
         ]+t[root].son[]==){
             clear(root);root=;
             return;
         }
         ]){
             ];t[root].fa=;
             clear(oldroot);return;
         }]){
             ];t[root].fa=;
             clear(oldroot);return;
         }
         int leftbig=pre(),oldroot=root;splay(leftbig);
         t[t[oldroot].son[]].fa=root;t[root].son[]=t[oldroot].son[];
         clear(root);updata(root);
     }
     void debug(int now){
         printf(],t[now].son[],t[now].siz,t[now].cnt,t[now].fa,t[now].v);
         ])debug(t[now].son[]);
         ])debug(t[now].son[]);
     }
 }
 int main(){
     //freopen("input.in","r",stdin);
     //freopen("output.out","w",stdout);
     using namespace solution;
     N=read();
     while(N--){
         int num=read();if(lable[num])continue;
         insert(num);lable[num]=;
         int maxx=nxt(),minn=pre();
         int tmp=oo;
         )tmp=num;
         if(maxx)tmp=min(tmp,t[maxx].v-num);
         if(minn)tmp=min(tmp,num-t[minn].v);
         anss+=tmp;
         //debug(root);puts("");
     }
     printf("%d\n",anss);
     ;
 }

BZOJ1588[HNOI2002]营业额统计的更多相关文章

  1. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  2. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  3. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  4. 【链表】BZOJ1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 17555  Solved: 7179[Submit][Sta ...

  5. [BZOJ1588][HNOI2002]营业额统计 无旋Treap

    [HNOI2002]营业额统计 时间限制: 5 Sec  内存限制: 162 MB 题目描述 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以 ...

  6. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  7. bzoj1588: [HNOI2002]营业额统计(权值线段树)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 16863  Solved: 6789[Submit][Sta ...

  8. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

  9. [BZOJ1588] [HNOI2002] 营业额统计 (treap)

    Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...

  10. BZOJ1588 [HNOI2002]营业额统计 set

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1588 题意概括 给出数列,求  ∑F[i],其中F[1] = a[1] , F[i] = min( ...

随机推荐

  1. 继续研究NDK

    继续研究NDK 我在阿里云服务器上搭建了Android ndk的开发平台,并且借助这一平台研究了NDK的内部细节. NDK提供了Android本地编程的接口,让你可以开发高效的依赖库,提高程序的速度, ...

  2. mysql full text全文索引必要条件

    show variables like 'ft_m%' 'ft_max_word_len', '84''ft_min_word_len', '4' 对于英文来说, ft_min_word_len=4是 ...

  3. Python 处理数据库返回结果

    游标执行后返回的结果都只是数据,但是不带有列名标识.这里需要处理2个问题: 将返回的数据映射到每一列上 当返回的结果很大的时候,需要使用迭代器来提升性能. 解决上面的2个问题,在python里面可以采 ...

  4. 2014 todo list

    1. 做好已知的各种项目,争取能成立固定团队2. 横向扩展技术学习,了解各种技术,加强技术素养3. 争取找个妹子4. 加强音律学习5. 继续发展各业余爱好6. 努力摇号 年底看收成.

  5. Mecanim动画系统 制作流程

  6. 东大OJ-最大子序列问题的变形

    1302: 最大子序列 时间限制: 1 Sec  内存限制: 128 MB 提交: 224  解决: 54 [提交][状态][讨论版] 题目描述 给定一个N个整数组成的序列,整数有正有负,找出两段不重 ...

  7. [AJAX系列]$.get(url,[data],[fn],[type])

    $.get(url,[data],[fn],[type]) 概述 通过远程HTTP GET请求载入信息 参数 url:待载入页眉的URL地址 data:待发送key/value参数 callback: ...

  8. MyBatis学习--高级映射

    简介 前面说过了简单的数据库查询和管理查询,在开发需求中有一些一对一.一对多和多对多的需求开发,如在开发购物车的时候,订单和用户是一对一,用户和订单是一对多,用户和商品是多对多.这些在Hibernat ...

  9. ORACLE数据泵使用详解

    来源于:http://blog.sina.com.cn/s/blog_490a0c990100wh4y.html http://blog.csdn.net/jojo52013145/article/d ...

  10. 【CodeVS 3153】取石子游戏

    http://codevs.cn/problem/3153/ 对于这道题,直觉告诉我每一个状态一定是必胜或必败的 然后设定操作次数t,t为取完些石子最多需要多少步. 如果\(a_i\)不为1,\(t= ...