用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度。。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 33333
#define inf 0x3f3f3f3f
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
using namespace std;
int fa[maxn],ch[maxn][],root,k[maxn],ind=;
inline void rotate(int p)
{
int q=fa[p],y=fa[q],x=ch[q][]==p;
ch[q][x]=ch[p][x^];fa[ch[q][x]]=q;
ch[p][x^]=q;fa[q]=p;
fa[p]=y;
if(y)
{
if(ch[y][]==q)ch[y][]=p;
else ch[y][]=p;
}
}
inline void splay(int x)
{
for(int y;y=fa[x];rotate(x))
{
if(fa[y])
{
if(y==lc(fa[y])&&x==lc(y)||(y==rc(fa[y])&&x==rc(y)))rotate(y);
else rotate(x);
}
}
root=x;
}
void insert(int x,int v)
{
while(ch[x][k[x]<v])x=ch[x][k[x]<v];
ch[x][k[x]<v]=++ind;
fa[ind]=x;k[ind]=v;splay(ind);
}
inline int pre(int x)
{
int tmp=ch[x][];
while(ch[tmp][])tmp=ch[tmp][];
return k[tmp];
}
inline int suc(int x)
{
int tmp=ch[x][];
while(ch[tmp][])tmp=ch[tmp][];
return k[tmp];
}
int main()
{
int t;int n;
int ans=;
scanf("%d",&n);
if(scanf("%d",&t)==-)t=;
root=;k[root]=t;
ans=t;
insert(root,inf);insert(root,-inf);
for(int i=;i<=n;i++)
{
if(scanf("%d",&t)==-)t=;
insert(root,t);
int a=pre(root);int b=suc(root);
ans+=min(t-a,b-t);
}
printf("%d\n",ans);
return ;
}

bzoj 1588 splay模板题的更多相关文章

  1. bzoj 3224 splay模板题4

    再刷水题我就废了... #include<iostream> #include<cstdio> #include<algorithm> #include<cs ...

  2. bzoj 3223 splay模板题3

    水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...

  3. bzoj 1208 splay模板题2

    自己yy了找前驱和后继,学了学怎么删除...(反正就是练模板) #include<iostream> #include<cstdio> #include<cstring& ...

  4. BZOJ 1588 平衡树 模板题

    Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int si ...

  5. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  6. 【BZOJ 3188】【Coci 2011】Upit Splay模板题

    转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...

  7. BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题

    题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...

  8. BZOJ 1588: Treap 模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12171  Solved: 4352 Description ...

  9. BZOJ 3224 平衡树模板题

    Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int n, ...

随机推荐

  1. PHP 图片处理PNG颜色丢失

    根据需求做一个用户点击测试桃花运的小程序.在开发中需要使用PHP进行开发,原理是将用户的姓名通过php的图片处理写入图片中,此处遇到一巨坑. 就是png图片在调用 imagecolorallocate ...

  2. 安装ESXi5.5遇到Relocating modules and starting up the kernel的处理

    在一些Dell较旧的服务器上安装ESXi 5.x时, 会遇到卡在Relocating modules and starting up the kernel过不去的问题. 比如我装的这台CS24VSS. ...

  3. Tstringlist分割字符串的用法?

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaText 2.Delimiter &Delim ...

  4. Win10添加简体中文美式键盘的方法

    在Win10中很多朋友发现没有简体中文(美式键盘)的选项,而如果使用“英语-美式键盘”作为默认输入法,有ModernApp的界面会变成英文,这十分不方便,那么有没有方可以在Win10中添加一个 简体中 ...

  5. usb驱动开发16之设备生命线

    回到struct usb_hcd,继续努力的往下看. kref,usb主机控制器的引用计数.struct usb_hcd也有自己专用的引用计数函数,看hcd.c文件. static void hcd_ ...

  6. ORA-28000: the account is locked-的解决办法

    ORA-28000: the account is locked第一步:使用PL/SQL,登录名为system,数据库名称不变,选择类型的时候把Normal修改为Sysdba;第二步:选择myjob, ...

  7. python数字图像处理(19):骨架提取与分水岭算法

    骨架提取与分水岭算法也属于形态学处理范畴,都放在morphology子模块内. 1.骨架提取 骨架提取,也叫二值图像细化.这种算法能将一个连通区域细化成一个像素的宽度,用于特征提取和目标拓扑表示. m ...

  8. [MetaHook] Event Hook

    #include <metahook.h> struct event_hook_t { event_hook_t *next; char *name; void (*pfnEvent)(e ...

  9. 微信公众平台开发(71)OAuth2.0网页授权

    微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...

  10. .NET CLR 运行原理

    原文: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects 文章讨论了: SystemDoma ...