[SDOI 2013] 森林

题目大意

给定一个森林,每个点有一个权值 wiw_i,给定 tt 组操作,操作有两种:

  • Q u v k:查询 uuvv 的路径上,第 kk 小的点权值。
  • L u v:连接 uuvv,保证始终为森林。

强制在线(异或 lastAnslastAns)。

1n80,0001 \leqslant n \leqslant 80,000

1wi1,000,000,0001 \leqslant w_i \leqslant 1,000,000,000

题目链接

【SDOI 2013】森林 - Luogu 3302

题解

询问就是 Count on a Tree 那题,用主席树,两点 lca 用倍增求。

连接两点时,用启发式合并,把小的一边加到大的一边,实现用 bfs 跑一遍小的一边即可。块的大小用并查集维护。

代码

居然会有入读测试点这种东西。。。

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
#include <cstdio>
#include <climits>
#include <queue>
#include <algorithm>
// #define DBG
const int MAXN = 80005;
const int MAXN_LOG = 20;
struct PSegT {
int l, r;
PSegT *lc, *rc;
int cnt;
PSegT(int l, int r, PSegT *lc = NULL, PSegT *rc = NULL) : l(l), r(r), lc(lc), rc(rc), cnt((lc ? lc->cnt : 0) + (rc ? rc->cnt : 0)) {}
PSegT(int l, int r, int cnt) : l(l), r(r), cnt(cnt), lc(NULL), rc(NULL) {}
void pushDown() {
if (lc && rc) return;
int mid = l + (r - l) / 2;
if (!lc) lc = new PSegT(l, mid);
if (!rc) rc = new PSegT(mid + 1, r);
}
PSegT *insert(int x) {
if (x < l || x > r) return this;
if (x == l && x == r) return new PSegT(l, r, cnt + 1);
int mid = l + (r - l) / 2;
pushDown();
if (x <= mid) return new PSegT(l, r, lc->insert(x), rc);
else return new PSegT(l, r, lc, rc->insert(x));
}
int rank() {
return lc ? lc->cnt : 0;
}
} *root;
struct Edge;
struct Node {
Edge *e;
PSegT *seg;
Node *f[MAXN_LOG];
int w, belong, dep;
#ifdef DBG
int id;
#endif
} N[MAXN];
struct Edge {
Node *u, *v;
Edge *next;
Edge(Node *u, Node *v) : u(u), v(v), next(u->e) {}
};
void addEdge(int u, int v) {
#ifdef DBG
printf("edge: %d --- %d\n", u, v);
#endif
N[u].e = new Edge(&N[u], &N[v]);
N[v].e = new Edge(&N[v], &N[u]);
}
int n, ccCnt, logn = 0;
void bfs(Node *s, bool init = true) {
++ccCnt;
std::queue<Node *> q;
s->belong = ccCnt;
if (init) {
s->f[0] = s;
s->seg = root->insert(s->w);
s->dep = 0;
}
q.push(s);
while (!q.empty()) {
Node *u = q.front();
q.pop();
for (int i = 1; i <= logn; i++) u->f[i] = u->f[i - 1]->f[i - 1];
for (Edge *e = u->e; e; e = e->next) if (e->v->belong != ccCnt && (init || e->v != s->f[0])) {
e->v->belong = ccCnt;
e->v->f[0] = u;
e->v->dep = u->dep + 1;
e->v->seg = u->seg->insert(e->v->w);
q.push(e->v);
}
}
}
void bfs() {
for (int i = 1; i <= n; i++) if (N[i].belong == 0) bfs(&N[i]);
}
struct UnionFindSet {
int fa[MAXN], size[MAXN];
void init(int n) {
for (int i = 1; i <= n; i++) fa[i] = i, size[i] = 1;
}
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
int p = find(x), q = find(y);
if (size[p] > size[q]) std::swap(p, q);
fa[p] = q;
size[q] += size[p];
}
int getSize(int x) {
return size[find(x)];
}
} ufs;
void link(int u, int v) {
int su = ufs.getSize(u), sv = ufs.getSize(v);
if (su > sv) std::swap(u, v);
addEdge(u, v);
N[u].f[0] = &N[v];
N[u].seg = N[v].seg->insert(N[u].w);
N[u].dep = N[v].dep + 1;
bfs(&N[u], false);
ufs.merge(u, v);
}
Node *lca(Node *u, Node *v) {
if (u->dep < v->dep) std::swap(u, v);
#ifdef DBG
printf("lca(%d, %d), u.dep = %d, v.dep = %d\n", u->id, v->id, u->dep, v->dep);
#endif
if (u->dep != v->dep)
for (int i = logn; ~i; i--) if (u->f[i]->dep >= v->dep) u = u->f[i];
if (u != v) {
for (int i = logn; ~i; i--) if (u->f[i] != v->f[i]) u = u->f[i], v = v->f[i];
return u->f[0];
}
return u;
}
int query(Node *u, Node *v, int k) {
Node *p = lca(u, v);
int l = 1, r = n;
PSegT *su = u->seg, *sv = v->seg, *sp = p->seg, *sf = p != p->f[0] ? p->f[0]->seg : root;
#ifdef DBG
printf("Q %d %d %d, lca = %d\n", u->id, v->id, k, p->id);
#endif
while (l < r) {
int mid = l + (r - l) / 2;
int t = 0;
if (su) t += su->rank();
if (sv) t += sv->rank();
if (sp) t -= sp->rank();
if (sf) t -= sf->rank();
if (k <= t) {
if (su) su = su->lc;
if (sv) sv = sv->lc;
if (sp) sp = sp->lc;
if (sf) sf = sf->lc;
r = mid;
} else {
if (su) su = su->rc;
if (sv) sv = sv->rc;
if (sp) sp = sp->rc;
if (sf) sf = sf->rc;
k -= t;
l = mid + 1;
}
}
return l;
}
int map[MAXN];
void discretization() {
std::sort(map + 1, map + n + 1);
int *end = std::unique(map + 1, map + n + 1);
for (int i = 1; i <= n; i++) N[i].w = std::lower_bound(map + 1, end, N[i].w) - map;
}
int main() {
scanf("%*d");
int m, q;
scanf("%d %d %d", &n, &m, &q);
ufs.init(n);
#ifdef DBG
for (int i = 1; i <= n; i++) N[i].id = i;
#endif
for (; 1 << (logn + 1) <= n; logn++);
for (int i = 1; i <= n; i++) scanf("%d", &N[i].w), map[i] = N[i].w;
discretization();
root = new PSegT(1, n);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
addEdge(u, v);
ufs.merge(u, v);
}
bfs();
int lastAns = 0;
while (q--) {
char op[2];
int u, v;
scanf("%s %d %d", op, &u, &v);
u ^= lastAns;
v ^= lastAns;
#ifdef DBG
printf("%s %d %d\n", op, u, v);
#endif
if (op[0] == 'Q') {
int k;
scanf("%d", &k);
k ^= lastAns;
printf("%d\n", lastAns = map[query(&N[u], &N[v], k)]);
} else link(u, v);
}
return 0;
}