题目链接

思路

模板

只是有几个容易出错的地方

第45行容易忘记

第54行里面的cnt--和siz--容易忘记

第56行是根据id判断不是val

第60行siz--容易忘记

第64行是siz+1不是siz+cnt

第77行和82行等于的情况容易忽略

代码

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<ctime>
#define ls TR[cur].ch[0]
#define rs TR[cur].ch[1]
using namespace std;
typedef long long ll;
const int N = 100000 + 100,INF = 1e7 + 100;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
struct node {
int ch[2],val,id,cnt,siz;
}TR[N];
void up(int cur) {
TR[cur].siz = TR[ls].siz + TR[rs].siz + TR[cur].cnt;
}
void rotate(int &cur,int f) {
int son = TR[cur].ch[f];
TR[cur].ch[f] = TR[son].ch[f ^ 1];
TR[son].ch[f ^ 1] = cur;
up(cur);
cur = son;
up(cur);
}
int tot;
void insert(int &cur,int val) {
if(!cur) {
cur = ++tot;
TR[cur].val = val;
TR[cur].id = rand();
TR[cur].cnt = TR[cur].siz = 1;
return;
}
TR[cur].siz++;//!!
if(TR[cur].val == val) { TR[cur].cnt++;return;}
int d = val > TR[cur].val;
insert(TR[cur].ch[d],val);
if(TR[TR[cur].ch[d]].id < TR[cur].id) rotate(cur,d);
}
void del(int &cur,int val) {
if(!cur) return;
if(val == TR[cur].val) {
if(TR[cur].cnt > 1) {TR[cur].cnt--;TR[cur].siz--; return;}
if(!ls || !rs) {cur = ls + rs;return;}
int d = TR[rs].id < TR[ls].id;
rotate(cur,d);
del(cur,val);
}
else TR[cur].siz--,del(TR[cur].ch[val > TR[cur].val],val);
}
int Rank(int cur,int val) {
if(!cur) return 0;
if(val == TR[cur].val) return TR[ls].siz + 1;
if(val < TR[cur].val) return Rank(ls,val);
return Rank(rs,val) + TR[ls].siz + TR[cur].cnt;
}
int kth(int cur,int now) {
while(1) {
if(TR[ls].siz >= now) cur = ls;
else if(TR[ls].siz + TR[cur].cnt < now) now -=TR[ls].siz + TR[cur].cnt,cur = rs;
else return TR[cur].val;
}
}
int pred(int cur,int val) {
if(!cur) return -INF;
if(val <= TR[cur].val) return pred(ls,val);//!!!
return max(pred(rs,val),TR[cur].val);
}
int nex(int cur,int val) {
if(!cur) return INF;
if(val >= TR[cur].val) return nex(rs,val);//!!!
return min(nex(ls,val),TR[cur].val);
}
int main() {
srand(time(0));
int n = read();
int rt = 0;
while(n--) {
int opt = read(),x = read();
if(opt == 1) insert(rt,x);
if(opt == 2) del(rt,x);
if(opt == 3) printf("%d\n",Rank(rt,x));
if(opt == 4) printf("%d\n",kth(rt,x));
if(opt == 5) printf("%d\n",pred(rt,x));
if(opt == 6) printf("%d\n",nex(rt,x));
} return 0;
}

一言

记忆或会消失,但我的心会记着承诺。

[luogu3369][普通平衡树]的更多相关文章

  1. [luogu3369]普通平衡树(替罪羊树模板)

    解题关键:由于需要根据平衡进行重建,所以不能进行去重,否则无法保证平衡性. #include<cstdio> #include<cstring> #include<alg ...

  2. [luogu3369]普通平衡树(fhq-treap模板)

    解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  3. [luogu3369]普通平衡树(treap模板)

    解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  4. [luogu3369] 普通平衡树(splay模板)

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

  5. [模板]fhqTreap

    用途 平衡树(可实现区间翻转) 原理 和treap一样,中序遍历表示权值的顺序,并且每个点有一个随机的附加值,形成一个堆来保证复杂度 但是不旋转,所有操作通过split和merge实现 分为两种spl ...

  6. 【替罪羊树】bzoj3224&luogu3369&cogs1829 [Tyvj 1728]普通平衡树

    [替罪羊树]bzoj3224&luogu3369&cogs1829 [Tyvj 1728]普通平衡树 bzoj 洛谷 cogs 先长点芝士 替罪羊树也是一种很好写的平衡树qwq..替罪 ...

  7. [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)

    解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...

  8. luogu3369 【模板】普通平衡树(Treap/SBT) treap splay

    treap做法,参考hzwer的博客 #include <iostream> #include <cstdlib> #include <cstdio> using ...

  9. luogu3369 【模板】 普通平衡树 Splay

    题目大意 维护一个数据结构,满足以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多个相同的数,因输出最小的排名) 查询排名为x ...

随机推荐

  1. python爬虫scrapy之scrapy终端(Scrapy shell)

    Scrapy终端是一个交互终端,供您在未启动spider的情况下尝试及调试您的爬取代码. 其本意是用来测试提取数据的代码,不过您可以将其作为正常的Python终端,在上面测试任何的Python代码. ...

  2. git format-patch制作内核补丁

    git init git add ./ git commit 之后修改代码 修改代码后执行 git add ./ git commit 执行完成后执行git log查询commit 的id 执行git ...

  3. k8s容器的资源限制

    1.k8s支持内存和cpu的限制 requests:容器运行需求,最低保障limits:限制,硬限制(资源上限) CPU: 1颗逻辑CPU(1核CPU=4个逻辑CPU) 1物理核=1000个微核(mi ...

  4. QTP键盘操作笔记

    micCtrlDwn  Presses the Ctrl key. micCtrlUp  Releases the Ctrl key. micLCtrlDwn  Presses the left Ct ...

  5. Eclipse配置C++时的三个关键环境变量

    ECLIPSE下载很简单,然后装上MinGW,安装就完成了,关键是要配置三个环境变量 include——C:\MinGW\include lib——C:\MinGW\lib path——C:\MinG ...

  6. Add Languages to Your Xamarin Apps with Multilingual App Toolkit

    With Xamarin, you can extend your cross-platform apps with support for native speakers, reaching mar ...

  7. 了解C#中的HashSet与示例

    在C#中引入HashSet 在.NET框架中,有几个类可用于执行这些操作.一些课程如下: 列表 字典 哈希集 队列 集合 在C#编程中,像ArrayList,List这样的集合,只需添加其中的值,而不 ...

  8. hdu-1058(map)

    题意:输出第n个能拆分成由,2,3,5,7中的一个或者多个组成的数: 解题思路:a[i+1]=a[i]*2;a[i+2]=a[i]*3;a[i+3]=a[i]*5;a[i+4]=a[i]*7;然后把重 ...

  9. MongoDB数据模型设计

    MongoDB的数据模式是一种灵活模式,其集合并不限制文档结构.这种灵活性让对象和数据库文档之间的映射变得很容易,即使数据记录之间有很大的变化,每个文档也可以很好的映射到各条不同的记录.但在实际使用中 ...

  10. css背景色 透明字体不透明

    .demo{ padding: 25px; background-color: rgba(,,,0.5);/* IE9.标准浏览器.IE6和部分IE7内核的浏览器(如QQ浏览器)会读懂 */ }