凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。
每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。
被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。
收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。
一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。
你得到了一年当中,领养者和被收养宠物到来收养所的情况,请你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

输入

从文件input.txt中读入数据。文件的第一行为一个正整数n,n<=80000,表示一年当中来到收养场的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养场的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

输出

输出文件output.txt中仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

样例输入

5 0 2 0 4 1 3 1 2 1 5

样例输出

3

提示

abs(3-2) + abs(2-4)=3,

最后一个领养者没有宠物可以领养。

题解:

裸的Treap找前驱后继,本蒟蒻开了两个平衡树,如果宠物Treap为空就在人的Treap里插人,人的Treap就在宠物的Treap里插宠物。

加一些判断再套个模版就ok了.

具体看代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int mod=;
const int N=;
struct node
{
node *child[];
int x,lev;
}a[N];
node *pos,*root[];
int size[];ll ans=;
int gi()
{
int str=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
} void newnode(node *&r,int key)
{
r=pos++;
r->child[]=r->child[]=NULL;
r->lev=rand();
r->x=key;
}
int pre,nxt;
void rotate(node *&r,bool t)//0 ->left 1 ->right
{
node *y=r->child[!t];
r->child[!t]=y->child[t];
y->child[t]=r;
r=y;
}
void insert(node *&r,int key)
{
if(r==NULL){newnode(r,key);return;}
bool t=r->x<key;
insert(r->child[t],key);
if(r->child[t]->lev>r->lev)rotate(r,!t);
}
void getspre(node *&r,int key)
{
if(r==NULL)return ;
if(r->x>key)getspre(r->child[],key);
else pre=r->x,getspre(r->child[],key);
} void getnext(node *&r,int key)
{
if(r==NULL)return ;
if(r->x<key)getnext(r->child[],key);
else nxt=r->x,getnext(r->child[],key);
} void Delet(node *&r,int key)
{
if(r==NULL)return ;
if(r->x==key)
{
if(r->child[] && r->child[])
{
bool t=r->child[]->lev<r->child[]->lev;
rotate(r,t);
Delet(r->child[t],key);
}
else
{
if(r->child[])r=r->child[];
else r=r->child[];
}
}
else Delet(r->child[key>r->x],key);
}
int main()
{
pos=a;
int n,x,y;
n=gi();
for(int i=;i<=n;i++)
{
x=gi();y=gi();
if(size[!x]==)
{
size[x]++;
insert(root[x],y);
}
else
{
pre=nxt=-;
getnext(root[!x],y);getspre(root[!x],y);
size[!x]--;
if(pre!=- && nxt!=-)
{
if(y-pre<=nxt-y){
ans+=y-pre;ans%=mod;
Delet(root[!x],pre);
}
else{
ans+=nxt-y;ans%=mod;
Delet(root[!x],nxt);
}
}
else
{
if(pre!=-){
ans+=y-pre;ans%=mod;
Delet(root[!x],pre);
}
else{
ans+=nxt-y;ans%=mod;
Delet(root[!x],nxt);
}
}
}
}
cout<<ans;
return ;
}

最新文章

  1. vue-router
  2. poj 1141
  3. HttpHandler过滤请求..
  4. windows调试器尝鲜
  5. [Hibernate] - many to many
  6. Linux系统信息查看命令
  7. 如何制作和部署war包
  8. sql 将一个表中的数据插入到另一个表中
  9. 使用jquery插件报错:TypeError:$.browser is undefined的解决方法
  10. 中文变英文字母(ios)
  11. 与IO相关的等待事件troubleshooting-系列9
  12. 大牛对ACM入门菜鸟的一些话
  13. git - 简明指南(转)
  14. ASP.NET MVC局部视图
  15. Linux防火墙配置—允许转发
  16. C# 接口使用方法
  17. Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)
  18. &lt;ROS&gt; 机器人描述--URDF和XACRO
  19. Latex citation using natbib and footnotesize
  20. Windows下dump文件生成与分析

热门文章

  1. 自动化测试工具Appium环境搭建
  2. scanf函数之扫描集
  3. Office 365 开发概览系列文章和教程
  4. windows phone 8.1开发:文件选择器FileSavePicker
  5. vueJS 获取后台数据 绑定data
  6. Logistic Regression理论总结
  7. 【2017-03-24】CSS样式表
  8. ad_封装_ads828
  9. calling c++ from golang with swig--windows dll 二
  10. 应不应该使用inline-block代替float