传送门1
传送门2
写在前面:我自己也会被写进机房大事记……
思路:很裸的splay练习,只涉及查前驱后继和删除操作,加一个标记记录现在是宠物多还是人多即可
注意:
1.查询和调用前驱后继时可能两者同时存在,记得判断一下
2.不知道为什么用bool型做树的标记时总是自己莫名改值,害我调了好久
代码:

#include<bits/stdc++.h>
#define mod 1000000
int n,x,tot,root,ans;
int hand,flag;
struct os
{int data,fa,left,right,sz;
}a[100010];
int in()
{int f=1,t=0;char ch=getchar();while (ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') t=t*10+ch-'0',ch=getchar();return f*t;
}
void made(int x)
{a[++tot].data=x;a[tot].sz=1;
}
void ct(int now)
{a[now].sz=a[now].left+a[now].right+1;
}
void rorate(int now,bool flag)
{int pa=a[now].fa;if (flag){a[pa].left=a[now].right;if (a[now].right)a[a[now].right].fa=pa;if (a[pa].fa){if (a[a[pa].fa].left==pa) a[a[pa].fa].left=now;else a[a[pa].fa].right=now;}a[now].fa=a[pa].fa;a[pa].fa=now;a[now].right=pa;}else{a[pa].right=a[now].left;if (a[now].left)a[a[now].left].fa=pa;if (a[pa].fa) { if (a[a[pa].fa].left==pa) a[a[pa].fa].left=now;else a[a[pa].fa].right=now;} a[now].fa=a[pa].fa;a[pa].fa=now;a[now].left=pa;}ct(pa);ct(now);
}
void splay(int x,int goal)
{int y;while (a[x].fa!=goal){y=a[x].fa;if (a[y].fa==goal){if (a[y].left==x) rorate(x,1);else rorate(x,0);}else if (y==a[a[y].fa].left){if (a[y].left==x) rorate(y,1);else rorate(x,0);rorate(x,1);}else{if (a[y].right==x) rorate(y,0);else rorate(x,1);rorate(x,0);}}if (!goal) root=x;
}
void insert(int x)
{made(x);a[tot].data=x;if (!root) {root=tot;return;}int now=root;while (now){if (x<a[now].data){if (!a[now].left) {a[now].left=tot;a[tot].fa=now;break;}else now=a[now].left;}else{if (!a[now].right) {a[now].right=tot;a[tot].fa=now;break;}else now=a[now].right;}}splay(tot,0);
}
int find(int x)
{int now=root;while (now){if (a[now].data==x) return now;else if (a[now].data>x) now=a[now].left;else now=a[now].right;}return 0;
}
int find_max(int now)
{if (!now) return 0;while (now){if (a[now].right) now=a[now].right;else return now;}
}
bool del(int now)
{splay(now,0);if (!a[now].left&&!a[now].right){root=0;return 1;}else if (!a[now].left){a[a[now].right].fa=0;root=a[now].right;}else if (!a[now].right){a[a[now].left].fa=0;root=a[now].left;}else{int child=find_max(a[now].left);splay(child,root);root=child;a[child].right=a[now].right;a[a[now].right].fa=child;a[child].fa=0;ct(child);}return 1;
}
int find_next_max(int x)
{int now=root,k=0;while (now){if (a[now].data>x)k=(a[k].data>a[now].data||!k)?now:k,now=a[now].left;else now=a[now].right;}if (k) splay(k,0);return k;
}
int find_next_min(int x)
{int now=root,k=0;while (now){if (a[now].data<x)k=(a[k].data<a[now].data||!k)?now:k,now=a[now].right;else now=a[now].left;}if (k) splay(k,0);return k;
}
main()
{n=in();for (int i=1;i<=n;i++){scanf("%d%d",&hand,&x);if (!root) flag=hand;if (flag==hand) insert(x);else{int p,p1=find_next_min(x),p2=find_next_max(x);if (p1&&(x-a[p1].data<=a[p2].data-x||!p2)) p=p1;else p=p2;ans=(ans+abs(a[p].data-x))%mod;del(p);}}printf("%d",ans);
}

【codevs1285】【BZOJ1208】宠物收养所,splay练习相关推荐

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

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

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

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

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

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

  4. bzoj1208【HNOI2004】宠物收养所

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

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

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

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

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 6182  Solved: 2396 [Submit][Status][Discuss] Descri ...

  7. B1208 [HNOI2004]宠物收养所 平衡树||set (滑稽)

    这个题是一道splay裸题,但是我不太会写,所以用set直接水过去!!!哈哈哈哈,美滋滋. set总结: set是一个集合,然后里面没用重复的元素.里面有一些函数: begin()     ,返回se ...

  8. 洛谷P2286 [HNOI2004]宠物收养场

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

  9. 洛谷 P2286 [HNOI2004]宠物收养场

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

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

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

最新文章

  1. #define 的换行问题
  2. php mysql 随机字符串函数是,mysql生成随机字符串函数分享
  3. 【Spring学习】IoC、DI、AOP入门学习
  4. Leetcode--820:单词的压缩编码(java)
  5. php中metadata,模型元数据(Models Metadata)
  6. Objective-C依然占C位,Swift和SwiftUI在iOS 15中的使用情况
  7. express基本原理
  8. windows 编写的硬件驱动_驱动更新有什么好处(一文解读驱动更新细则)
  9. python视频换脸下载_FaceSwap下载|Faceswap(AI视频换脸软件) v1.0官方版(附使用教程)...
  10. Sklearn聚类算法之meanshift
  11. win10怎样修改密码及忘记密码了怎么办
  12. 外文论文阅读生词积累
  13. 微信视频号视频如何下载保存?教你批量下载保存视频号视频到手机相册
  14. Android Canvas制作一个可以拖动改变任意形状的四边形效果,并填充颜色
  15. layui 重置checkbox全部选中和未选中
  16. 贝恩资本联手华为22亿美元收购3Com
  17. lnux php 连接access,在Linux下实现对Microsoft Access Database(.mdb
  18. 互联网行业入门必读书籍
  19. c++中将字符串转换为无符号整数函数:std::stoul and std::stoull
  20. IOS之高德地图(一)显示出地图并定位成功

热门文章

  1. 硬核评测:企业上云的极速存储挑战,华为云全新极速IO云硬盘性能评测
  2. 【带着canvas去流浪(11)】Three.js入门学习笔记
  3. 野生前端的数据结构基础练习(2)——队列
  4. 揭秘丨7分钟看懂华为云鲲鹏Redis背后的自研技术
  5. cpu计算速度排行榜_CPU速度的计算方法和单位
  6. java运行多个linux命令_在shell的一行中运行多个命令
  7. spring整合atomikos实现分布式事务的方法示例_分布式事务一:基于数据库原生分布式事务方案实现...
  8. java中“53”个关键字(含2个保留字)
  9. python取模1e9+7_G题取1E9+7wa,取1E9+9等一些其他的值ac
  10. python打开控制台运行_如何在IPython控制台中默认运行文件而不是终端?