……迷上平衡树……
【HNOI2004】宠物收养所
描述
最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。
每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。
1、被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。
2、收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。
一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。
你的任务:你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。
输入
第一行为一个正整数n,n <= 80000,表示一年当中来到收养所的宠物和领养者的总数。
接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
输出
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
样例输入
5
0 2
0 4
1 3
1 2
1 5
样例输出
3
提示
样例说明:样例的计算方法:(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
分析
啊啊啊啊,本宝宝好激动好激动,一次性AC,而且还都是自己想出来的,值得纪念!!!!!此处应有掌声【手动鼓掌】
但其实除了题面又长又臭以外,也就是一个披了件外套的平衡树板子题,没什么好得瑟的
我还是来稍微精简一下题意吧
你的程序需要
1.支持插入一个数
2.查询 x 的前驱和后继,并计算差值,得到最小的前驱或后继,若相同则记录前驱
大概就是这样咯,只是插入和查询,这两者是会交换的,不过不影响,咱们直接去代码上解释吧
代码
#include<bits/stdc++.h>
#define N 500000
#define P 1000000
#define in read()
#define inf (int)2e+9
using namespace std;
int n,m,ch[N][2],sze[N],rt=0,num[N],pri[N];
int a[N],tot=0;
inline int read(){
char ch;int f=1,res=0;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
while(ch>='0'&&ch<='9'){
res=(res<<3)+(res<<1)+ch-'0';
ch=getchar();
}
return f==1?res:-res;
}
void pushup(int x){ sze[x]=sze[ch[x][1]]+sze[ch[x][0]]+1;}
void rotate(int &rt,int t){
int x=ch[rt][t];
ch[rt][t]=ch[x][t^1];
ch[x][t^1]=rt;
pushup(rt);pushup(x);
rt=x;
}
void insert(int &rt,int x){
if(rt){
int t=x>a[rt];
insert(ch[rt][t],x);
if(pri[ch[rt][t]]<pri[rt]) rotate(rt,t);
}
else{
rt=++tot;
a[tot]=x;sze[tot]=1;pri[tot]=rand();
}
pushup(rt);
}
void del(int &rt,int x){
if(a[rt]==x){
if(!ch[rt][0]&&!ch[rt][1]){
rt=0;
return;
}
rotate(rt,pri[ch[rt][0]]<pri[ch[rt][1]]);
del(rt,x);
}
else del(ch[rt][x>a[rt]],x);
pushup(rt);
}
int findpre(int rt,int x){
if(!rt) return -inf;
if(x>a[rt]) return max(a[rt],findpre(ch[rt][1],x));
else return findpre(ch[rt][0],x);
}
int findsuf(int rt,int x){
if(!rt) return inf;
if(x>=a[rt]) return findsuf(ch[rt][1],x);
else return min(a[rt],findsuf(ch[rt][0],x));
}
int main(){
n=in;long long sum=0;
srand(time(0));
int first=-1,x,y;
while(n--){
x=in;y=in;
if(first==-1) first=x;
if(x==first) insert(rt,y);
else{
//to find b's prefix,b's suffix
int aa=findpre(rt,y),bb=findsuf(rt,y);
if(aa==-inf&&bb==inf){//that's to say there is no more pets or people on the waiting list
first=x;
insert(rt,y);
continue;
}//here we exchange insert and query
int cha1=abs(aa-y),cha2=abs(bb-y);
if(cha1==cha2){
if(aa>bb) swap(bb,aa);//keep aa the smallest
sum=(sum+y-aa)%P;
del(rt,aa);
continue;
}
else{
if(cha1<cha2){
sum=(sum+cha1)%P;
del(rt,aa);//cause next time you can't adopt the same pet,so you have to delete it after it has been adopted
}
else {
sum=(sum+cha2)%P;
del(rt,bb);
}
}
}
}
printf("%lld",sum);
return 0;
}