FHQ-Treap 模板
Aaron_Romeo · · 算法·理论
#include <iostream>
#include <algorithm>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <vector>
#include <bitset>
#include <random>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int uint;
bool st;
namespace IO
{
#ifdef LOCAL
const int N = 1;
#else
const int N = 1 << 23;
#endif
template<typename T> struct flt {T num; int pcs; flt() {num = 0, pcs = 6;} flt(const T &_num, const int &_pcs) {num = _num, pcs = _pcs;}};
struct I {FILE *in; char ibuf[N], *p1, *p2; I() {*this = stdin;} I(FILE *f) {*this = f;} I & operator = (FILE *f) {return p1 = p2 = ibuf, in = f, *this;} bool flush() {return p1 != p2 || (p2 = (p1 = ibuf) + fread(ibuf, 1, N, in), p1 != p2);} char gc() {return !flush() ? EOF : *p1 ++ ;} int getline(std :: string &s) {s.clear(); char c; while (c = gc(), c != '\n' && c != EOF) if (c != '\r') s += c; return s.length();} int getline(char *s) {char *t = s; char c; while (c = gc(), c != '\n' && c != EOF) if (c != '\r') *t ++ = c; *t = 0; return t - s;} int getdigit() {char c; while (!isdigit(c = gc()) && c != EOF); return c == EOF ? 0 : c - '0';} template<typename T> T readnum() {bool f = 0; T x = 0; char c; while (!isdigit(c = gc()) && c != EOF) f = c == '-'; while (isdigit(c)) x = x * 10 + c - '0', c = gc(); if (c == '.') for (T v = 0.1; isdigit(c = gc()); v *= 0.1) x += v * (c - '0'); return f ? -x : x;} template <typename T, typename = std :: enable_if_t<std :: is_arithmetic<T> :: value || std :: is_same<T, bool> :: value>> operator T() {return readnum<T>();} operator char() {char c = gc(); return c == EOF ? 0 : c;} operator std :: string() {std :: string s; char c; while (c = gc(), c == ' ' || c == '\t' || c == '\r' || c == '\n'); while (c != ' ' && c != '\t' && c != '\n' && c != EOF) {if (c != '\r') s += c; c = gc();} return s;} I & operator () () {return *this;} template<typename T> int operator () (T &x) {return x = operator T(), 0;} int operator () (char *s) {char *t = s, c; while (c = gc(), c == ' ' || c == '\t' || c == '\r' || c == '\n'); while (c != ' ' && c != '\t' && c != '\n' && c != EOF) {if (c != '\r') *t ++ = c; c = gc();} *t = 0; return t - s;} template<typename T, typename ...Args> typename std :: enable_if<std :: is_pointer<typename std :: remove_reference<T> :: type> :: value || std::is_array<T> :: value, int> :: type operator () (T x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);} template<typename T, typename ...Args> typename std :: enable_if<!std :: is_pointer<typename std :: remove_reference<T> :: type> :: value && !std::is_array<T> :: value, int> :: type operator () (T &x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);}};
struct O {FILE *out; char obuf[N], *p3; O() {p3 = obuf, out = stdout;} O(FILE *f) {p3 = obuf, out = f;} ~O() {flush();} O & operator = (FILE *f) {return flush(), out = f, *this;} void flush() {fwrite(obuf, p3 - obuf, 1, out), p3 = obuf;} int close() {return flush(), fclose(out);} void pc(char x) {*p3 ++ = x; if (p3 - obuf == N) flush();} template<typename ...Args> int printf(const char *format, Args ...args) {char s[1 << 23], *t = s; sprintf(s, format, args...); while (*t) pc(*t ++ ); return t - s;} template<typename T> void writeint(const T &x) {T y = x; if (!y) return pc('0'); if (y < 0) pc('-'), y = -y; char c[1 << 6]; int len = 0; while (y) c[ ++ len] = y % 10 + '0', y /= 10; while (len) pc(c[len -- ]);} template<typename T> void operator () (const T &x) {writeint<T>(x);} void operator () (const char &x) {pc(x);} void operator () (const std :: string &s) {for (char i : s) pc(i);} void operator () (char *s) {while (*s) pc(*s ++ );} void operator () (const char *s) {while (*s) pc(*s ++ );} template<typename T> void writefloat(const flt<T> &x) {T y = x.num; int r = x.pcs; if (y < 0) pc('-'), y = -y; T f = 5; for (int i = 1; i <= r + 1; i ++ ) f *= 0.1; y += f; char c[1 << 8]; int len = 0; if (y < 1) pc('0'); for (T t = std :: floor(y); t >= 1; t = std :: floor(t * 0.1)) c[ ++ len] = (int)(t - std :: floor(t * 0.1) * 10) + '0'; while (len) pc(c[len -- ]); if (!r) return ; pc('.'); for (T t = (y - std :: floor(y)) * 10; r; t = (t - std :: floor(t)) * 10, r -- ) pc((int)t + '0');} template<typename T> void operator () (const flt<T> &x) {writefloat<T>(x);} void operator () (const float &x) {writefloat(flt<float>(x, 6));} void operator () (const double &x) {writefloat(flt<double>(x, 6));} void operator () (const long double &x) {writefloat(flt<long double>(x, 6));} template<typename T, typename ...Args> void operator () (const T &x, const Args &...args) {operator () (x), operator () (args...);}};
} IO :: I read; IO :: O write, debug = stderr; using IO :: flt;
inline void initialize()
{
}
namespace solve
{
const int N = 1e5;
std :: mt19937_64 rand(time(0));
int root;
int son[N + 5][2], size[N + 5], val[N + 5], tot;
ull key[N + 5];
inline int make_new(int x)
{
tot ++ ;
size[tot] = 1, key[tot] = rand(), val[tot] = x;
return tot;
}
inline void push_up(int u) {size[u] = size[son[u][0]] + size[son[u][1]] + 1;}
void split(int p, int v, int &x, int &y)
{
if (!p) return x = y = 0, void();
if (val[p] <= v)
{
split(son[p][1], v, x, y);
son[p][1] = x;
push_up(x = p);
}
else
{
split(son[p][0], v, x, y);
son[p][0] = y;
push_up(y = p);
}
}
int merge(int u, int v)
{
if (!u || !v) return u | v;
if (key[u] < key[v])
{
son[u][1] = merge(son[u][1], v);
return push_up(u), u;
}
else
{
son[v][0] = merge(u, son[v][0]);
return push_up(v), v;
}
}
inline void insert(int x)
{
int A, B;
split(root, x, A, B);
int C = make_new(x);
A = merge(A, C);
root = merge(A, B);
}
inline void del(int x)
{
int A, B, C;
split(root, x - 1, A, B);
split(B, x, B, C);
B = merge(son[B][0], son[B][1]);
B = merge(B, C);
A = merge(A, B);
root = A;
}
inline int get_rank(int x)
{
int A, B;
split(root, x - 1, A, B);
int res = size[A];
root = merge(A, B);
return res;
}
inline int nth_element(int k)
{
int u = root;
while (1)
{
if (size[son[u][0]] >= k) u = son[u][0];
else
{
if (size[son[u][0]] + 1 == k) return val[u];
k -= size[son[u][0]] + 1;
u = son[u][1];
}
}
}
inline int get_la(int x)
{
int A, B;
split(root, x - 1, A, B);
int u = A;
while (son[u][1]) u = son[u][1];
int res = val[u];
root = merge(A, B);
return res;
}
inline int get_ne(int x)
{
int A, B;
split(root, x, A, B);
int u = B;
while (son[u][0]) u = son[u][0];
int res = val[u];
root = merge(A, B);
return res;
}
void print(int u)
{
if (!u) return;
print(son[u][0]);
write(val[u], " ");
print(son[u][1]);
}
int main()
{
int n = read();
while (n -- )
{
int op = read();
if (op == 1)
{
int x = read();
insert(x);
}
if (op == 2)
{
int x = read();
del(x);
}
if (op == 3)
{
int x = read();
write(get_rank(x) + 1, "\n");
}
if (op == 4)
{
int k = read();
write(nth_element(k), "\n");
}
if (op == 5)
{
int x = read();
write(get_la(x), "\n");
}
if (op == 6)
{
int x = read();
write(get_ne(x), "\n");
}
}
return 0;
}
}
bool ed;
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
// freopen("f:/study/Informatics/code/in.txt", "r", stdin);
// freopen("f:/study/Informatics/code/out.txt", "w", stdout);
#elif defined ONLINE_JUDGE
// freopen("name.in", "r", stdin);
// freopen("name.out", "w", stdout);
#else
// freopen("name.in", "r", stdin);
// freopen("name.out", "w", stdout);
#endif
#ifdef LOCAL
debug("Start\n\n");
#endif
initialize();
int T = 1;
while (T -- ) solve :: main();
#ifdef LOCAL
debug("\n\n", flt<double>(std :: clock() / 1000.0, 2), "s ", flt<double>(std :: abs(&ed - &st) / 1024.0 / 1024.0, 1), "MB\n");
#endif
return 0;
}