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树相关推荐

  1. HYSBZ - 1208 宠物收养所(Splay)

    题目链接:点击查看 题目大意:中文题 题目分析:一个星期前用单旋splay写的,一直TLE,一气之下用set乱搞写过去了,今天突然想起来这个题,意识到会不会是单旋的问题,改成双旋立马AC,而且比set ...

  2. BZOJ 1208 宠物饲养所 Splay

    要实现的操作是插入,删除,找到比指定值大的,小的值操作. Splay的删除操作可以是直接用二叉搜索树的删除方式,或者是先将要删除的节点Splay到根,然后找到左子树中最大的节点,将其Splay到根的左 ...

  3. NHOI 2004 宠物收养所 splay解法

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec   Memory Limit: 162 MB Submit: 3047   Solved: 1098 [ Submit ...

  4. BZOJ 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 7684  Solved: 3042 [Submit][S ...

  5. BZOJ 1208: [HNOI2004]宠物收养所 (Treap)

    BZOJ 1208: [HNOI2004]宠物收养所 题目概述: 有一家宠物收养所,提供两种服务:收养主人遗弃的宠物和让新主人领养宠物. 宠物收养所中总是会有两种情况发生:遗弃宠物过多和领养宠物人过多 ...

  6. 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 1.用Treap树写: 只需要三个操作,插入,删除,查找(同时找出其前继后继): View Code /************************** ...

  7. 「HNOI2004」 宠物收养所 - 平衡树Splay

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

  8. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  9. bzoj1208【HNOI2004】宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec   Memory Limit: 162 MB Submit: 5923   Solved: 2296 [ Submit ...

  10. BZOJ1208[HNOI2004]宠物收养场——treap

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

最新文章

  1. 漫画:什么是 HTTPS 协议?
  2. 北京高性能计算机应用中心,中国气象局高性能计算机系统资源使用报告-北京高性能计算机应用中心.PDF...
  3. 记一次EventBus内存泄露导致的项目问题
  4. Titanium Developer
  5. graylog2 架构--转载
  6. 【杂谈】有三AI季划成员的4大专属权益,你是否已经利用好了?
  7. 判断点是否处于多边形内的三种方法(转)
  8. 纪中A组模拟赛总结(2021.7.19)
  9. Android中SQLiteDatabase操作【附源码】
  10. pytorch Inception代码实现
  11. 当select查询为空
  12. android源码分析网上随笔记录
  13. windows计算机锁屏的快捷键是什么,win10电脑锁屏快捷键是什么
  14. python三国演义人物出场统计
  15. WCF服务系列——定义宿主(IIS服务宿主)
  16. 仓储管理之盘点——SAP盘点方法与流程
  17. cl_long.py
  18. ubuntu vscode use clang-format google style
  19. HTML/CSS+JavaScript+jQuery
  20. 前端JavaScript(1) --Javascript简介,第一个JavaScript代码,数据类型,运算符,数据类型转换,流程控制,百度换肤,显示隐藏...

热门文章

  1. c# 计算一年有多少周
  2. 今天终于把爬虫的Ajax请求搞懂了
  3. 【EdgeX(13)】 :EdgeX官方的摄像头demo-app,接入摄像头可以控制摄像头的上下左右方向,创建OpenVINO的目标检测跟踪服务,并成功接受到相关坐标数据,可以直接做边缘计算了。
  4. 开源 免费 java CMS - FreeCMS2.8 会员头像设置
  5. linux常用命令与问题排查命令记录
  6. AutoLeaders控制组——51单片机学习笔记(LED点阵屏、DS1302时钟芯片)
  7. 搭建gos_Gos ast Package pt 1的好东西
  8. U3D Shader基础
  9. ios13 微信提示音插件_iOS 13 替换微信提示音(教程),简单操作
  10. 建筑企业收并购系列二:股转与吸收合并