传送门

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

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. Tyk API网关介绍及安装说明

    Tyk API网关介绍及安装说明 Tyk是一个开源的轻量级API网关程序. 什么是API网关 API网关是一个各类不同API的前置服务器.API网关封装了系统内部架构,对外提供统一服务.此外还可以实现 ...

  2. android之广播(二)

    广播接受者不仅可以通过清单文件来向系统注册,也可以通过代码来注册.并且有的广播必须通过代码来注册广播接受者. 锁屏和解锁广播 电量改变广播 打开屏幕和关闭屏幕 这里将广播接收者写在服务里面 <? ...

  3. DBVisualizer 6.0.2配置

    DBVis是一款通过JDBC连接数据库的软件,理论上,可以连接任何数据库,今天,我们以DBVisualizer6.0.2免费版为例 (一)连接MySql5 1.解压压缩包至本地某路径下 2.配置环境变 ...

  4. Python 练习册

    01:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果 [图像处理] 类似于图中效果: py 2.7代码: from PIL import Image, Im ...

  5. 【JavaEE企业应用学习记录】验证配置

    package sanglp; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validat ...

  6. java 常用的一些关键字

    1.关键字extends 1.继承作用 优化代码,减少代码的重复使用. 2.继承使用时机 两个类之间必须要满足is a的关系 ,才能够拥有继承关系,不是任 何 情况下都允许继承 3.继承的使用注意事项 ...

  7. Linux下运行memcached失败

    Linux下运行memcached失败 1.错误信息如下 [root@localhost ~]# memcached can't run as root without the -u switch 2 ...

  8. android-之测试框架的使用AndroidTestCase

    Android Manifest.xml中所需要添加的配置 <instrumentation android:name="android.test.InstrumentationTes ...

  9. nginx启动、重启、关闭

    一.启动 cd usr/local/nginx/sbin ./nginx 二.重启 更改配置重启nginx kill -HUP 主进程号或进程号文件路径 或者使用 cd /usr/local/ngin ...

  10. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed

    iOS [错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'] 错误:'Ch ...