传送门

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

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. Nodejs进阶:如何玩转子进程(child_process)

    本文摘录自个人总结<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 在node中,child_process这个模 ...

  2. 通向高可扩展性之路(WhatsApp篇)---- 脸书花了190亿买来的WhatsApp的架构

    原文链接:http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-bill ...

  3. BI的相关问题[转]

    什么是BI? Business Intelligence(BI) = Data Warehouse(DW) + OLAP + Data Mining(DM) 商业智能=数据仓库+联机分析+数据挖掘 做 ...

  4. P值与significant(显著性)的理解

    P值与significant的理解 来源:广州市统计局   发表日期:2015-01-21     P值可以理解为结论的风险大小,也就是根据数据得出的结果有多大的错误风险,P值越小,结论错误的风险越小 ...

  5. 在eclipse中使用第三方库总结

    一.建立user library 导入第三方jar文件,最简单的方式是:右键工程/属性/java build path/add external jars. 另一种方式是:window/prefren ...

  6. [AJAX系列]onreadystatechange事件

    onreadystatechange事件: 当请求被发送到服务器时,我们需要执行一些基于响应的任务 每当readyState改变时,就会触发onreadystatechange事件 readyStat ...

  7. linux基础-第十五单元 软件包的管理

    使用RPM安装及移除软件 什么是RPM rpm的文件名 rpm软件安装与移除工作中经常使用的选项 查看RPM软件包中的信息 查询已安装的软件包信息 RPM包的属性依赖性问题 什么是RPM包的属性依赖性 ...

  8. 89c51中断入口地址表

    中断源   中断入口地址 外部中断0 0003H 定时器/计数器T0 000BH 外部中断1 0013H 定时器/计数器T1 001BH 串行口中断   0023H

  9. [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能

    原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...

  10. iOS开发中的错误整理,iOS9之后的UIWindow(TopWindow)以及UIWindow与statusBar的关系

    iOS9之后的UIWindow的问题(TopWindow),以及UIWindow与statusBar之间的联系 1.出现的问题 iOS9之后UIWindow必须要指定一个根控制器,不然会报错!iOS9 ...