题目

详情见链接

代码

 #include<cstdio>
#include<iostream>
#define outd(x) printf("%d\n",x)
#define outld(x) printf("%lld\n", x)
#define ls(x) arr[x].child[0]
#define rs(x) arr[x].child[1]
inline int read_int()
{
char c;
int ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
using namespace std; const int maxn = + ; struct node
{
int child[], size, value, key;
}arr[maxn];
int tot; //当前节点个数 inline void push_up(int index)
{
arr[index].size = arr[ls(index)].size + arr[rs(index)].size + ;
} void split(int root, int& x, int& y, int value) //x中的小于或等于value,y中的大于value
{
if (!root) { x = y = ; return; }
if (arr[root].value <= value) { x = root; split(rs(root), rs(x), y, value); }
else { y = root; split(ls(root), x, ls(y), value); }
push_up(root);
} void merge(int&root, int x, int y)
{
if (!x || !y) { root = x + y; return; } //其中一个为0,root等于另一个
if (arr[x].key < arr[y].key) { root = x; merge(rs(root), rs(x), y); }
else { root = y; merge(ls(root), x,ls(y)); }
push_up(root);
} inline void insert(int& root, int value)
{
int x = , y = , z = ++tot;
arr[z].value = value, arr[z].size = , arr[z].key = rand();
split(root, x, y, value);
merge(x, x, z);
merge(root, x, y);
} inline void erase(int& root, int value)
{
int x = , y = , z = ;
split(root, x, y, value);
split(x, x, z, value - ); //z中全是value
merge(z, ls(z), rs(z));
merge(x, x, z);
merge(root, x, y);
} inline int Kth_number(int root, int k)
{
while (arr[ls(root)].size + != k)
{
if (arr[ls(root)].size >= k) root = ls(root);
else { k -= (arr[ls(root)].size + ); root = rs(root); }
}
return arr[root].value;
} inline int Get_rank(int& root, int value)
{
int x = , y = ;
split(root, x, y, value - );
int res = arr[x].size + ;
merge(root, x, y);
return res;
} inline int Pre(int& root, int value)
{
int x = , y = ;
split(root, x, y, value - );
int res = Kth_number(x, arr[x].size);
merge(root, x, y); //merge回去
return res;
} inline int Suf(int& root, int value)
{
int x = , y = ;
split(root, x, y,value);
int res = Kth_number(y, );
merge(root, x, y);
return res;
} int n, op, root; int main()
{
srand();
n = read_int();
while (n--)
{
op = read_int();
if (op == ) insert(root, read_int());
if (op == ) erase(root, read_int());
if (op == ) outd(Get_rank(root, read_int()));
if (op == ) outd(Kth_number(root, read_int()));
if (op == ) outd(Pre(root, read_int()));
if (op == ) outd(Suf(root, read_int()));
} return ;
}

参考视频链接:https://www.bilibili.com/video/av46204315/?p=2

最新文章

  1. HDU 1528 贪心模拟/二分图
  2. python之路-Day11
  3. linuxMint下面的截图工具
  4. git 教程(4)--版本回退
  5. flex loaderInfo为null在creationComplete事件中
  6. Hbase0.98.4/Hadoop2.4.1整合小结【原创】
  7. UIImageView 动画 / UIImage 方向
  8. axure变量的使用
  9. SparkSQL On Yarn with Hive,操作和访问Hive表
  10. angular2 学习笔记 ( rxjs 流 )
  11. SpriteKit所有的类
  12. OpenCV-Python教程(10、直方图均衡化)
  13. C/C++宏定义中#与##区别 .
  14. solr学习-基础环境搭建(一)
  15. 【C#】C#获取文件夹下的所有文件
  16. FastAdmin 基本知识流程一栏
  17. &lt;Numerical Analysis&gt;(by Timothy Sauer) Notes
  18. python中的异常处理tryexcept
  19. Python自动化开发 - 模块与包
  20. 启动Tomcat 卡在 Initializing Spring FrameworkServlet &#39;SpringMVC&#39;

热门文章

  1. unity update优化
  2. ue4 weapon
  3. spring-eureka 源码解读----为什么一个服务最多两分钟被其他服务感知
  4. 2017-9-2 NOIP模拟赛
  5. 笔记-JavaWeb学习之旅17
  6. NFS服务及DHCPD服务
  7. 洛谷P4568 飞行路线
  8. MySQL server has gone away和Maximum execution time of 120 seconds exceeded
  9. 在双系统(Win7和Ubuntu Kylin)中卸载Ubuntu
  10. DRF教程9-权限