BZOJ 3224 SBT 普通平衡树
复习了一下SBT的模板,但是BZOJ不知道为什么注册不了,所以就没交,测了样例能过!
#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
#define lc ch[0]
#define rc ch[1]
const int MAXN=500000;
const int INF=0x3f3f3f3f;
struct node
{
node* ch[2];
int sz,v;
node(){}
}SBT[MAXN+10],*NILL=new node,*root=NILL,*tot=SBT;
int getint()
{
int ret=0;bool f=0;char ch;
while((ch=getchar())<'0'||ch>'9')f=!f;
while(ch>='0'&&ch<='9')ret=ret*10+(ch-'0');
return f?-ret:ret;
}
void init()
{
NILL->lc=NILL;
NILL->rc=NILL;
NILL->sz=0;
}
//更新子树大小
inline void update(node* r)
{
r->sz=r->lc->sz+rc->sz+1;
}
node* newnode()
{
tot->lc=tot->rc=NILL;
tot->sz=1;
return tot++;
}
void rotate(node* r,bool f)
{
node *t=r->ch[f];
r->ch[f]=t->ch[!f];
t->ch[!f]=r;
t->sz=r->sz;
update(r);
r=t;
}
void mt(node* &r,bool f)//利用左右对称,带上参数f同时减去不必要的检查
{
if(r==NILL)return;//NILL为空指针
if(r->ch[f]->ch[f]->sz>r->ch[!f]->sz)//左左>右
{
rotate(r,f);
}
else if(r->ch[f]->ch[!f]->sz>r->ch[!f]->sz)//左右>右,先旋左儿子,再旋根
{
rotate(r->ch[f],!f);rotate(r,f);
}
else return;
mt(r->ch[f],f);
mt(r,f);
}
void insert(node* &r,int v)
{
if(r==NILL)
{
r=newnode();
r->v=v;
return;
}
r->sz++;
bool k=v>r->v;
insert(r->ch[k],v);
mt(r,k);
}
int del(node* &r,int x)
{
int ret;
r->sz--;
if(r->v==x||(r->lc==NILL&&x<r->v)||(r->rc==NILL&&x>r->v))//删除根节点的情况
{
ret=r->v;
if(r->lc==NILL||r->rc==NILL)
r=r->lc==NILL?r->rc:r->lc;
else
r->v=del(r->lc,x);
}
else ret=del(r->ch[x>=r->v],x);
return ret;
}
int sel(int val)//val的排位
{
int ret=1;
node* p=root;
while(p!=NILL)
{
if(val<=p->v)
{
p=p->lc;
}
else
{
ret+=p->lc->sz+1;
p=p->rc;
}
}
return ret;
}
int rk(int x)//排第x的值
{
node* p=root;
while(p!=NILL)
{
if(x==p->lc->sz+1)
return p->v;
if(x<=p->lc->sz)
p=p->lc;
else
{
x-=p->lc->sz+1;
p=p->rc;
}
}
return INF;
}
//查询前驱(0)后继(1)
int query(int v,bool f)
{
node* p=root;
int ret=f?INF:-INF;
while(p!=NILL)
{
if(p->v!=v&&(f==(p->v>v)&&f==(ret>p->v)))
{
ret=p->v;
}
if(v==p->v)
p=p->ch[f];
else p=p->ch[v>p->v];
}
return ret;
}
int main()
{
init();
int kase=getint();
while(kase--)
{
int opt=getint(),x=getint();
switch(opt)
{
case 1:insert(root,x);break;
case 2:del(root,x);break;
case 3:printf("%d\n",sel(x));break;
case 4:printf("%d\n",rk(x));break;
case 5:printf("%d\n",query(x,0));break;
case 6:printf("%d/n",query(x,1));break;
}
}
return 0;
}
BZOJ 3224 SBT 普通平衡树的更多相关文章
- 【BZOJ 3224】普通平衡树 模板题
删除节点时把节点splay到根: 然后把根左子树的最右边节点splay到根的左孩子上: 然后删除就可以了: 我的教训是删根的时候根的右孩子的父亲指针一定要记得指向根的左孩子!!! my AC code ...
- 【BZOJ 3224】 普通平衡树
[题目链接] 点击打开链接 [算法] 本题是Splay模板题,值得一做! [代码] #include<bits/stdc++.h> using namespace std; #define ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- 洛谷 P3369 BZOJ 3224 【模板】普通平衡树(Treap/SBT)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询 ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9629 Solved: 4091[Submit][Sta ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ 3224: Tyvj 1728 普通平衡树 vector
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
随机推荐
- 异步SOCKET分包和组包的一种通用算法
unit uPackage;// 应用协议// cxg 2016-9-23// 包=包头+包体 interface uses SysUtils, Classes, PeachCtrl.Net.Iocp ...
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
Visual Studio 2017中使用正则修改部分内容 最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...
- TF-IDF与余弦类似性的应用(一):自己主动提取关键词
作者: 阮一峰 日期: 2013年3月15日 原文链接:http://www.ruanyifeng.com/blog/2013/03/tf-idf.html 这个标题看上去好像非常复杂,事实上我要谈的 ...
- ora-12541无监听的一种场景
项目上突然出现无法连接Oracle数据库的情况,提示无监听程序. 现象: 查看 listener.ora配置无问题,用Net Configuration Assistant重建监听,NCA也处于假死状 ...
- centos6.5 yum安装MySQL5.6
创建MySQL用户 #useradd mysql #passwd mysql #chmod u+w /etc/sudoers #vi /etc/sudoers mysql ALL=(ALL) ALL ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- RDD的基本命令
1 创建RDD intRDD=sc.parallelize([3,1,2,5,6]) intRDD.collect()[4, 2, 3, 6, 7] 2 单RDD转换 (1) MAP def addo ...
- mac系统下如何删除银行安全插件
要分类解决了,一般safair插件都是pkg包安装的 如果:1.制作者够良心,PKG安装包中植入了删除卸载功能,那就好办了,打开当时安装的pkg包,执行删除选项 2.没良心的,装了不好删的,我目前个人 ...
- 如何制作.a静态库?合成多架构静态库?
08_01静态库 08_02制作静态库 .a 1.新建项目com+shift+n:选择Framework&Library. 2.下一步. 项目名不能为中文. 3.编写代码之后.用真机运行.会自 ...
- bzoj 5017 炸弹
题目大意: 直线上有n个炸弹有坐标x和半径r 当一个炸弹被引爆时 若有炸弹的坐标在该炸弹坐标+-r范围内则另一个炸弹也被引爆 求先引爆每一个炸弹最终会引爆多少炸弹 思路: 可以想到n平方连边然后tar ...