题目:

洛谷也能评


题解:

记录一下当前树维护是宠物还是人,用Splay维护插入和删除.

对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对值小就好啦

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 800010
#define MOD 1000000
#define which(x) (ls[fa[(x)]]==(x))
typedef long long ll;
using namespace std;
int n,root,tot,val[N],fa[N],ls[N],rs[N],sze[N],cnt[N],flag,op,value;
ll ans;
int read()
{
int ret=,neg=;
char j=getchar();
for (;j>'' || j<'';j=getchar())
if (j=='-') neg=-;
for (;j>='' && j<='';j=getchar())
ret=ret*+j-'';
return ret*neg;
}
void Upt(int x) {sze[x]=sze[ls[x]]+sze[rs[x]]+;}
void Rotate(int u)
{
int v=fa[u],w=fa[v],b=which(u)?rs[u]:ls[u];
if (w) which(v)?ls[w]=u:rs[w]=u;
which(u)?(ls[v]=b,rs[u]=v):(rs[v]=b,ls[u]=v);
fa[u]=w,fa[v]=u;
if (b) fa[b]=v;
Upt(v),Upt(u);
}
void Splay(int x)
{
while (fa[x])
{
if (fa[fa[x]])
if (which(x)==which(fa[x])) Rotate(fa[x]);
else Rotate(x);
Rotate(x);
}
root=x;
}
void Insert(int x)
{
int cur=root,v=;
while (cur)
if (x<val[v=cur]) cur=ls[cur];
else cur=rs[cur];
fa[++tot]=v,val[tot]=x,sze[tot]=;
if (v) x<val[v]?ls[v]=tot:rs[v]=tot;
Splay(tot);
}
int Find(int x)
{
int cur=root,v=;
while (cur && val[cur]!=x)
if (x<val[v=cur]) cur=ls[cur];
else cur=rs[cur];
return cur?cur:v;
}
int getmin(int x)
{
while (ls[x]) x=ls[x];
return x;
}
int getmax(int x)
{
while (rs[x]) x=rs[x];
return x;
}
int getpre(int x)
{
int u=Find(x);
Splay(u);
if (val[u]<=x) return u;
return getmax(ls[u]);
}
int getnxt(int x)
{
int u=Find(x);
Splay(u);
if (val[u]>=x) return u;
return getmin(rs[u]);
}
void Erase(int x)
{
Splay(x);
if (ls[x]== && rs[x]==) root=;
else
if (ls[x]== || rs[x]==) root=ls[x]+rs[x],fa[root]=;
else{ fa[ls[x]]=;
int v=getmax(ls[x]);
Splay(v);
rs[v]=rs[x],fa[rs[x]]=v,Upt(root);
}
}
int main()
{
n=read();
for (int i=;i<=n;i++)
{
op=read(),value=read();
if (sze[root]==)
Insert(value),flag=op;
else if (op!=flag)
{
int u=getpre(value),v=getnxt(value);
if (u!= && (v== || value-val[u]<=val[v]-value))
ans=(ans+value-val[u])%MOD,Erase(u);
else ans=(ans+val[v]-value)%MOD,Erase(v);
}
else Insert(value);
}
printf("%lld",ans);
return ;
}

最新文章

  1. php 数组动态添加实现代码(最土团购系统的价格排序)
  2. cairo-1.14.6 static compiler msys mingw32
  3. Mac系统修改Intellij Idea默认JDK版本
  4. Java 第13章 带参数的方法
  5. 支付宝支付后回调通知中responseTxt=true isSign=False可能的问题
  6. Javascript的一种代码结构方式——插件式
  7. Git版本管理:Windows下Git配置与使用指南
  8. C#设置按钮三态背景图片
  9. Java SpringMVC 定时任务
  10. 好用的开源库(二)——uCrop 图片裁剪
  11. ORACLE升级PSU&amp;OJVM注意的问题及遇到问题解决思路
  12. Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
  13. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理
  14. 前端-JavaScript1-4——JavaScript之变量
  15. 避免resolv.conf设置被覆盖
  16. You have tried to change the API from what has been previously approved.
  17. Field &#39;***********&#39; doesn&#39;t have a default value
  18. manifest.xml
  19. Android之MVC——Model通知View去更新(实用)
  20. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

热门文章

  1. 基础篇(1):c++程序基本结构
  2. SSH &amp; 文件传输 &amp; 远程桌面管理
  3. FreeBSD--如何最有效率的安装软件
  4. C语言进阶——const 和 volatile 分析09
  5. urllib使用一
  6. PHP.21-商品信息管理
  7. MySQL之查询性能优化(四)
  8. nohup 重定向的问题-- 费元星 站长
  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目5
  10. win7 64位如何共享XP上的打印机?