BZOJ1588[HNOI2002]营业额统计
平衡树常规题,给出两种实现算法
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]营业额统计的更多相关文章
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- 【链表】BZOJ1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 17555 Solved: 7179[Submit][Sta ...
- [BZOJ1588][HNOI2002]营业额统计 无旋Treap
[HNOI2002]营业额统计 时间限制: 5 Sec 内存限制: 162 MB 题目描述 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以 ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- bzoj1588: [HNOI2002]营业额统计(权值线段树)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 16863 Solved: 6789[Submit][Sta ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
- [BZOJ1588] [HNOI2002] 营业额统计 (treap)
Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...
- BZOJ1588 [HNOI2002]营业额统计 set
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1588 题意概括 给出数列,求 ∑F[i],其中F[1] = a[1] , F[i] = min( ...
随机推荐
- 简易版viewport
首先这里涉及到虚拟像素和物理像素,不管是移动设备还是pc都存在这个概念1.在显示器精度还不是很高的年代,一个物理像素就等于一个虚拟像素,1000px的网页就占据了1000的物理像素(如果你玩过小霸王, ...
- php file_get_contents失败[function.file-get-contents]: failed to open stream: HTTP request failed!解决
在使用file_get_contents方法来获取远程文件时会出现 [function.file-get-contents]: failed to open stream: HTTP request ...
- Vmware player 12
免费版的虚拟机Vmware,体积小.运行快速... 官方下载界面 下载地址: http://yunpan.cn/cm5smywVvqS8V 访问密码 35ac 官方下载:点击下载
- C# Label显示多行文本及换行(WinForm/WebForm)
转载: http://www.cnblogs.com/greatverve/archive/2011/10/20/label-multi.html WinForm 很多朋友都会在开发WinForm中遇 ...
- Java--笔记(4)
31.中间件是一种独立的系统软件或服务程序,分布式应用软件借助这种软件在不同的技术之间共享资源.中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯.是连接两个独立应用程序或独立系统的软 ...
- HttpServletRequest 转换成MultipartHttpServletRequest
//转换 HttpServletRequestMultipartHttpServletRequest mulReq=(MultipartHttpServletRequest)request;//获取上 ...
- 【BZOJ 2693】jzptab
莫名其妙地又卡在long long上了,我果然又在同一个地方犯逗. 在“在路上,同梦行”群里闹了个大笑话QAQ #include<cstdio> #include<cstring&g ...
- css3 操作动画要点
CSS3 有3种和动画相关的属性:transform, transition, animation. 不同点: 1. 触发条件不同.transition通常和hover等事件配合使用,由事件触发.a ...
- 控件 UI: VisualState, VisualStateManager, 控件的默认 UI
VisualState 和 VisualStateManager 控件的默认 Style, ControlTemplate, VisualState 示例1.演示“VisualState 和 Visu ...
- 【POJ 3041】Asteroids (最小点覆盖)
每次选择清除一行或者一列上的小行星.最少选择几次. 将行和列抽象成点,第i行为节点i+n,第j列为节点j,每个行星则是一条边,连接了所在的行列. 于是问题转化成最小点覆盖.二分图的最小点覆盖==最大匹 ...