P3369 【模板】普通平衡树
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入 x x 数 删除 x x 数(若有多个相同的数,因只删除一个) 查询 x x 数的排名(排名定义为比当前数小的数的个数 +1 + 1 ) 查询排名为 x x 的数 求 x x 的前驱(前驱定义为小于 x x,且最大的数) 求 x x 的后继(后继定义为大于 x x,且最小的数) 很好的模板题 借鉴了一位在役国集选手 #include<bits/stdc++.h> #define MAXN 300005 using namespace std; struct Splay{ int num[MAXN],ch[MAXN][2],sz[MAXN],f[MAXN],cnt[MAXN],rt,tot; int get(int x){ return (x==ch[f[x]][1]); } int up(int x){ sz[x] = cnt[x]; if(ch[x][0])sz[x] = sz[x]+sz[ch[x][0]]; if(ch[x][1])sz[x] = sz[x]+sz[ch[x][1]]; return 0; } int rote(int x){ int y = f[x],z = f[y],k = get(x),p = get(y); if(y==0)return 0; if(z)ch[z][p]