FHQ-Treap学习笔记

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/141201809

FHQ-treap核心函数:merge和split

使用时就是把一段区间给split出来,然后打tag,然后再merge起来

Treap的特点:满足堆的性质

对于merge,我们把优先级小的作为根,然后递归下去

split直接按size决定往左还是往右,通过引用来不断完善两棵树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int merge(int x, int y) {
if(!x || !y) return x + y;
if(x) push_down(x); if(y) push_down(y);
if(pri[x] < pri[y]) {
rs[x] = merge(rs[x], y); push_up(x);
return x;
}
else {
ls[y] = merge(x, ls[y]); push_up(y);
return y;
}
}
void split(int k, int sze, int &x, int &y) {
if(!k) return x = y = 0, void();
push_down(k);
if(w[ls[k]] + 1 > sze) {
y = k; split(ls[k], sze, x, ls[y]);
}
else {
x = k; split(rs[k], sze - w[ls[k]] - 1, rs[x], y);
}
push_up(k);
}

P2596 ZJOI2006 书架

每次模拟拿出对应的那个东西,然后接到头或尾。或者拿出相邻两个,调换顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define debag(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 80010
int n, m, i, j, k;
int root, mp[N];

struct fhq_treap {
int w[N], ls[N], rs[N], pri[N], a[N], fa[N], tot;
void print1(int x) {
if(!x) return;
printf("%d(%d) -> [%d %d]\n", a[x], w[x], a[ls[x]], a[rs[x]]);
print1(ls[x]); print1(rs[x]);
}
void print2(int x) {
if(!x) return ;
print2(ls[x]); printf("%d ", a[x]); print2(rs[x]);
if(x == root) printf("\n------(%d)-------\n", w[0]);
}
void push_up(int x) {
w[x] = 1 + w[ls[x]] + w[rs[x]];
}
int merge(int x, int y) {
if(!x || !y) return x + y;
if(pri[x] < pri[y]) {
rs[x] = merge(rs[x], y);
fa[ls[y]] = fa[rs[y]] = y;
fa[ls[x]] = fa[rs[x]] = x; fa[0] = 0;
push_up(x);
return x;
}
else {
ls[y] = merge(x, ls[y]);
fa[ls[y]] = fa[rs[y]] = y;
fa[ls[x]] = fa[rs[x]] = x; fa[0] = 0;
push_up(y);
return y;
}
}
void split(int k, int sze, int &x, int &y) {
if(!k) return x = y = 0, void();
// debug("At %d (%d | %d) : ", a[k], sze, w[ls[k]]);
if(w[ls[k]] + 1 <= sze) {
// debug("To r\n");
x = k; split(rs[k], sze - w[ls[k]] - 1, rs[x], y);
fa[rs[x]] = x;
}
else {
// debug("To l\n");
y = k; split(ls[k], sze, x, ls[y]);
fa[ls[y]] = y;
}
push_up(k);
}
void add(int id) { // 插入至末尾
int x = ++tot;
a[x] = id; w[x] = 1; pri[x] = rand(); mp[id] = x;
root = merge(root, x);
}
int rank(int k) {
int x = mp[k], ans = w[ls[x]] + 1;
assert(a[x] == k);
while(fa[x]) {
if(fa[x]) assert(ls[fa[x]] == x || rs[fa[x]] == x);
// debug("%d -> %d ans += %d\n", a[x], a[fa[x]], w[ls[x]]);
if(x != ls[fa[x]]) ans += w[ls[fa[x]]] + 1;
x = fa[x];
}
return ans;
}
int find(int x, int s) {
while(x) {
// debug("%d(%d %d) %d\n", a[x], w[x], w[ls[x]], s);
if(w[ls[x]] + 1 == s) return a[x];
if(w[ls[x]] >= s) x = ls[x];
else s -= (w[ls[x]] + 1), x = rs[x];
}
assert(x); return x;
}
}T;

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
srand(time(0));
// srand(55);
n = read(); m = read();
for(i = 1; i <= n; ++i) {
k = read(); T.add(k);
}
char op[10];
int x, y, z; //m = 0;
// T.print1(root); T.print2(root);
for(i = 1; i <= m; ++i) {
scanf("%s", op); int s = read();
// debag("%s%d\n", op, s);
if(op[0] == 'T' || op[0] == 'B') {
int k = T.rank(s), x, y, z;
T.split(root, k - 1, x, y);
T.split(y, 1, y, z);
// debug("========= %d ========\n", k);
// T.print2(x); debug(" | "); T.print1(y); debug(" | "); T.print1(z);
// debug("\n=================\n");
x = T.merge(x, z);
if(op[0] == 'T') root = T.merge(y, x);
if(op[0] == 'B') root = T.merge(x, y);
}
if(op[0] == 'I') {
int t = read(); k = T.rank(s); if(!t) continue;
if(t == 1) {
T.split(root, k - 1, x, root);
T.split(root, 1, y, root);
T.split(root, 1, z, root);
}
else if(t == -1) {
T.split(root, k - 2, x, root);
T.split(root, 1, y, root);
T.split(root, 1, z, root);
}
x = T.merge(x, z); x = T.merge(x, y); root = T.merge(x, root);
}
if(op[0] == 'A') {
k = T.rank(s); printf("%d\n", k - 1);
}
if(op[0] == 'Q') {
printf("%d\n", T.find(root, s));
}
T.fa[root] = 0;
// T.print2(root);
}
return 0;
}



