bzoj 1208 splay模板题2
自己yy了找前驱和后继,学了学怎么删除。。。(反正就是练模板)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 80005
#define lc(x) ch[x][0]
#define rc(x) ch[x][1]
#define inf 0x3f3f3f3f
using namespace std;
int n,k[N*],size,ch[N*][],root,cnt,fa[N];
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;
}
}
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]=++cnt;
fa[cnt]=x;k[cnt]=v;splay(cnt);
}
int now1,now2;
int pre(int x,int v)
{
int tmp=-inf;
while(ch[x][k[x]<v])
{
if(k[x]<=v)if(k[x]>tmp)tmp=k[x],now1=x;
x=ch[x][k[x]<v];
}if(k[x]<=v)if(k[x]>tmp)tmp=k[x],now1=x;
return tmp;
}
int suc(int x,int v)
{
int tmp=inf;
while(ch[x][k[x]<v])
{
if(k[x]>=v)if(k[x]<tmp)tmp=k[x],now2=x;
x=ch[x][k[x]<v];
}if(k[x]>=v)if(k[x]<tmp)tmp=k[x],now2=x;
return tmp;
}
void del(int x)
{
splay(x);
if(!ch[x][])fa[ch[x][]]=,root=ch[x][];
else if(!ch[x][])fa[ch[x][]]=,root=ch[x][];
else
{
fa[ch[x][]]=;
int tmp=ch[x][];while(ch[tmp][])tmp=ch[tmp][];
splay(tmp);
ch[tmp][]=ch[x][];fa[ch[x][]]=tmp;
}
return ;
}
int main()
{
scanf("%d",&n);
cnt=;root=;
k[]=inf;int ans=;
insert(root,-inf);
size=;int shu=;
for(int i=;i<=n;i++)
{
int t1,t2;scanf("%d%d",&t1,&t2);
if(!t1)
{
if(!shu||size==)
{
insert(root,t2);
shu=;size++;
}
else
{
size--;
int qq=pre(root,t2),ww=suc(root,t2);
if(abs(qq-t2)<=abs(ww-t2))
{
ans+=abs(qq-t2);del(now1);
}
else
{
ans+=ww-t2;del(now2);
}
}
}
else
{
if(shu||size==)
{
insert(root,t2);
shu=;size++;
}
else
{
size--;
int qq=pre(root,t2),ww=suc(root,t2);
if(abs(qq-t2)<=abs(ww-t2))
{
ans+=abs(qq-t2);del(now1);
}
else
{
ans+=ww-t2;del(now2);
}
}
}
ans%=;
}
printf("%d\n",ans);
return ;
}
bzoj 1208 splay模板题2的更多相关文章
- bzoj 1588 splay模板题
用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...
- bzoj 3224 splay模板题4
再刷水题我就废了... #include<iostream> #include<cstdio> #include<algorithm> #include<cs ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
- 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...
- 【BZOJ 3188】【Coci 2011】Upit Splay模板题
转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...
- BZOJ 1588 平衡树 模板题
Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int si ...
- BZOJ 3224 平衡树模板题
Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int n, ...
- PKU-3580 SuperMemo(Splay模板题)
SuperMemo 题目链接 Your friend, Jackson is invited to a TV show called SuperMemo in which the participan ...
随机推荐
- 移动开发webapp开发常用meta设置手机浏览器全屏模式
1.WebApp全屏模式: <meta name="viewport" content="width=device-width,initial-scale=1.0, ...
- Xcode基本操作
2.偏好设置 通过“command+,”快捷键或”Xcode|Preferences”菜单呼出偏好设置. (1)主题及字体(Preferences->Fonts & Colors) 选中 ...
- Jenkins 安装
1. 调整tomcat的jvm配置, 以免内存不够, 在catalina.sh中 #!bin/sh下添加 JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX ...
- SHGetFileInfo函数详解
SHGetFileInfo函数: WINSHELLAPI DWORD WINAPI SHGetFileInfo( LPCTSTR pszPath, DWORD dwFileAttributes, SH ...
- ubuntu13.04环境hadoop1.2.1单机模式安装
一.虚拟机上安裝ubuntun 13.04 中文版 当然,你要是习惯看英文版,也可以直接安装英文版.老老实实从官网下载安装即可,安装系统不是本文的重点.这里只提一个注意事项:新手安装前,切记断网,因为 ...
- Windows 8的本地化应用程序清单
I need to localize some data in application manifest (like name, description, splashscreen images et ...
- Alpha版本项目展示要求
Alpha版本展示的时间暂定为11月17日课上.如有变动,另行通知. 在Alpha阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写 ...
- 分析cocos2d-x中的CrystalCraze示例游戏
cocos2d-x自带了不少示例,以及几个比较简单的游戏,不过这些游戏都是用javascript binding(SpiderMonkey)做的,所以我猜测javascript binding可能是c ...
- ICSharpCode.SharpZipLib
ICSharpCode.SharpZipLib 压缩.解压文件 附源码 http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZipli ...
- 工作随笔——使用svnsync实时备份SVN版本库
前段时间把SVN版本库从win迁移到了Linux上,没隔几天那台win的磁盘就严重坏道了....这TMD什么运气! 花费了点时间研究了下svn自己的同步工具.写个日志记录下. 注意:svnsync要求 ...