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> ...
随机推荐
- WebView加载URL跳转到系统浏览器的解决方法
1.问题 webview加载url跳转到系统浏览器,用户体验非常的差 2.解决方法 重写WebViewClient的shouldOverrideUrlLoading(WebView view, Str ...
- Cadence 15.7 win7无法启动解决方法
原帖地址:http://blog.sina.com.cn/s/blog_69a5dce90100kscf.html 按照XP下的破解方法安装Cadence15.7后, 如果不能正常启动Cadence ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- WebApi 中使用 Token
1.登陆的时候根据用户信息生成Token var token = FormsAuthentication.Encrypt( new FormsAuthenticationTicket( , " ...
- org.apache.poi3.1.7 Excle并发批量导入导出
org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...
- [php]Maximum function nesting level of '100' reached错误
今天在做后台一个模块的时候报出了这个错误. Maximum function nesting level of '100' reached 仔细分析之后发现是在类的初始化过程中(__construct ...
- android 中使用svg
http://www.see-source.com/blog/300000038/1189.html http://www.jianshu.com/p/30dfa5920658#
- 变分推断(Variational Inference)
变分 对于普通的函数f(x),我们可以认为f是一个关于x的一个实数算子,其作用是将实数x映射到实数f(x).那么类比这种模式,假设存在函数算子F,它是关于f(x)的函数算子,可以将f(x)映射成实数F ...
- 从分布式锁来看redis和zookpeer!
从分布式锁来看redis和zookpeer! 目前网上大部分的基于zookpeer,和redis的分布式锁的文章都不够全面.要么就是特意避开集群的情况,要么就是考虑不全,读者看着还是一脸迷茫.坦白说, ...
- zookeeper学习与实战(二)集群部署
上一篇介绍了单机版zookeeper安装,这种情况一般用于开发测试.如果是生产环境建议用分布式集群部署,防止单点故障,增加zookeeper服务的高可用. [环境介绍] 三台机器:192. ...