BZOJ 1208 宠物收养所 Splay树

2024-06-15 11:18
文章标签 宠物 bzoj splay 1208 收养

本文主要是介绍BZOJ 1208 宠物收养所 Splay树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Splay的简单应用,找和一个数最接近的数,例如找和x最接近的数,把x旋转到根,要么是左子树的最大值,要么是右子树的最小值。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int mod = 1000000;
const int maxn = 80010;
const int INF = 999999999;
struct Splay
{
int pre[maxn], ch[maxn][2], sz[maxn];
int root, top;
int val[maxn];
void pushup(int rt)
{
sz[rt] = sz[ch[rt][0]] + sz[ch[rt][1]] + 1;
}
void init()
{
pre[0] = ch[0][0] = ch[0][1] = sz[0] = 0;
root = top = 0;
}
void rotate(int x, int d)
{
int y = pre[x];
ch[y][d^1] = ch[x][d];
pre[ch[x][d]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1] == y] = x;
pre[x] = pre[y];
ch[x][d] = y;
pre[y] = x;
pushup(y);
}
void splay(int x, int goal)
{
while(pre[x] != goal)
{
if(pre[pre[x]] == goal)
{
rotate(x, ch[pre[x]][0] == x);
}
else
{
int y = pre[x], z = pre[y];
int d = (ch[z][0] == y);
if(ch[y][d] == x)
{
rotate(x, d^1);
rotate(x, d);
}
else
{
rotate(y, d);
rotate(x, d);
}
}
}
if(goal == 0)
root = x;
pushup(x);
}
void NewNode(int &x, int f, int c)
{
x = ++top;
ch[x][0] = ch[x][1] = 0;
sz[x] = 1;
pre[x] = f;
val[x] = c;
}
void insert(int k)
{
int x = root;
if(x == 0)
{
NewNode(root, 0, k);
return;
}
while(ch[x][k>val[x]])
x = ch[x][k>val[x]];
NewNode(ch[x][k>val[x]], x, k);
splay(ch[x][k>val[x]], 0);
pushup(x);
}
int get_min(int x)  
{  
if(!x)  
return 0;  
while(ch[x][0])  
{  
x = ch[x][0];  
}  
return x;  
}  
int get_max(int x)  
{  
if(!x)  
return 0;  
while(ch[x][1])  
{  
x = ch[x][1];  
}  
return x;  
}  
void remove(int x)  
{  
splay(x, 0);
if(!ch[root][0] && !ch[root][1])
{
root = 0;
return;
}
if(!ch[root][0])  
{  
root = ch[root][1];  
pre[root] = 0;
}  
else
{  
int m = get_max(ch[root][0]);  
splay(m, root);  
ch[m][1] = ch[root][1];  
pre[ch[root][1]] = m;  
root = m;  
pre[root] = 0;  
pushup(root);
}  
}  
int find(int x)
{
int rt = root;
while(rt)
{
if(val[rt] == x)
return rt;
rt = ch[rt][x>val[rt]];
}
return rt;
}
LL cal(int x)
{
int rt = find(x);
LL ans = 0;
if(rt != 0)
{
remove(rt);
return 0;
}
insert(x);
int l = ch[root][0], r = ch[root][1];
while(l && ch[l][1])
l = ch[l][1];
while(r && ch[r][0])
r = ch[r][0];
LL d1 = INF, d2 = INF;
if(l)
d1 = x-val[l];
if(r)
d2 = val[r]-x;
if(d1 <= d2)
{
remove(root);
remove(l);
return d1;
}
else
{
remove(root);
remove(r);
return d2;
}
}
}A, B;
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
LL ans = 0;
A.init();
B.init();
for(int i = 1; i <= n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if(!x)
{
if(B.sz[B.root] == 0)
A.insert(y);
else
ans += B.cal(y);
}
else
{
if(A.sz[A.root] == 0)
B.insert(y);
else
ans += A.cal(y);
}
ans %= mod;
}
printf("%lld\n", ans);
}
return 0;
}

