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. React antd嵌入百度编辑器(css加载不到等问题,'offsetWidth' of null)

    之前有看过一些类似的文章,以为嵌入不会遇到太多坑 结果...    其他不说,先来描述下跳坑的过程 先定义Ueditor.js类,这个和网上版本类似 import React, { Component ...

  2. 【BZOJ4016】[FJOI2014]最短路径树问题 最短路径树+点分治

    [BZOJ4016][FJOI2014]最短路径树问题 Description 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径 ...

  3. sqlserver删除所有表

    --/第1步**********删除所有表的外键约束*************************/ DECLARE c1 cursor for select 'alter table ['+ o ...

  4. 【python】-- 面向对象引子、概念

    面向过程编程 1.编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马 ...

  5. 【译】快速高效学习Java编程在线资源Top 20

    想要加强你的编程能力吗?想要提升你的 Java 编程技巧和效率吗? 不用担心.本文将会提供快速高效学习 Java 编程的 50 多个网站资源: 开始探索吧: 1.MKyong:许多开发者在这里可以找到 ...

  6. Django之CURD插件2

    目标:达到下图拥有功能的实现 1.绑定编辑按钮 ************思路**************** 1.为编辑按钮添加样式,可以根据样式来进行判断在什么状态. 2.进入编辑模式,将可编辑的字 ...

  7. oracle chain

    [oracle@tyger dbs]$ sqlplus / as sysdba SQL*Plus: Release 10.2.0.1.0 - Production on Tue May 6 13:02 ...

  8. 聊聊数据库~6.SQL运维中篇

    上篇回顾:https://www.cnblogs.com/dotnetcrazy/p/10810798.html#top 1.6.5.MySQL日志相关 本文的测试环境:MySQL5.7.26.Mar ...

  9. spring+hibernate+springmvc整合框架搭建

    搭建maven web项目这里不再讲述,详情请查看http://www.cnblogs.com/wql025/p/5215570.html 现在讲述项目的搭建过程. 1.通过maven导入项目所用的j ...

  10. Call method 的使用

    SAP学习日志---Call method 的使用 以及常见错误 转载▼   可以通过以下方法 call method 1. 进入全局类中 找到方法,拖到程序中 2. 使用pattern 中的 AAB ...