洛谷 2234 [HNOI2002]营业额统计——treap(入门)
题目:https://www.luogu.org/problemnew/show/P2234
学习了一下 treap 的写法。
学习材料:https://blog.csdn.net/litble/article/details/78934306
http://memphis.is-programmer.com/posts/46317.html
https://www.cnblogs.com/AKMer/p/9981274.html
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return fx?ret:-ret;
}
int Mn(int a,int b){return a<b?a:b;}
const int N=,M=1e9;
int n,rt,tot,vl[N],c[N][],rd[N],ans;
void rotate(int &cr,bool d)
{
int v=c[cr][!d];
c[cr][!d]=c[v][d]; c[v][d]=cr; cr=v;
}
void ins(int &cr,int k)
{
if(!cr){cr=++tot; vl[cr]=k; rd[cr]=rand()%M; return;}
int d;
if(k<=vl[cr]) d=, ins(c[cr][],k);
else d=, ins(c[cr][],k);
if(rd[c[cr][d]]>rd[cr])rotate(cr,!d);
}
int fnd_pr(int cr,int k)
{
if(!cr)return N;
if(vl[cr]<=k)
{
int d=fnd_pr(c[cr][],k);
return d==N?cr:d;
}
return fnd_pr(c[cr][],k);
}
int fnd_sc(int cr,int k)
{
if(!cr)return N;
if(vl[cr]>=k)
{
int d=fnd_sc(c[cr][],k);
return d==N?cr:d;
}
return fnd_sc(c[cr][],k);
}
int main()
{
srand(time()); n=rdn();
for(int i=,d;i<=n;i++)
{
d=rdn();
if(i==)ans=d;
else
{
int u=fnd_pr(rt,d), v=fnd_sc(rt,d);
if(u==N)ans+=vl[v]-d;
else if(v==N)ans+=d-vl[u];
else ans+=Mn(vl[v]-d,d-vl[u]);
}
ins(rt,d);
}
printf("%d\n",ans); return ;
}
洛谷 2234 [HNOI2002]营业额统计——treap(入门)的更多相关文章
- [洛谷P2234][HNOI2002] 营业额统计 - Treap
Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...
- 洛谷.2234.[HNOI2002]营业额统计(Splay)
题目链接 //模板吧 #include<cstdio> #include<cctype> #include<algorithm> using namespace s ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- 洛谷—— P2234 [HNOI2002]营业额统计
https://www.luogu.org/problem/show?pid=2234 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业 ...
- 洛谷P2234 [HNOI2002]营业额统计
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
- 洛谷 P2234 [HNOI2002]营业额统计
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
- 洛谷P2234 [HNOI2002]营业额统计(01Tire树)
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
- bzoj 1588: [HNOI2002]营业额统计 treap
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 13902 Solved: 5225[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
随机推荐
- L255
If a farmer wishes to succeed, he must try to keep a wide gap between his consumption and his produc ...
- [ log4j ]-日志文件的使用
在java文件中通过 log4j 输出日志信息 1,先引入 log4j-xx.x.jar 包 2,新建一个日志类 PrintLog4j.java: package com.stu.log4j; imp ...
- Arduino显示PM2.5
这代码一般都是复制过来,在小改下就行了 代码如下: #include <Wire.h> #include <LiquidCrystal_I2C.h> #include < ...
- idea本地安装 lombok插件
转:https://blog.csdn.net/weixin_41404773/article/details/80689639 idea本地安装 lombok插件 项目中经常使用bean,entit ...
- python 1-10考试
- LeetCode—66、88、118、119、121 Array(Easy)
66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...
- 中国顶级黑客X档案
sunwear QQ:47347 微博: http://t.qq.com/sunwe4r 博客:http://hi.baidu.com/patricksunwear 好像不用了 日娃哥.EST核心成员 ...
- SQL注入之Sqli-labs系列第十七关(UPDATA– 基于错误– 单引号– 字符型)
开始挑战第十七关(Update Query- Error based - String) 首先介绍下update的用法: 作用:Update 语句用于修改表中的数据. 语法:UPDATE 表名称SET ...
- 如何在Ubuntu/Linux内使用 zip 压缩隐藏文件?
假设你的当前目录结构为: A文件--B文件夹--C文件等 | | D文件夹-E文件 | | 等等文件(含隐藏) 那么在使用zip命令时加上 -r 即可包含整个目录结构和隐藏文件: zip -r B.z ...
- Python网络爬虫之图片懒加载技术、selenium和PhantomJS
引入 图片懒加载 selenium phantomJs 谷歌无头浏览器 知识点回顾 验证码处理流程 动态数据加载处理 一.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素材http://sc.ch ...