题目链接

思路

模板

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

第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. bootstrap3兼容IE8

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. springboot 如何操作redis

    1.首先应该引入 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  3. 神经网络-SGD-2

    接上节: 3.梯度(gradient): def numerical_gradient(f,x): h=1e-5 grad=np.zeros_like(x) for index_x in range( ...

  4. java学习之—使用栈实现字符串数字四则运算

    /** * 使用栈存储后缀表达式 * Create by Administrator * 2018/6/13 0013 * 下午 2:25 **/ public class StackX { priv ...

  5. 网站滚动n个像素后,头部固定

    //固顶 $(window).scroll(function() { var top = $(window).scrollTop(); if(top>=1200){ $(".x_men ...

  6. Flask从入门到精通

    1. flask的下载和安装 在windows下安装flask flask依赖于两个库, 即werkzeug 和jinja2 , werkzeug负责服务器的部署, jinja2负责模板引擎,管理安装 ...

  7. Bootstrap之响应式导航栏

    代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...

  8. DEV GridControl/TreeList 中ShowingEditor使用

    ShowingEditor事件对我来说就是控制单元格的编辑属性,在特定场景中(TreeList中要求子节点某些列可编辑,父节点不可编辑)就需要使用此事件来实现,与此同时,上一篇也介绍了特定场景单元格样 ...

  9. PC平台的SIMD支持检测

    如果我们希望在用SIMD来提升程序处理的性能,首先需要做的就是检测程序所运行的平台是否支持相应的SIMD扩展.平台对SIMD扩展分为两部分的支持: CPU对SIMD扩展的支持.SIMD扩展是随着CPU ...

  10. ES系列十五、ES常用Java Client API

    一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...