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. cf #257(Div.2) A. Jzzhu and Children

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. 使用Ant打包Android应用具体解释

    计划写个完整的使用Ant打包Android应用的系列文章.三篇文章.首篇具体介绍採用Ant打包Android应用的流程.列出部分定制问题及其解决方法,第二篇介绍我理解的Ant打包的思路与主要的概念和使 ...

  3. 构建自己的AngularJS - 作用域和Digest(三)

    作用域 第一章 作用域和Digest(三) $eval - 在当前作用域的上下文中运行代码 Angular有多种方式让你在当前作用域的上下文中运行代码.最简单的是$eval.传入一个函数当做其參数.然 ...

  4. poj3352Road Construction 边双连通+伪缩点

    /* 对于边双连通分支,求法更为简单. 仅仅需在求出全部的桥以后,把桥边删除.\ 原图变成了多个连通块,则每一个连通块就是一个边双连通分支. 桥不属于不论什么 一个边双连通分支,其余的边和每一个顶点都 ...

  5. NOIP2017提高组 模拟赛13(总结)

    NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...

  6. bzoj2229: [Zjoi2011]最小割(分治最小割+最小割树思想)

    2229: [Zjoi2011]最小割 题目:传送门 题解: 一道非常好的题目啊!!! 蒟蒻的想法:暴力枚举点对跑最小割记录...绝对爆炸啊.... 开始怀疑是不是题目骗人...难道根本不用网络流?? ...

  7. 查看typedef类型

    typedef unsigned long int NUM; #include <iostream> using namespace std; NUM x; cout << t ...

  8. @Query Annotation in Spring Data JPA--转

    原文地址:http://javabeat.net/spring-data-jpa-query/ In my previous post on Spring Data, I have explained ...

  9. hbase的命令

    1.1. 命令 名称 命令表达式 创建表 create '表名', '列族名1','列族名2','列族名N' 查看所有表 list 描述表 describe  ‘表名’ 判断表存在 exists  ' ...

  10. springMVC接受对象实体并且对象实体里面又有对象集合方式

    springMVC接受对象实体并且对象实体里面又有对象集合方式: Ajax: function add(){ var orders = [ { orderNo : "H222255" ...