P2042 NOI2005 维护数列

这题要维护的东西很多,但多开几个数组,写多一下pushup和pushdown就行。但在pushup或者访问的时候一定要先把儿子pushdown了。

对于多个给定值,如果快速建fhqtreap?

我们可以使用栈来实现。因为是按顺序给,所以我们只需要维护最右边一条链,

  1. 比较栈顶节点与当前节点优先级,若大于,则退栈

  2. 最后一个弹出的点为当前点的左儿子(比它前,但优先级少于它)

  3. 此时它是栈顶点的右儿子

注意,在这个过程中,要时刻pushup和pushdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 500010
int n, m, i, j, k, root;

struct fhq_treap {
int tot, ls[N], rs[N], w[N], a[N], pri[N];
int s[N], sl[N], sr[N], mx[N], b[N];
bool tag[N];
queue<int> q;
int st[500010], top;
void push_up(int k) {
if(ls[k]) push_down(ls[k]);
if(rs[k]) push_down(rs[k]);
w[k] = 1 + w[ls[k]] + w[rs[k]];
s[k] = s[ls[k]] + s[rs[k]] + a[k];
sl[0] = sr[0] = mx[0] = -2e9;
sl[k] = max({sl[ls[k]], s[ls[k]] + a[k], s[ls[k]] + a[k] + sl[rs[k]]});
sr[k] = max({sr[rs[k]], s[rs[k]] + a[k], s[rs[k]] + a[k] + sr[ls[k]]});
mx[k] = max(mx[ls[k]], mx[rs[k]]);
mx[k] = max(mx[k], max(0, sr[ls[k]]) + a[k] + max(0, sl[rs[k]]));
sl[0] = sr[0] = mx[0] = 0;
// if(a[k] == 3) debug("======== %lld %lld %lld %lld\n", sl[k], sr[k], s[k], mx[k]);
}
void push_down(int k) {
if(b[k] != 1e9) {
// debug("I am back!\n");
if(ls[k]) b[ls[k]] = b[k];
if(rs[k]) b[rs[k]] = b[k];
s[k] = b[k] * w[k]; a[k] = b[k];
sl[k] = sr[k] = mx[k] = max(b[k], b[k] * w[k]);
tag[k] = 0; b[k] = 1e9;
return ;
}
if(tag[k]) {
tag[ls[k]] ^= 1; tag[rs[k]] ^= 1; tag[k] = 0;
swap(ls[k], rs[k]);
swap(sl[k], sr[k]);
}
}
void print1(int x) {
if(!x) return;
// push_down(x);
printf("%lld(%lld) -> [%lld %lld] (%lld %lld %lld %lld)\n", a[x], w[x], a[ls[x]], a[rs[x]], s[x], sl[x], sr[x], mx[x]);
print1(ls[x]); print1(rs[x]);
}
void print2(int x) {
if(!x) return ;
// push_down(x);
print2(ls[x]); printf("%d ", a[x]); print2(rs[x]);
// if(x == root) printf("\n------(%lld)-------\n", w[0]);
}
int merge(int x, int y) {
if(!x || !y) return x + y;
if(x) push_down(x); if(y) push_down(y);
if(pri[x] < pri[y]) {
rs[x] = merge(rs[x], y); push_up(x);
return x;
}
else {
ls[y] = merge(x, ls[y]); push_up(y);
return y;
}
}
void split(int k, int sze, int &x, int &y) {
if(!k) return x = y = 0, void();
push_down(k);
// debug("At %lld try to %lld | %lld\n", k, sze, w[ls[k]]);
if(w[ls[k]] + 1 > sze) {
// printf("Go l\n");
y = k; split(ls[k], sze, x, ls[y]);
}
else {
// printf("Go r\n");
x = k; split(rs[k], sze - w[ls[k]] - 1, rs[x], y);
}
push_up(k);
// if(x) push_up(x); if(y) push_up(y);
}
int ask() {
int u;
if(!q.empty()) u = q.front(), q.pop();
else u = ++tot;
return u;
}
void insert(int val) {
int u = ask(); w[u] = 1; pri[u] = rand();
a[u] = s[u] = sl[u] = sr[u] = mx[u] = val; b[u] = 1e9;
root = merge(root, u);
}
int build(int op = 0) {
int n = read(), val, top = 0, ans;
if(op) m = read();
while(n--) {
val = read();
int u = ask(), las = 0; w[u] = 1; pri[u] = rand();
a[u] = s[u] = sl[u] = sr[u] = mx[u] = val; b[u] = 1e9;
while(top && pri[st[top]] > pri[u])
las = st[top], push_up(las), --top;
ls[u] = las; push_up(u);
if(top) rs[st[top]] = u, push_up(st[top]);
else ans = u;
st[++top] = u;
}
while(top) push_up(st[top]), --top;
return ans;
}
void cun(int x) {
if(ls[x]) cun(ls[x]);
if(rs[x]) cun(rs[x]);
ls[x] = rs[x] = mx[x] = w[x] = pri[x] =
s[x] = sl[x] = sr[x] = a[x] = tag[x] = 0; b[x] = 0;
q.push(x);
}
}T;

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
srand(440103);
// srand(time(0));
// printf("--------\n");
root = T.build(1);
// T.print1(root);
// T.print2(root); printf("\n");
char op[20];
for(i = 1; i <= m; ++i) {
scanf("%s", op);
if(op[0] == 'I') {
k = read();
int u = T.build(), x, y;
// T.print2(u); printf("\n");
T.split(root, k, x, y);
root = T.merge(x, u); root = T.merge(root, y);
}
if(op[0] == 'D') {
k = read(); j = read();
if(j == 0) continue;
int x, y, z;
T.split(root, k - 1, x, y);
T.split(y, j, y, z);
root = T.merge(x, z);
T.cun(y);
}
if(op[0] == 'M' && op[6] == 'A') {
int x, y, z, c;
k = read(); j = read(); c = read();
if(j == 0) continue;
T.split(root, k - 1, x, y);
T.split(y, j, y, z);
T.b[y] = c; T.push_down(y);
root = T.merge(x, y); root = T.merge(root, z);
}
if(op[0] == 'R') {
int x, y, z;
k = read(); j = read();
if(j == 0) continue;
T.split(root, k - 1, x, y);
T.split(y, j, y, z);
T.tag[y] ^= 1; T.push_down(y);
root = T.merge(x, y); root = T.merge(root, z);
}
if(op[0] == 'G') {
int x, y, z;
k = read(); j = read();
if(j == 0) { printf("0\n"); continue; }
T.split(root, k - 1, x, y);
T.split(y, j, y, z);
T.push_down(y);
// T.print2(x); printf("\n");
// T.print1(y); printf("\n");
// T.print2(z); printf("\n");
printf("%d\n", T.s[y]); //printf("> [%lld %lld]\n", T.w[s[ls[y]]], )
root = T.merge(x, y); root = T.merge(root, z);
}
if(op[0] == 'M' && op[6] == 'M') {
//T.push_down(root); //T.print1(root);
printf("%d\n", T.mx[root]);
}
// T.print2(root); printf("\n");
}
return 0;
}