【题解】CF-487E-Tourists

题目

http://codeforces.com/contest/487/problem/E

https://www.luogu.org/problemnew/show/CF487E

题意

给一个带点权连通简单无向图,支持

  • 修改点权
  • 询问两点间可能的简单路径上的最小值的最小值

题解

  • 建立广义圆方树,方点的权值定义为树上所有儿子的权值的最小值。
  • 树链剖分 + 线段树维护路径最小值
  • 如果 LCA 是方点需要额外考虑这个方点在树上的父亲的权值

代码(看起来很长,但其实就是几个板子嵌在一起)

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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------
const int N = 2E5 + 100;

namespace X {
struct E { int to, nxt; } e[N];
int hd[N], ecnt;
void addedge(int u, int v) {
e[ecnt] = {v, hd[u]};
hd[u] = ecnt++;
}
int low[N], dfn[N], clk, B, bno[N];
vector<int> bc[N];
bool vise[N];
void init() {
memset(vise, 0, sizeof vise);
memset(hd, -1, sizeof hd);
memset(dfn, 0, sizeof dfn);
memset(bno, -1, sizeof bno);
B = clk = 0;
}

void tarjan(int u, int feid) {
static int st[N], p;
static auto add = [&](int x) {
if (bno[x] != B) { bno[x] = B; bc[B].push_back(x); }
};
low[u] = dfn[u] = ++clk;
for (int i = hd[u]; ~i; i = e[i].nxt) {
if ((feid ^ i) == 1) continue;
if (!vise[i]) { st[p++] = i; vise[i] = vise[i ^ 1] = true; }
int v = e[i].to;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
bc[B].clear();
while (1) {
int eid = st[--p];
add(e[eid].to); add(e[eid ^ 1].to);
if ((eid ^ i) <= 1) break;
}
++B;
}
} else low[u] = min(low[u], dfn[v]);
}
}
}

const int INF = 1E9 + 7;
namespace T {
#define lson o * 2, l, (l + r) / 2
#define rson o * 2 + 1, (l + r) / 2 + 1, r
int minv[N << 2];
void up(int o) { minv[o] = min(minv[o * 2], minv[o * 2 + 1]); }
void mod(int p, int v, int o, int l, int r) {
if (p > r || l > p) return;
if (l == r) minv[o] = v;
else { mod(p, v, lson); mod(p, v, rson); up(o); }
}
int query(int ql, int qr, int o, int l, int r) {
if (l > qr || ql > r) return INF;
if (ql <= l && r <= qr) return minv[o];
return min(query(ql, qr, lson), query(ql, qr, rson));
}
template<typename T>
void build(int o, int l, int r, T&& f) {
if (l == r) minv[o] = f(l);
else { build(lson, f); build(rson, f); up(o); }
}
}

vector<int> G[N];
int w[N];
multiset<int> S[N];

int fa[N], dep[N], idx[N], out[N], ridx[N];
namespace hld {
int sz[N], son[N], top[N], clk;
void predfs(int u, int d) {
dep[u] = d; sz[u] = 1;
int& maxs = son[u] = -1;
for (int& v: G[u]) {
if (v == fa[u]) continue;
fa[v] = u;
predfs(v, d + 1);
sz[u] += sz[v];
if (maxs == -1 || sz[v] > sz[maxs]) maxs = v;
}
}
void dfs(int u, int tp) {
top[u] = tp; idx[u] = ++clk; ridx[clk] = u;
if (son[u] != -1) dfs(son[u], tp);
for (int& v: G[u])
if (v != fa[u] && v != son[u]) dfs(v, v);
out[u] = clk;
}
template<typename T>
int go(int u, int v, T&& f = [](int, int) {}) {
int uu = top[u], vv = top[v];
while (uu != vv) {
if (dep[uu] < dep[vv]) { swap(uu, vv); swap(u, v); }
f(idx[uu], idx[u]);
u = fa[uu]; uu = top[u];
}
if (dep[u] < dep[v]) swap(u, v);
f(idx[v], idx[u]);
return v;
}
}

char s[10];
int main() {
#ifdef zerol
freopen("in", "r", stdin);
#endif
X::init();
int n, m, Qn; cin >> n >> m >> Qn;
FOR (i, 1, n + 1) scanf("%d", &w[i]);
FOR (_, 0, m) {
int u, v; scanf("%d%d", &u, &v);
X::addedge(u, v); X::addedge(v, u);
}
FOR (i, 1, n + 1) if (!X::dfn[i]) X::tarjan(i, -1);
int nn = n;
FOR (i, 0, X::B) {
++nn;
for (int& x: X::bc[i]) {
G[nn].push_back(x);
G[x].push_back(nn);
}
}
hld::predfs(1, -1); hld::dfs(1, 1);
FOR (i, 1, n + 1) {
int f = fa[i];
if (f != -1) {
S[f].insert(w[i]);
}
}
auto f = [&](int x)->int {
int u = ridx[x];
if (u <= n) return w[u];
else return *S[u].begin();
};
T::build(1, 1, nn, f);
while (Qn--) {
int u, v; scanf("%s%d%d", s, &u, &v);
if (s[0] == 'A') {
int ans = INF;
int lca = hld::go(u, v, [&](int l, int r) {
ans = min(ans, T::query(l, r, 1, 1, nn));
});
if (lca > n) ans = min(ans, w[fa[lca]]);
printf("%d\n", ans);
} else {
int f = fa[u];
if (f != -1) {
S[f].erase(S[f].find(w[u]));
S[f].insert(v);
T::mod(idx[f], *S[f].begin(), 1, 1, nn);
}
w[u] = v; T::mod(idx[u], v, 1, 1, nn);
}
}
}
w