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的更多相关文章

  1. bzoj1208splay模板题

    想试下新找的板子,没想到交上去CE了..懒得调..以后有机会就改 /* 用type标记当前树上的是宠物还是人 每次求前驱后缀,删掉最近的那个点 */ #include<iostream> ...

随机推荐

  1. 【BZOJ4184】shallot 线段树+vector+线性基

    [BZOJ4184]shallot Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从 ...

  2. java Web程序使用wro4j合并、压缩js、css等静态资源

    在Web项目中,js.css合并压缩,不仅有利于减少Http请求数量.减少宽带资源占用,还能有效的管理各种js.css的引入,使整个项目更加有序.而对于访问用户来说,其更大的好处是增加了页面的打开速度 ...

  3. 1.Python学习---helloworld

    1.首先访问http://www.python.org/download/去下载最新的python版本. 2.安装下载包,一路next. 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...

  4. C语言之基本算法12—谁是冠军

    /* ================================================================== 题目:甲乙丙丁四人猜A,B,C,D,E,F6个人谁是冠军,甲 ...

  5. TensorFlow 初级教程(三)

    TensorFlow基本操作 import os import tensorflow as tf os.environ[' # 使用TensorFlow输出Hello # 创建一个常量操作( Cons ...

  6. sin6_addr打印:string to sockaddr_in6 and sockaddr_in6 to string

    函式原型: #include <arpa/inet.h> const char *inet_ntop(int af, const void *src, char *dst, socklen ...

  7. secureCRT linux shell显示中文乱码 解决方法

    引:有没有这样的经历: 1.在shell中直接查看包含中文的文件时,出现一堆火星文,不得不下载下来window看. 2.无法正常的在shell中输入中文. 3.make的时候输出一堆乱码. 以下是查阅 ...

  8. Django模型系统——ORM

    一.概论 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描 ...

  9. ruby on rails 生产环境调试项目日志查看

    1.项目目录:log/production.log 2.nginx日志:/opt/nginx/logs 生产环境下做的任何更改都要重启服务器 重启 sudo kill $(cat /opt/nginx ...

  10. JS实现下拉列表的二级联动

    这个是简单也是最基本的下拉框联动的示例,这个示例主要针对那些只有二级联动,且第一级是固定的选项,第二级的内容也比较简单,不刷新的联动,动态的联动需要检索数据库,这个对不需要更新的二级联动比较实用.这里 ...