Treap:

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
int size,n,xx,root,ans,tmp;
struct Treap{int ch[2],v,cnt,rnd,sz;}tr[500050];
void Upd(int k){tr[k].sz=tr[tr[k].ch[0]].sz+tr[tr[k].ch[1]].sz+tr[k].cnt;}
void rot(int &k,bool f){int t=tr[k].ch[f];tr[k].ch[f]=tr[t].ch[!f],tr[t].ch[!f]=k,Upd(k),Upd(t),k=t;}
void insert(int &k,int num){
if(!k){k=++size,tr[k].cnt=tr[k].sz=1,tr[k].rnd=rand(),tr[k].v=num;return;}
tr[k].sz++;
if(tr[k].v==num){tr[k].cnt++;return;}
bool f=num>tr[k].v;
insert(tr[k].ch[f],num);
if(tr[tr[k].ch[f]].rnd<tr[k].rnd)rot(k,f);
}
void pre(int k,int num){
if(!k)return;
if(tr[k].v==num)tmp=0;
else if(tr[k].v>num)tmp=min(tmp,tr[k].v-num),pre(tr[k].ch[0],num);
else pre(tr[k].ch[1],num);
}
void nxt(int k,int num){
if(!k)return;
if(tr[k].v==num)tmp=0;
else if(tr[k].v<num)tmp=min(tmp,num-tr[k].v),nxt(tr[k].ch[1],num);
else nxt(tr[k].ch[0],num);
}
int main(){
scanf("%d%d",&n,&xx);
insert(root,xx),ans+=xx;
for(int i=1;i<n;i++){
scanf("%d",&xx);
tmp=0x3fffffff,pre(root,xx),nxt(root,xx);
ans+=tmp,insert(root,xx);
}
printf("%d\n",ans);
}

Splay:

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 33333
#define inf 0x3f3f3f3f
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
int n,fa[N],ch[N][2],root,k[N],ind=1,xx,ans;
void rotate(int p){
int q=fa[p],y=fa[q],x=ch[q][1]==p;
ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
ch[p][x^1]=q;fa[q]=p;
fa[p]=y;
if(y){
if(ch[y][0]==q)ch[y][0]=p;
else ch[y][1]=p;
}
}
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);
}
int pre(int x){
int tmp=ch[x][0];
while(ch[tmp][1])tmp=ch[tmp][1];
return k[tmp];
}
int suc(int x){
int tmp=ch[x][1];
while(ch[tmp][0])tmp=ch[tmp][0];
return k[tmp];
}
int main(){
scanf("%d",&n);
insert(root,inf),insert(root,-inf);
scanf("%d",&xx);insert(root,xx);
ans=xx;
for(int i=2;i<=n;i++){
scanf("%d",&xx);
insert(root,xx);
int a=pre(root),b=suc(root);
ans+=min(xx-a,b-xx);
}
printf("%d\n",ans);
}

BZOJ 1588 平衡树 模板题的更多相关文章

  1. bzoj 1588 splay模板题

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

  2. BZOJ 3224 平衡树模板题

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

  3. HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  4. 【BZOJ 3223】文艺平衡树 模板题

    就是打个翻转标记,下推标记时记得交换左右孩子指针,查询kth和中序遍历输出时也记得要下推标记同时交换指针,二者不可缺!←这是易错点 仿陈竞潇学长模板的代码: #include<cctype> ...

  5. BZOJ 1208 宠物收养所 | 平衡树模板题

    BZOJ 1208 宠物收养所 我犯过的错误:删除一个节点后没有update新的根节点,导致size错了! #include <cstdio> #include <cmath> ...

  6. 【BZOJ 3224】普通平衡树 模板题

    删除节点时把节点splay到根: 然后把根左子树的最右边节点splay到根的左孩子上: 然后删除就可以了: 我的教训是删根的时候根的右孩子的父亲指针一定要记得指向根的左孩子!!! my AC code ...

  7. bzoj 1588 平衡树 splay

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

  8. BZOJ 1588: Treap 模板

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

  9. bzoj 3224 splay模板题4

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

随机推荐

  1. [React] Integration test a React component that consumes a Render Prop

    In this lesson, I use Enzyme and Jest's Snapshot functionality to write an integration test for a co ...

  2. 开源TT框架上的日志类

    public class Logger { /** * log tag */ private String tagName = "MoGuLogger";// tag name / ...

  3. 剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到)

    剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到) 1.新建Converter public class BorderClipConverter : IM ...

  4. MVVM 中 ViewModelBase和 CommandBase

    public class ViewModelBase : INotifyPropertyChanged , IDisposable { public virtual string DisplayNam ...

  5. C#各个版本中的新增特性详解【转】

    序言 自从2000年初期发布以来,c#编程语言不断的得到改进,使我们能够更加清晰的编写代码,也更加容易维护我们的代码,增强的功能已经从1.0搞到啦7.0甚至7.1,每一次改过都伴随着.NET Fram ...

  6. SparkShuffle调优原理和最佳实践

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  7. C# HttpHelper万能框架实现 接口

    POST请请求是使用Http协议与请求的URL进行连接,然后再写入数据,最后关闭连接的过程 方法(1) //要Post的数据 string postdate = "a=123&c=4 ...

  8. Servlet学习(三)——实例:用户登录并记录登陆次数

    1.前提:在Mysql数据库下建立数据库web13,在web13下创建一张表user,插入几条数据如下: 2.创建HTML文件,命名为login,作为登录界面(以post方式提交) <!DOCT ...

  9. NEON基本知识

    http://blog.csdn.net/EmSoftEn/article/details/51834171 http://blog.csdn.net/yxnyxnyxnyxnyxn/article/ ...

  10. python中*号用法总结

    python 中有很多地方用到星号,有时候会想知道这个*是干嘛用的,总结如下,有不当之处,还望不吝指出,谢谢.1.乘法: 在很多时候是用作乘法的,例如: In [90]: 2*7 Out[90]: 1 ...