【模板】树套树
树套树(现只有部分模板)
e.g P2617 Dynamic Rankings
///线段树套平衡树,实现区间第k小查询+单点修改
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define MAXN 200010
using namespace std;
const int INF = 0x3f3f3f3f;
inline int read() {
register int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct node {
char type[2];
int x, y, k;
} Commands[MAXN];
struct node1 {
int l, r;
} Segment_Tree[MAXN << 3];
struct node2 {
int child[2], val;
int dat, siz;
int cmp1(int x) const {
if (x == val) return -1;
return x < val ? 0 : 1;
}
//val:位序; dat:优先级;siz:以该节点为根的节点个数
} Treap[MAXN * 20];
int cnt_treap;
///平衡树模板:
inline int New(int val) {
int p = ++cnt_treap;
Treap[p].val = val;
Treap[p].child[0] = Treap[p].child[1] = 0;
Treap[p].dat = rand();
Treap[p].siz = 1;
return p;
}
inline void Update_treap(int p) {
Treap[p].siz = Treap[Treap[p].child[0]].siz + Treap[Treap[p].child[1]].siz + 1;
}
inline void Zig(int &p) {
int q = Treap[p].child[0];
Treap[p].child[0] = Treap[q].child[1];
Treap[q].child[1] = p;
p = q;
Update_treap(Treap[p].child[1]);
Update_treap(p);
}
inline void Zag(int &p) {
int q = Treap[p].child[1];
Treap[p].child[1] = Treap[q].child[0];
Treap[q].child[0] = p;
p = q;
Update_treap(Treap[p].child[0]);
Update_treap(p);
}
inline void Insert(int &p, int val) {
if (!p) {
p = New(val);
return;
}
if (val < Treap[p].val) {
Insert(Treap[p].child[0], val);
if (Treap[p].dat < Treap[Treap[p].child[0]].dat) Zig(p);
} else {
Insert(Treap[p].child[1], val);
if (Treap[p].dat < Treap[Treap[p].child[1]].dat) Zag(p);
}
Update_treap(p);
}
inline void Remove(int &p, int val) {
if (!p) return;
if (val == Treap[p].val) {
if (Treap[p].child[0] || Treap[p].child[1]) {
if (Treap[Treap[p].child[0]].dat > Treap[Treap[p].child[1]].dat) {
Zig(p);
Remove(Treap[p].child[1], val);
} else {
Zag(p);
Remove(Treap[p].child[0], val);
}
Update_treap(p);
} else
p = 0;
return;
}
if (val < Treap[p].val)
Remove(Treap[p].child[0], val);
else
Remove(Treap[p].child[1], val);
Update_treap(p);
}
inline int Find(int p) {
int x = Treap[p].val;
if (x != -INF && x != INF) return x;
if (Treap[p].child[0]) {
x = Find(Treap[p].child[0]);
if (x) return x;
}
if (Treap[p].child[1]) {
x = Find(Treap[p].child[1]);
if (x) return x;
}
return 0;
}
inline int Calc(int p, int val) {
if (p == 0) return 0;
int cnt = Treap[Treap[p].child[0]].siz;
if (val == Treap[p].val) return cnt + 1;
else if (val < Treap[p].val) return Calc(Treap[p].child[0], val);
else return cnt + 1 + Calc(Treap[p].child[1], val);
}
int root[MAXN << 3];
int n, m, a[MAXN], b[MAXN << 1], eid;
///线段树模板:
inline void build(int p, int l, int r) {
Segment_Tree[p].l = l;
Segment_Tree[p].r = r;
root[p] = New(-INF);
Treap[root[p]].child[1] = New(INF);
Update_treap(root[p]);
if (l == r) return;
int mid = l + ((r - l) >> 1);
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
inline void add(int p, int x, int val) {
Insert(root[p], val);
if (Segment_Tree[p].l == Segment_Tree[p].r) return;
int mid = Segment_Tree[p].l + ((Segment_Tree[p].r - Segment_Tree[p].l) >> 1);
if (x <= mid) add(p << 1, x, val);
else add(p << 1 | 1, x, val);
}
inline void remove(int p, int x, int val) {
Remove(root[p], val);
if (Segment_Tree[p].l == Segment_Tree[p].r) return;
int mid = Segment_Tree[p].l + ((Segment_Tree[p].r - Segment_Tree[p].l) >> 1);
if (x <= mid) remove(p << 1, x, val);
else remove(p << 1 | 1, x, val);
}
inline int query(int p, int l, int r, int k) {
if (Segment_Tree[p].l == Segment_Tree[p].r) return Find(root[p]);
int lcnt = Calc(root[p << 1], r) - Calc(root[p << 1], l - 1);
if (k <= lcnt) return query(p << 1, l, r, k);
else return query(p << 1 | 1, l, r, k - lcnt);
}
int main() {
n = read(); m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
b[++eid] = a[i];
}
for (int i = 1; i <= m; i++) {
scanf("%s %d %d", &Commands[i].type, &Commands[i].x, &Commands[i].y);
if (Commands[i].type[0] == 'Q') scanf("%d", &Commands[i].k);
else b[++eid] = Commands[i].y;
}
sort(b + 1, b + 1 + eid);
int M = unique(b + 1, b + 1 + eid) - (b + 1);
build(1, 1, M);
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(b + 1, b + 1 + M, a[i]) - b;
add(1, a[i], i);
}
for (int i = 1; i <= m; i++) {
if (Commands[i].type[0] == 'Q')
printf("%d\n", b[a[query(1, Commands[i].x, Commands[i].y, Commands[i].k)]]);
else {
Commands[i].y = lower_bound(b + 1, b + 1 + M, Commands[i].y) - b;
remove(1, a[Commands[i].x], Commands[i].x);
add(1, Commands[i].y, Commands[i].x);
a[Commands[i].x] = Commands[i].y;
}
}
return 0;
}