传送门

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

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. Javascript字符串

    ## 定义 ``` var str = new String("abcdefg"); var str = "abcdefg"; ``` ## 常用方法 ### ...

  2. 为Visual Studio更换皮肤和背景图

    最近在博客上别人在老早之前就有加了背景图,想想怎么能这么out,于是乎,百度一下,能否知道? 咳咳,资源还是有滴!可惜都是比较旧的跟新版本的配置方法可能有点不一样, 可能会有很多人会觉得不适应,不过没 ...

  3. C# WinForm捕获全局异常

    网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...

  4. java 中遍历hashmap 和hashset 的方法

    一.java中遍历hashmap:    for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {     String ...

  5. android之广播(一)

    Android中的广播 系统在运行过程中会产生很多事件,某些事件产生的时候,比如:电量的改变,收发短信.拨打电话.屏幕解锁.开机等等,系统会发送广播.只要应用程序接受到这条广播就知道系统发生了相应的事 ...

  6. ASimpleCache使用感受

    一.简介 ASimpleCache只能作为一份教程,一个学习样板,不能当真把它当回事. 作者杨福海,Afinal框架也是他创造的. 可是我读ASimpleCache的900行代码时,发现各种难看,并且 ...

  7. $(document).ready()和window.onload的区别

    来源于: The window.onload event fires when a document is completely downloaded to the browser. This mea ...

  8. oracle-day1

    今天的学习内容是oracle产品的三种安装方式,还有使用dbca静默建库 oracle产品的三种安装方式分别为: 1.图形化(Java向导)安装引导 2.使用应答文件静默安装 3.直接将装好的orac ...

  9. [转]session 持久化问题(重启服务器session 仍然存在)

    转:http://xiaolongfeixiang.iteye.com/blog/560800 关于在线人数统计,大都使用SessionListener监听器实现. SessionListener 触 ...

  10. Java算法-选择排序

    (转载出处) 选择排序的基本思想是遍历数组的过程中,以 i 代表当前需要排序的序号,则需要在剩余的 [i…n-1] 中找出其中的最小值,然后将找到的最小值与 i 指向的值进行交换.因为每一趟确定元素的 ...