题意:一家宠物收养所负责处理领养者与遗弃宠物业务,有人来领宠物,则领一只最理想的。若没有宠物了,领养者们就得等到宠物来,宠物一来立刻送给其中一个等待者。如果有两个理想的选择,则选择那个值较小的。收养所时刻只存在人or宠物or没有存在任何!

思路:

  这题主要是树中可能存在人或者宠物,但两者不可能同时存在,因为一旦存在,必须有对象被配对并从树中删除。 所有函数独自写起来都是很简单的,但是合起来使用时就得小心了,得配合一点。

  要时刻注意树中是否还有节点,若没有,则可能要树要换种类了。

 #include <bits/stdc++.h>
#define pii pair<int,int>
#define INF 0x3f7f7f7f
#define LL long long
using namespace std;
const int N=;
const int mod=; struct node
{
int key, pre, ch[];
}nod[N];
int node_cnt, root, flag; int create_node(int v,int far)
{
nod[node_cnt].ch[]=nod[node_cnt].ch[]=;
nod[node_cnt].pre=far;
nod[node_cnt].key=v;
return node_cnt++;
} void Rotate(int t,int d)
{
int son=nod[t].ch[d];
int far=nod[t].pre;
int gra=nod[far].pre;
nod[t].pre=gra;
nod[far].pre=t;
nod[son].pre=far;
nod[t].ch[d]=far;
nod[far].ch[d^]=son;
nod[gra].ch[ nod[gra].ch[]==far ]=t;
} int Insert(int t,int v)
{
if(t==) return root=create_node(v, ); //树中还没有节点。
if( v < nod[t].key )
{
if(nod[t].ch[]) return Insert(nod[t].ch[], v);
else return nod[t].ch[]=create_node(v, t);
}
else
{
if(nod[t].ch[]) return Insert(nod[t].ch[], v);
else return nod[t].ch[]=create_node(v, t);
}
} void Splay(int t, int goal)
{
while(nod[t].pre!=goal)
{
int f=nod[t].pre, g=nod[f].pre;
if(g==goal) Rotate( t, nod[f].ch[]==t );
else
{
int d1=nod[f].ch[]==t, d2=nod[g].ch[]==f;
if(d1==d2) Rotate(f, d1),Rotate(t, d1);
else Rotate(t, d1),Rotate(t, d2);
}
}
if(goal==) root=t; //随时更新根!
} int Find(int t,int v) //在子树t中找到值为v的点,返回点号
{
while(t)
{
if(v==nod[t].key) return t;
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
return ; //找不到
} int Find_pre(int t,int v)
{
int val=-;
while(t)
{
if(nod[t].key<v) val=max(val, nod[t].key);
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
if(val<) return INF;
return val;
} int Find_bac(int t,int v)
{
int val=INF;
while(t)
{
if(nod[t].key>v) val=min(val, nod[t].key);
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
return val;
} void Delete(int v) //在树中删除值为v的任意一个点
{
Splay(Find(root, v), ); //将目标旋转到根
int L=nod[root].ch[];
int R=nod[root].ch[];
if(L== && R==) root=;
else if(R==) //没有右子树
{
nod[L].pre=;
root=L;
}
else if(L==) //没有左子树
{
nod[R].pre=;
root=R;
}
else //有前有后
{
while(nod[R].ch[]) R=nod[R].ch[]; //找到后继
Splay(R, root);
nod[R].ch[]=L;
nod[R].pre=;
nod[L].pre=R;
root=R;
}
} int main()
{
//freopen("input.txt", "r", stdin);
int n, ans, a, b, contain;
while(cin>>n)
{
flag=node_cnt=;
contain=root=ans=;
while(n--)
{
scanf("%d%d",&a,&b);
if(a==flag || contain==) // 树为空 or 树中是同种类 则 插入
{
flag=a;
contain++;
Splay(Insert(root, b), );
}
else
{
if(Find(root, b)) Delete(b); //刚好有值为b的。
else
{
int L=Find_pre(root, b);
int R=Find_bac(root, b);
if( abs(L-b)<=abs(R-b) ) //L和R不可能同时为INF。
{
ans=(ans+abs(L-b))%mod;
Delete(L);
}
else
{
ans=(ans+abs(R-b))%mod;
Delete(R);
}
}
contain--;
}
}
printf("%d\n", ans );
}
return ;
}

AC代码

HYSBZ 1208 宠物收养所 (Splay树)的更多相关文章

  1. bzoj 1208 宠物收养所--splay

    这个题也是单点维护,不管来的是人还是狗,只要num=0就插入,否则就删除. // File Name: ACM/bzoj/1208.cpp // Author: Zlbing // Created T ...

  2. BZOJ 1208 宠物收养所 | 平衡树模板题

    BZOJ 1208 宠物收养所 我犯过的错误:删除一个节点后没有update新的根节点,导致size错了! #include <cstdio> #include <cmath> ...

  3. Bzoj 1208: [HNOI2004]宠物收养所(splay)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...

  4. 【BZOJ-1208】宠物收养所 Splay

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6638  Solved: 2601[Submit][Sta ...

  5. 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...

  6. BZOJ 1208 宠物收养所

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  7. BZOJ 1208 宠物收养所 set+二分

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题目大意: 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠 ...

  8. [bzoj1208][HNOI2004]宠物收养所——splay

    题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...

  9. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

随机推荐

  1. hdu 4302 Holedox Eating(优先队列/线段树)

    题意:一只蚂蚁位与原点,在x轴正半轴上会不时地出现一些蛋糕,蚂蚁每次想吃蛋糕时选取最近的去吃,如果前后距离相同,则吃眼前的那一块(即方向为蚂蚁的正前),求最后蚂蚁行进距离. 思路:优先队列q存储蚂蚁前 ...

  2. Android 增,删,改,查 通讯录中的联系人

    一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...

  3. 出现Insufficient space for shared memory file错误解决

    今天在linux下敲命令,出现上面的错误,原来是临时文件目录(/tmp)下的空间不够了,df一看/下100%了.

  4. nginx下laravel框架rewrite的设置

    nginx下laravel框架rewrite的设置 百牛信息技术bainiu.ltd整理发布于博客园 在nginx的vhost站点配置文件中加入以下内容即可 1 2 3 4 5 6 7 8 9 10 ...

  5. 6-12 SVM小结

    介绍了SVM的概念以及如何利用SVM进行一个身高体重的训练和预测.如果类别比较简单的话,那么在二维空间上它有可能就是一条直线.如果类别比较复杂,那么投影到高维空间上它就是一个超平面.所以SVM的本质它 ...

  6. 利用ffmpeg0.6.1把.h264纯码流打包成.mp4 .avi等格式 (转载)

    转自:http://cache2.weidaohang.org/h/index.php?q=aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cWluZ183MzkvYXJ0aWNsZS ...

  7. Spring Boot 学习系列(序)—Spring Boot

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Spring Boot? Spring Boot 是由pivotal团队提供的一个基于Spring的全新框架 ...

  8. 51nod 1133【贪心】

    思路: 按照终点升序,然后遍历一下就好了: #include <bits/stdc++.h> using namespace std; typedef long long LL; cons ...

  9. Codeforces277A 【dfs联通块】

    题意: 给出n个人会的语言类型,然后问这n个人里面还需要几个人学习一下语言就可以n个直接互通了.a会1,2,b会2,3,c会4,那么只要C学一下1或者2,或者3就好了...大致就是这个意思. 思路: ...

  10. Cg(C for Graphic)语言表达式与控制语句(转)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 在上一章中,我们已经介绍了 Cg 语言的 ...