题目链接:

https://www.lydsy.com/JudgeOnline/problem.php?id=3224

思路:

splay树模板题:

推荐博客:https://blog.csdn.net/clove_unique/article/details/50630280

b站上splay树的讲解视频也可以看下,讲的很好,推荐看完视频了解了splay的原理再写

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1e5+;
const int inf = <<;
struct node{
int fa,s[],x,size,cnt;
}tr[M];
int top,root,n;
void pushup(int k){
tr[k].size = tr[tr[k].s[]].size+tr[tr[k].s[]].size+tr[k].cnt;
} void rotate(int k){
int x = tr[k].fa,y = tr[x].fa,b=tr[x].s[]==k;
tr[y].s[tr[y].s[]==x]=k;tr[k].fa=y;
tr[x].s[b]=tr[k].s[b^]; tr[tr[k].s[b^]].fa=x;
tr[k].s[b^]=x; tr[x].fa = k;
pushup(x); pushup(k);
} void splay(int x,int go){ //伸展操作
int y = tr[x].fa,z = tr[y].fa;
for(;tr[x].fa^go;rotate(x)){
y = tr[x].fa,z = tr[y].fa;
if(z^go) (tr[y].s[] == x)^(tr[z].s[]==y)?rotate(x):rotate(y);
}
if(!go) root = x;
} void insert(int x){ //插入操作
int u = root,fa = ;
for(;u&&tr[u].x^x;u=tr[u].s[x>tr[u].x]) fa = u;
if(u) tr[u].cnt++; //若已经有这个数了,到cnt++即可
else{ //新建一个节点
u = ++top;
if(fa) tr[fa].s[x>tr[fa].x] = u;
tr[top].fa = fa;tr[top].x = x;tr[top].cnt = tr[top].size = ;
}
splay(u,); //splay 到根
}
//这个函数还有一个功能是把一个跟x不相隔仍荷属的数转移到根
int rank(int x){ //查找x的排名
int u = root;
if(!u) return ;
for(;tr[u].s[x>tr[u].x]&&x^tr[u].x;u=tr[u].s[x>tr[u].x]);
splay(u,); //splay 到根
return tr[tr[u].s[]].size;
}
//求x的前驱后继
int next(int x,int f){ //f=0表示前驱,f=1表示后继,返回的是编号
rank(x); //将与x不相隔任何数的数转移到根
int u = root;
if(tr[u].x>x&&f||tr[u].x<x&&!f) return u; //若已经是答案,则返回
u = tr[u].s[f];
while(tr[u].s[f^]) u = tr[u].s[f^]; //查找前驱时在左子树中查找最大值,后继反之
return u;
} void erase(int x){
int up = next(x,),lo = next(x,),u; //得到前驱后继
splay(lo,); splay(up,lo); //移成一个好局面
u = tr[up].s[];
if(tr[u].cnt > ){
tr[u].cnt--;
splay(u,);
}
else {
tr[up].s[] = ;
pushup(up); pushup(lo);
}
} int find(int x){
x++;
int u = root,son;
if(tr[u].size < x) return ; //没有这个数
while(){
son = tr[u].s[];
if(tr[son].size >= x) u = son;
else if(x>tr[son].size+tr[u].cnt)
x -= tr[son].size + tr[u].cnt, u = tr[u].s[];
else break;
}
splay(u,);
return tr[u].x;
} int main(){
scanf("%d",&n);
int op,x;
insert(-inf); insert(inf);
for(int i = ;i <= n;i ++){
scanf("%d%d",&op,&x);
switch(op){
case : insert(x);break;
case : erase(x); break;
case : printf("%d\n",rank(x));break;
case : printf("%d\n",find(x)); break;
case : printf("%d\n",tr[next(x,)].x); break;
case : printf("%d\n",tr[next(x,)].x); break;
}
}
return ;
}

bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)的更多相关文章

  1. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

  2. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  3. BZOJ 3224: Tyvj 1728 普通平衡树 treap

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  4. BZOJ 3224: Tyvj 1728 普通平衡树 vector

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  5. BZOJ 3224: Tyvj 1728 普通平衡树(BST)

    treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...

  6. BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 22483  Solved: 10130[Submit][S ...

  7. BZOJ 3224 Tyvj 1728 普通平衡树模板

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...

  8. bzoj 3224/Tyvj 1728 普通平衡树(splay)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  9. fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树

    题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...

随机推荐

  1. linux调度器源码分析 - 新进程加入(三)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 之前的文章已经介绍了调度器已经初始化完成,现在只需要加入一个周期定时器tick驱动它进行周期调度即可,而加 ...

  2. eclipse 报错Version 1.6.0_45 of the JVM is not suitable for this product. Version:1.7 or greater is required

    最近离职来了一家新公司,之前的公司的开发IDE用的是IntelliJIDEA和SpringSourceToolSuit,自己在家里用的也是MyEclipse,所以使用eclipse的经验还是不足.结果 ...

  3. 求组合数、求逆元、求阶乘 O(n)

    在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...

  4. 千兆以太网TCP协议的FPGA实现

    转自https://blog.csdn.net/zhipao6108/article/details/82386355 千兆以太网TCP协议的FPGA实现 Lzx 2017/4/20 写在前面,这应该 ...

  5. Ionic 中控件点击延迟的处理

    原文发表于我的技术博客 本文分享了在 Ionic 中如何处理控件点击延迟的问题. 原文发表于我的技术博客 1. 问题描述 在 Ionic 中,当在 iOS 环境下运行元素的点击事件时,你会发现点击响应 ...

  6. E. Binary Numbers AND Sum

    链接 [http://codeforces.com/contest/1066/problem/E] 题意 给你长度分别为n,m的二进制串,当b>0时,对a,b,&运算,然后b右移一位,把 ...

  7. linux内核分析字符集实践报告

  8. Minimum Integer CodeForces - 1101A (思维+公式)

    You are given qq queries in the following form: Given three integers lili, riri and didi, find minim ...

  9. python2 与 python3 实现共存

    已有配置  Anaconda2+python2.7 方案一:直接安装官网原生python3.6 1.修改根目录下python.exe ->python3.exe    pythonw.exe - ...

  10. 【转】STM32 独立看门狗简介

    STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路就是为了避免这种 ...