bzoj1208Splay
Splay查前驱后继
小tips:在bzoj上while(scanf)这种东西可以让程序多组数据一起跑 反正没加我就t了
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int inf=1e9;
const int mod=;
inline int read()
{
int x=,f=;
char ch=getchar();
while(!isdigit(ch)){if(ch=='')f=-f;ch=getchar();}
while(isdigit(ch)){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct SplayTree
{
int rt,size;
int son[maxn][],f[maxn],val[maxn];
inline void Rotate(int x,int type)
{
int y=f[x];
son[y][!type]=son[x][type];
f[son[x][type]]=y;
f[x]=f[y];
if(f[x])son[f[y]][son[f[y]][]==y]=x;
son[x][type]=y;
f[y]=x;
}
inline void Splay(int x,int goal)
{
while(f[x]!=goal)
{
if(f[f[x]]==goal)
{
if(son[f[x]][]==x) Rotate(x,);
else Rotate(x,);
}
else
{
int y=f[x],z=f[y];
if(son[z][]==y)
{
if(son[y][]==x)Rotate(y,),Rotate(x,);
else Rotate(x,),Rotate(x,);
}
else
{
if(son[y][]==x)Rotate(y,),Rotate(x,);
else Rotate(x,),Rotate(x,);
}
}
}
if(goal==) rt=x;
}
inline void addnode(int fx,int &x,int a)
{
x=++size;
f[x]=fx;
val[x]=a;
son[x][]=son[x][]=;
}
inline void inittree()
{
size=;
addnode(,rt,-inf);
addnode(rt,son[rt][],inf);
}
inline void addNode(int a)
{
int x=rt;
while(son[x][val[x]<a]) x=son[x][val[x]<a];
addnode(x,son[x][val[x]<a],a);
Splay(size,);
}
inline void Delete(int a)
{
Splay(a,);
int tmp=son[rt][];
while(son[tmp][]) tmp=son[tmp][];
Splay(tmp,rt);
son[tmp][]=son[rt][];
f[son[rt][]]=tmp;
f[tmp]=;
rt=tmp;
}
inline int findnode(int a)
{
int x=rt;
while(x)
{
if(val[x]==a)return x;
if(val[x]>a)x=son[x][];
else x=son[x][];
}
return ;
}
inline int fx_min(int a)
{
int x=rt,minn=inf;
while(x)
{
if(val[x]==a) return a;
if(val[x]>a) minn=min(minn,val[x]);
if(val[x]>a) x=son[x][];
else x=son[x][];
}
return minn;
}
inline int fx_max(int a)
{
int x=rt,maxx=-inf;
while(x)
{
if(val[x]==a) return a;
if(val[x]<a) maxx=max(maxx,val[x]);
if(val[x]<a) x=son[x][];
else x=son[x][];
}
return maxx;
} }Splay;
int n,a,b,ans,Type[];
int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=,Type[]=Type[]=;
Splay.inittree();
for(int i=;i<=n;i++)
{
a=read(),b=read();
if(Type[!a]==)
{
Type[a]++;
Splay.addNode(b);
}
else
{
int minn=Splay.fx_min(b);
int maxx=Splay.fx_max(b);
ans=(ans+min(minn-b,b-maxx))%mod;
if(b-maxx<=minn-b)Splay.Delete(Splay.findnode(maxx));
else Splay.Delete(Splay.findnode(minn));
Type[!a]--;
}
}
cout<<ans%mod<<endl;
}
}
bzoj1208Splay的更多相关文章
- bzoj1208splay模板题
想试下新找的板子,没想到交上去CE了..懒得调..以后有机会就改 /* 用type标记当前树上的是宠物还是人 每次求前驱后缀,删掉最近的那个点 */ #include<iostream> ...
随机推荐
- PHPstudy如何在本地搭建多站点
参考地址: http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html
- C++模板类[初步]
/* * stacktp.h * * Created on: 2014年3月29日 * Author: */ /** * - 模板类的概念,与使用 * -# export template <c ...
- 九度OJ 1250:矩阵变换 (矩阵运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:95 解决:47 题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩 ...
- Lock和Condition
1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...
- ASP向上取整
<%Function Ceil(value) Dim return return = int(value) Cei2=value-return if Cei2>0 ...
- 页游手游服务器(一)c实现拓展lua网络
把工作几年服务器相关的部分内容,通过服务器解决方案,做一次总结.整个实现的主体是lua脚本,lua实现主要缺少的两大块:1网络部分2数据库部分这两部分必须通过c/c++做扩展先来做net,主要是服务器 ...
- GPS模块坐标偏差很大?
回答这个问题,首先要了解几个概念: 火星坐标系:天朝有关部门规定,为了保证国家安全,所有的地图公司提供的地图必须对实际的GPS坐标进行一定的偏移,偏移后的GPS坐标系俗称火星坐标系,而这个偏移是不固定 ...
- Arduino 看门狗使用
1.需要调用 #include <avr/wdt.h> 2.设置看门狗复位时间 wdt_enable(WDTO_2S); 代码时间定义的底层查看 #define WDTO_15MS 0 / ...
- CAS的实现Atomic类库
atomic 原子(atomic)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operation)意为"不可被中断的一个或一系列操作".在多 ...
- [原创]java WEB学习笔记10:GenericServlet
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...