这篇关于BZOJ 1208 宠物收养所 Splay树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063296

相关文章

基于SpringBoot的宠物服务系统+uniapp小程序+LW参考示例

系列文章目录 1.基于SSM的洗衣房管理系统+原生微信小程序+LW参考示例 2.基于SpringBoot的宠物摄影网站管理系统+LW参考示例 3.基于SpringBoot+Vue的企业人事管理系统+LW参考示例 4.基于SSM的高校实验室管理系统+LW参考示例 5.基于SpringBoot的二手数码回收系统+原生微信小程序+LW参考示例 6.基于SSM的民宿预订管理系统+LW参考示例 7.基于

除猫毛应该用哪款宠物空气净化器?希喂、安德迈哪款更值得推荐

自从我的朋友也养了猫之后,我和她能讨论的话题就更多了,每天都在分享自家的猫咪今天干了什么可爱的事,一起探讨应该怎么让猫咪胖起来,每天撸都撸不够,好想时时刻刻和猫咪待在一起。 但她说到,本来这种生活挺好的,但是自从养了猫之后,家里的各个角落都开始有猫咪的毛发,每天都得清理,而且还有这个排便时的臭味,家里简直就是无法忍受,导致现在家里的氛围就更差了,她婆婆每天都在担心养了猫之后对家里人造成健

【BZOJ】1324 Exca王者之剑 最大权独立集

传送门:【BZOJ】1324  Exca王者之剑 题目分析:赤裸裸的最大权独立集。。。最小割解决 代码如下: #include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , a , b ) for ( int

【BZOJ】1026: [SCOI2009]windy数 数位DP

传送门:【BZOJ】1026: [SCOI2009]windy数 题目分析:数位DP水题。 代码如下: #include <stdio.h>#include <cstring>#include <algorithm>#define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )#define For( i ,

【BZOJ】2152: 聪聪可可 点分治

传送门:【BZOJ】2152: 聪聪可可 题目分析:记录权值和%3的路径的个数。。。然后去重。。没了。。 代码如下: #include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std ;typedef lo

Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers

对应POJ 题目:点击打开链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 72765 Accepted: 22465Case Time Limit: 2000MS Description You have N integers, A1

Splay树(区间添加删除 | 区间翻转)——HDU 3487 Play with Chain

对应HDU题目:点击打开链接 Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4571    Accepted Submission(s): 1859 Problem Descript

SprinBoot+Vue宠物领养救助微信小程序的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue3.6 uniapp代码 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍:CSDN认证博客专家,CSDN平台Java领域

宠物空气净化器真的有用吗?哪款能真的起到吸毛除臭效果

现在养猫养狗已经成为了一个浪潮,甚至有人发了一张对比图:在店门口,偏年老一代的就是抱着小孩,但年轻一辈的就是抱着宠物。虽然说不管是养猫养狗还是养小孩都是自己的选择,但是显而易见的就是现在大街上宠物出现的频率明显变多,对于大部分独居青年而言,养宠物反而是一个精神寄托,甚至成为精神支撑,支撑着这些年轻人在城市里打拼。 对于我而言,之前养猫就是觉得一个人太孤独,回到家后发现家里并没有什么能和我

宠物空气净化器真的有用吗?去浮毛好用的宠物空气净化器推荐

不知不觉我已经养宠五年了,一人两猫作伴的日子充满着幸福,可猫毛的存在偶尔也会让小家出现裂缝。每当换毛季,我的鼻子就率先作出反应,瘙痒加上止不住喷嚏都在反映着不佳的空气质量。这都是因为猫咪疯狂掉毛,浮毛上附着的细菌随着呼吸进入到人体,引起各种反应。 朋友在看到后,推荐我买台宠物空气净化器来改善家中的环境。相信很多人和我一样,对宠物空气净化器了解不是很多,更别提知道它的功能了。为了可以减轻家中猫毛打