1588: [HNOI2002]营业额统计

题目:传送门


题解:

   复习splay所以来刷个水。。。

   题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值

   那就直接找前驱后继啊(不过定义要改一下,是可以相同的)

  


代码:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
int d,n,c,f,son[];
}tr[];int len,root;
void add(int d,int f)
{
len++;tr[len].d=d;tr[len].n=tr[len].c=;
tr[len].son[]=tr[len].son[]=;tr[len].f=f;
if(d<tr[f].d)tr[f].son[]=len;
else tr[f].son[]=len;
}
void update(int x)
{
int lc=tr[x].son[],rc=tr[x].son[];
tr[x].c=tr[lc].c+tr[rc].c+tr[x].n;
}
int findip(int d)
{
int x=root;
while(tr[x].d!=d)
{
if(d<tr[x].d)
{
if(tr[x].son[]==)break;
else x=tr[x].son[];
}
else
{
if(tr[x].son[]==)break;
else x=tr[x].son[];
}
}
return x;
}
void rotate(int x,int w)
{
int f=tr[x].f,ff=tr[f].f;
int r,R;
r=tr[x].son[w],R=f;
tr[R].son[-w]=r;
if(r!=)tr[r].f=R; r=x,R=ff;
if(tr[R].son[]==f)tr[R].son[]=r;
else tr[R].son[]=r;
tr[r].f=R; r=f,R=x;
tr[R].son[w]=r;
tr[r].f=R; update(f);update(x);
}
void splay(int x,int rt)
{
while(tr[x].f!=rt)
{
int f=tr[x].f,ff=tr[f].f;
if(ff==rt)
{
if(tr[f].son[]==x)rotate(x,);
else rotate(x,);
}
else
{
if(tr[ff].son[]==f && tr[f].son[]==x)rotate(f,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(f,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(x,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(x,),rotate(x,);
}
}
if(rt==)root=x;
}
void ins(int d)
{
if(root==)
{
add(d,);root=len;
return ;
}
int x=findip(d);
if(tr[x].d==d)
{
tr[x].n++;
update(x);
splay(x,);
}
else
{
add(d,x);
update(x);
splay(len,);
}
}
int findqianqu(int d)
{
int x=findip(d);splay(x,);
if(d<tr[x].d && tr[x].son[]!=)
{
x=tr[x].son[];
while(tr[x].son[]!=)x=tr[x].son[];
}
if(d<tr[x].d)x=;
return x;
}
int findhouji(int d)
{
int x=findip(d);splay(x,);
if(d>tr[x].d && tr[x].son[]!=)
{
x=tr[x].son[];
while(tr[x].son[]!=)x=tr[x].son[];
}
if(d>tr[x].d)x=;
return x;
}
int main()
{
int n;scanf("%d",&n);root=;
int ans=;int d;
scanf("%d",&d);ans+=d;ins(d);
for(int i=;i<=n;i++)
{
scanf("%d",&d);int ss=;
int lc=findqianqu(d),rc=findhouji(d);
if(lc!=)ss=abs(tr[lc].d-d);
if(rc!=)ss=min(abs(tr[rc].d-d),ss);
ans+=ss;ins(d);
}
printf("%d\n",ans);
return ;
}

bzoj1588: [HNOI2002]营业额统计(splay)的更多相关文章

  1. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  2. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  3. bzoj1588: [HNOI2002]营业额统计 splay瞎写

    最近各种瞎写数论题,感觉需要回顾一下数据结构 写一发splay冷静一下(手速过慢,以后要多练练) 用splay是最直接的方法,但我感觉离散一波应该可以做出来(没仔细想过) 现在没有很追求代码优美,感觉 ...

  4. [bzoj1588][HNOI2002]营业额统计——splay

    题目大意 你被要求编写一个数据结构,支援以下操作,操作在线. 插入一个元素 查询一个元素与之前插入元素的最小差值. 题解 一道模板题.我是写了一个pre和succ函数水过的.1A,比较高兴. 代码 # ...

  5. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  6. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  7. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  8. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

  9. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

随机推荐

  1. Gerapy 使用详解

    https://blog.csdn.net/fengltxx/article/details/79894839

  2. 单件模式(Singleton)C++实现

    意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 实用性:1.当类只能有一个实例而且客户可以从一个众所周知的访问点访问它. 2.当这个唯一的实例应该是通过子类可扩展的,并且客户应该无需更改 ...

  3. CSS3伪元素、伪类选择器

    伪元素选择器: ::first-letter:为某个元素中的文字的首字母或第一个字使用样式. ::first-line:为某个元素的第一行文字使用样式. ::before:在某个元素之前插入一些内容. ...

  4. 给<hr/>添加样式

    点线式 破折线式 直线式 双线式 脊线式 槽线式 内嵌效果的 突起效果的 border-top:10px 设置水平线的大小 <hr style=" border-top:5px dot ...

  5. 编码和解码(字符串与byte[]之间的转换)

    资源来自互联网http://www.cnblogs.com/dabaopku/archive/2012/02/27/2370446.html 非常蛋疼的事情, google 和 baidu 在编码是分 ...

  6. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  7. Swift - what's the difference between metatype .Type and .self?

    Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...

  8. 提示 npm update check failed

    执行npm命令时出现以下提示 虽然不影响代码运行,但总觉得看了很碍事, 查找资料后发现是因为文件夹权限的问题, .config / configstore文件夹中包含一个文件:update-notif ...

  9. Jquery- scrollTop()一个问题:

    在使用jquery的scrollTop()方法获取滚动条的位置时,发现变量名为top会有影响,代码: <!doctype html> <html> <head> & ...

  10. MySQL主主高可用(keepalive)

    2台新的虚拟机172.16.1.1.172.16.1.2  (配置yum源 ) 安装数据库服务 其中 172.16.1.1.172.16.1.2运行数据库服务并设置数据库管理员从本机登录的密码是xzw ...