Problem
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
- 插入x数
- 删除x数(若有多个相同的数,因只删除一个)
- 查询x数的排名(若有多个相同的数,因输出最小的排名)
- 查询排名为x的数
- 求x的前驱(前驱定义为小于x,且最大的数)
- 求x的后继(后继定义为大于x,且最小的数)
Solution
Treap模板题:
Treap为一种节点的优先级满足堆性质的二叉搜索树。
非旋转Treap:
代码较短,常数较Splay较小,比旋转Treap稍大。但因为不是旋转的,所以支持区间操作和可持久化。
最主要的操作为Merge,Split
建树方法参照笛卡尔树的建树方法。
推荐网站:http://memphis.is-programmer.com/posts/46317.html
旋转Treap:
常数十分的小,非常的好用,但不支持区间操作和可持久化。
主要操作为单旋:左旋和右旋
Notice
还不够熟练,很容易打错
Code
非旋转Treap
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define pairint pair<int, int>
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, root, a[N + 5];
struct node
{
int Size[N + 5], Val[N + 5], Level[N + 5], Son[2][N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + 1;
}
int Newnode(int v)
{
int u = ++point;
Val[u] = v, Level[u] = rand();
Son[0][u] = Son[1][u] = 0, Size[u] = 1;
return u;
}
int Merge(int X, int Y)
{
if (X * Y == 0) return X + Y;
if (Level[X] < Level[Y])
{
Son[1][X] = Merge(Son[1][X], Y);
up(X); return X;
}
else
{
Son[0][Y] = Merge(X, Son[0][Y]);
up(Y); return Y;
}
}
void Split(int u, int t, int &x, int &y)
{
if (!u)
{
x = y = 0;
return;
}
if (Val[u] <= t) x = u, Split(Son[1][u], t, Son[1][u], y);
else y = u, Split(Son[0][u], t, x, Son[0][u]);
up(u);
}
void Build(int l, int r)
{
int last, u, s[N + 5], top = 0;
rep(i, l, r)
{
int u = Newnode(a[i]);
last = 0;
while (top && Level[s[top]] < Level[u])
{
up(s[top]);
last = s[top];
s[top--] = 0;
}
if (top) Son[1][s[top]] = u;
Son[0][u] = last;
s[++top] = u;
}
while (top) up(s[top--]);
root = s[1];
}
int Find_rank(int v)
{
int x, y, t;
Split(root, v - 1, x, y);
t = Size[x];
root = Merge(x, y);
return t + 1;
}
int Find_num(int u, int v)
{
if (!u) return 0;
if (v <= Size[Son[0][u]]) return Find_num(Son[0][u], v);
else if (v <= Size[Son[0][u]] + 1) return u;
else return Find_num(Son[1][u], v - Size[Son[0][u]] - 1);
}
void Insert(int v)
{
int t = Newnode(v), x, y;
Split(root, v, x, y);
root = Merge(Merge(x, t), y);
}
void Delete(int v)
{
int x, y, z;
Split(root, v, x, z), Split(x, v - 1, x, y);
root = Merge(Merge(x, Merge(Son[0][y], Son[1][y])), z);
}
}Treap;
int sqz()
{
int n = read(), x = 0, y = 0;
while (n--)
{
int op = read(), t = read();
switch (op)
{
case 1: Treap.Insert(t); break;
case 2: Treap.Delete(t); break;
case 3: printf("%d\n", Treap.Find_rank(t)); break;
case 4: printf("%d\n", Treap.Val[Treap.Find_num(root, t)]); break;
case 5: Treap.Split(root, t - 1, x, y);
printf("%d\n", Treap.Val[Treap.Find_num(x, Treap.Size[x])]);
root = Treap.Merge(x, y); break;
case 6: Treap.Split(root, t, x, y);
printf("%d\n", Treap.Val[Treap.Find_num(y, 1)]);
root = Treap.Merge(x, y); break;
}
}
return 0;
}
旋转Treap
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, pre, suf, root;
struct node
{
int Val[N + 5], Level[N + 5], Size[N + 5], Son[2][N + 5], Num[N + 5];
inline void up(int u)
{
Size[u] = Size[Son[0][u]] + Size[Son[1][u]] + Num[u];
}
inline void Newnode(int &u, int v)
{
u = ++point;
Level[u] = rand(), Val[u] = v;
Size[u] = Num[u] = 1, Son[0][u] = Son[1][u] = 0;
}
inline void Lturn(int &x)
{
int y = Son[1][x]; Son[1][x] = Son[0][y], Son[0][y] = x;
Size[y] = Size[x]; up(x); x = y;
}
inline void Rturn(int &x)
{
int y = Son[0][x]; Son[0][x] = Son[1][y], Son[1][y] = x;
Size[y] = Size[x]; up(x); x = y;
}
void Insert(int &u, int t)
{
if (u == 0)
{
Newnode(u, t);
return;
}
Size[u]++;
if (t == Val[u]) Num[u]++;
else if (t < Val[u])
{
Insert(Son[0][u], t);
if (Level[Son[0][u]] < Level[u]) Rturn(u);
}
else if (t > Val[u])
{
Insert(Son[1][u], t);
if (Level[Son[1][u]] < Level[u]) Lturn(u);
}
}
void Delete(int &u, int t)
{
if (!u) return;
if (Val[u] == t)
{
if (Num[u] > 1)
{
Num[u]--, Size[u]--;
return;
}
if (Son[0][u] * Son[1][u] == 0) u = Son[0][u] + Son[1][u];
else if (Level[Son[0][u]] < Level[Son[1][u]]) Rturn(u), Delete(u, t);
else Lturn(u), Delete(u, t);
}
else if (t < Val[u]) Size[u]--, Delete(Son[0][u], t);
else Size[u]--, Delete(Son[1][u], t);
}
int Find_rank(int u, int t)
{
if (!u) return 0;
if (Val[u] == t) return Size[Son[0][u]] + 1;
else if (t < Val[u]) return Find_rank(Son[0][u], t);
else return Size[Son[0][u]] + Num[u] + Find_rank(Son[1][u], t);
}
int Find_num(int u, int t)
{
if (!u) return 0;
if (t <= Size[Son[0][u]]) return Find_num(Son[0][u], t);
else if (t <= Size[Son[0][u]] + Num[u]) return Val[u];
else return Find_num(Son[1][u], t - Size[Son[0][u]] - Num[u]);
}
void Find_pre(int u, int t)
{
if (!u) return;
if (t > Val[u])
{
pre = u;
Find_pre(Son[1][u], t);
}
else Find_pre(Son[0][u], t);
}
void Find_suf(int u, int t)
{
if (!u) return;
if (t < Val[u])
{
suf = u;
Find_suf(Son[0][u], t);
}
else Find_suf(Son[1][u], t);
}
}Treap;
int sqz()
{
int n = read();
while (n--)
{
int op = read(), x = read();
switch (op)
{
case 1: Treap.Insert(root, x); break;
case 2: Treap.Delete(root, x); break;
case 3: printf("%d\n", Treap.Find_rank(root, x)); break;
case 4: printf("%d\n", Treap.Find_num(root, x)); break;
case 5: pre = 0, Treap.Find_pre(root, x); printf("%d\n", Treap.Val[pre]); break;
case 6: suf = 0, Treap.Find_suf(root, x); printf("%d\n", Treap.Val[suf]); break;
}
}
}