#include <bits/stdc++.h>
#define ws weights
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
const int inf = 1e9 + 20;
const ll infty = (ll) (1e18);
int dsu[N];
vector <int> vs[N], ws[N];
int get(int v) {
if (v == dsu[v]) return v;
else return dsu[v] = get(dsu[v]);
}
void uni(int u, int v, int w) {
u = get(u), v = get(v);
if (vs[u].size() > vs[v].size()) swap(u, v);
for (int x : vs[u]) vs[v].push_back(x);
ws[v].push_back(w);
for (int x : ws[u]) ws[v].push_back(x);
dsu[u] = v;
}
int cost[N];
int edge_r[N];
ll t[4 * N][2][2];
void recalc(int v, int l, int r) {
int m = (l + r) / 2;
int my_w = edge_r[m - 1];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
t[v][i][j] = t[v * 2 + 1][i][1] + t[v * 2 + 2][1][j];
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int k = 1 ^ j;
for (int l = 0; l < 2; l++) {
t[v][i][l] = min(t[v][i][l], t[v * 2 + 1][i][j] + t[v * 2 + 2][k][l] + my_w);
}
}
}
}
void build_leaf(int v, int i) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
t[v][x][y] = (x && y ? cost[i] : 0);
}
}
}
void build(int v, int l, int r) {
if (r - l == 1) {
build_leaf(v, l);
} else {
int m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m, r);
recalc(v, l, r);
}
}
void upd(int v, int l, int r, int i) {
if (r - l == 1) {
build_leaf(v, l);
} else {
int m = (l + r) / 2;
if (i < m) {
upd(v * 2 + 1, l, m, i);
} else {
upd(v * 2 + 2, m, r, i);
}
recalc(v, l, r);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector <int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector <pair <int, pair <int, int> > > e;
auto add_edge = [&] (int a, int b, int w) {
e.push_back({w, {a, b}});
};
for (int i = 2; i <= n; i++) {
add_edge(i, i - 1, inf);
}
for (int i = 0; i < m; i++) {
int w, u, v;
cin >> u >> v >> w;
add_edge(u, v, w);
}
sort(e.begin(), e.end());
for (int i = 1; i <= n; i++) {
dsu[i] = i;
vs[i] = {i};
ws[i] = {};
}
for (auto c : e) {
int u = c.second.first, v = c.second.second;
if (get(u) != get(v)) {
uni(u, v, c.first);
}
}
vector <int> perm(n + 1);
int root = get(1);
for (int i = 0; i < (int) vs[root].size(); i++) {
perm[vs[root][i]] = i;
if (i + 1 < (int) vs[root].size())
edge_r[i] = ws[root][i];
}
for (int i = 1; i <= n; i++) {
cost[perm[i]] = a[i];
}
build(0, 0, n);
int q;
cin >> q;
while (q--) {
int i, w;
cin >> i >> w;
i = perm[i];
cost[i] = w;
upd(0, 0, n, i);
cout << t[0][1][1] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
12 ms |
14416 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
13 ms |
14516 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
953 ms |
80060 KB |
Output is correct |
2 |
Correct |
835 ms |
76520 KB |
Output is correct |
3 |
Correct |
288 ms |
20656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
20628 KB |
Output is correct |
2 |
Correct |
249 ms |
18480 KB |
Output is correct |
3 |
Correct |
187 ms |
16696 KB |
Output is correct |
4 |
Correct |
1074 ms |
82808 KB |
Output is correct |
5 |
Correct |
1052 ms |
80456 KB |
Output is correct |
6 |
Correct |
929 ms |
79084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
360 ms |
74272 KB |
Output is correct |
2 |
Correct |
405 ms |
74376 KB |
Output is correct |
3 |
Correct |
398 ms |
74652 KB |
Output is correct |
4 |
Correct |
280 ms |
21476 KB |
Output is correct |
5 |
Correct |
255 ms |
20276 KB |
Output is correct |
6 |
Correct |
173 ms |
18784 KB |
Output is correct |
7 |
Correct |
706 ms |
82656 KB |
Output is correct |
8 |
Correct |
674 ms |
82656 KB |
Output is correct |
9 |
Correct |
699 ms |
82808 KB |
Output is correct |
10 |
Correct |
685 ms |
82800 KB |
Output is correct |
11 |
Correct |
676 ms |
82728 KB |
Output is correct |
12 |
Correct |
686 ms |
82648 KB |
Output is correct |
13 |
Correct |
677 ms |
82708 KB |
Output is correct |
14 |
Correct |
676 ms |
82780 KB |
Output is correct |
15 |
Correct |
823 ms |
82704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
360 ms |
74272 KB |
Output is correct |
2 |
Correct |
405 ms |
74376 KB |
Output is correct |
3 |
Correct |
398 ms |
74652 KB |
Output is correct |
4 |
Correct |
280 ms |
21476 KB |
Output is correct |
5 |
Correct |
255 ms |
20276 KB |
Output is correct |
6 |
Correct |
173 ms |
18784 KB |
Output is correct |
7 |
Correct |
706 ms |
82656 KB |
Output is correct |
8 |
Correct |
674 ms |
82656 KB |
Output is correct |
9 |
Correct |
699 ms |
82808 KB |
Output is correct |
10 |
Correct |
685 ms |
82800 KB |
Output is correct |
11 |
Correct |
676 ms |
82728 KB |
Output is correct |
12 |
Correct |
686 ms |
82648 KB |
Output is correct |
13 |
Correct |
677 ms |
82708 KB |
Output is correct |
14 |
Correct |
676 ms |
82780 KB |
Output is correct |
15 |
Correct |
823 ms |
82704 KB |
Output is correct |
16 |
Correct |
387 ms |
74200 KB |
Output is correct |
17 |
Correct |
405 ms |
74504 KB |
Output is correct |
18 |
Correct |
421 ms |
74592 KB |
Output is correct |
19 |
Correct |
286 ms |
21404 KB |
Output is correct |
20 |
Correct |
266 ms |
20264 KB |
Output is correct |
21 |
Correct |
178 ms |
18800 KB |
Output is correct |
22 |
Correct |
670 ms |
82748 KB |
Output is correct |
23 |
Correct |
670 ms |
82728 KB |
Output is correct |
24 |
Correct |
724 ms |
82736 KB |
Output is correct |
25 |
Correct |
732 ms |
82640 KB |
Output is correct |
26 |
Correct |
663 ms |
82700 KB |
Output is correct |
27 |
Correct |
766 ms |
82748 KB |
Output is correct |
28 |
Correct |
721 ms |
82948 KB |
Output is correct |
29 |
Correct |
628 ms |
82708 KB |
Output is correct |
30 |
Correct |
731 ms |
82684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1147 ms |
100572 KB |
Output is correct |
2 |
Correct |
1259 ms |
99656 KB |
Output is correct |
3 |
Correct |
1272 ms |
100152 KB |
Output is correct |
4 |
Correct |
1313 ms |
99692 KB |
Output is correct |
5 |
Correct |
1176 ms |
99092 KB |
Output is correct |
6 |
Correct |
1197 ms |
99372 KB |
Output is correct |
7 |
Correct |
1176 ms |
100548 KB |
Output is correct |
8 |
Correct |
1186 ms |
99612 KB |
Output is correct |
9 |
Correct |
1124 ms |
100196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
12 ms |
14416 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
13 ms |
14516 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
6 |
Correct |
295 ms |
44496 KB |
Output is correct |
7 |
Correct |
380 ms |
44616 KB |
Output is correct |
8 |
Correct |
305 ms |
44852 KB |
Output is correct |
9 |
Correct |
478 ms |
48560 KB |
Output is correct |
10 |
Correct |
156 ms |
18048 KB |
Output is correct |
11 |
Correct |
140 ms |
17520 KB |
Output is correct |
12 |
Correct |
115 ms |
16956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
12 ms |
14416 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
13 ms |
14516 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
6 |
Correct |
953 ms |
80060 KB |
Output is correct |
7 |
Correct |
835 ms |
76520 KB |
Output is correct |
8 |
Correct |
288 ms |
20656 KB |
Output is correct |
9 |
Correct |
284 ms |
20628 KB |
Output is correct |
10 |
Correct |
249 ms |
18480 KB |
Output is correct |
11 |
Correct |
187 ms |
16696 KB |
Output is correct |
12 |
Correct |
1074 ms |
82808 KB |
Output is correct |
13 |
Correct |
1052 ms |
80456 KB |
Output is correct |
14 |
Correct |
929 ms |
79084 KB |
Output is correct |
15 |
Correct |
360 ms |
74272 KB |
Output is correct |
16 |
Correct |
405 ms |
74376 KB |
Output is correct |
17 |
Correct |
398 ms |
74652 KB |
Output is correct |
18 |
Correct |
280 ms |
21476 KB |
Output is correct |
19 |
Correct |
255 ms |
20276 KB |
Output is correct |
20 |
Correct |
173 ms |
18784 KB |
Output is correct |
21 |
Correct |
706 ms |
82656 KB |
Output is correct |
22 |
Correct |
674 ms |
82656 KB |
Output is correct |
23 |
Correct |
699 ms |
82808 KB |
Output is correct |
24 |
Correct |
685 ms |
82800 KB |
Output is correct |
25 |
Correct |
676 ms |
82728 KB |
Output is correct |
26 |
Correct |
686 ms |
82648 KB |
Output is correct |
27 |
Correct |
677 ms |
82708 KB |
Output is correct |
28 |
Correct |
676 ms |
82780 KB |
Output is correct |
29 |
Correct |
823 ms |
82704 KB |
Output is correct |
30 |
Correct |
387 ms |
74200 KB |
Output is correct |
31 |
Correct |
405 ms |
74504 KB |
Output is correct |
32 |
Correct |
421 ms |
74592 KB |
Output is correct |
33 |
Correct |
286 ms |
21404 KB |
Output is correct |
34 |
Correct |
266 ms |
20264 KB |
Output is correct |
35 |
Correct |
178 ms |
18800 KB |
Output is correct |
36 |
Correct |
670 ms |
82748 KB |
Output is correct |
37 |
Correct |
670 ms |
82728 KB |
Output is correct |
38 |
Correct |
724 ms |
82736 KB |
Output is correct |
39 |
Correct |
732 ms |
82640 KB |
Output is correct |
40 |
Correct |
663 ms |
82700 KB |
Output is correct |
41 |
Correct |
766 ms |
82748 KB |
Output is correct |
42 |
Correct |
721 ms |
82948 KB |
Output is correct |
43 |
Correct |
628 ms |
82708 KB |
Output is correct |
44 |
Correct |
731 ms |
82684 KB |
Output is correct |
45 |
Correct |
1147 ms |
100572 KB |
Output is correct |
46 |
Correct |
1259 ms |
99656 KB |
Output is correct |
47 |
Correct |
1272 ms |
100152 KB |
Output is correct |
48 |
Correct |
1313 ms |
99692 KB |
Output is correct |
49 |
Correct |
1176 ms |
99092 KB |
Output is correct |
50 |
Correct |
1197 ms |
99372 KB |
Output is correct |
51 |
Correct |
1176 ms |
100548 KB |
Output is correct |
52 |
Correct |
1186 ms |
99612 KB |
Output is correct |
53 |
Correct |
1124 ms |
100196 KB |
Output is correct |
54 |
Correct |
295 ms |
44496 KB |
Output is correct |
55 |
Correct |
380 ms |
44616 KB |
Output is correct |
56 |
Correct |
305 ms |
44852 KB |
Output is correct |
57 |
Correct |
478 ms |
48560 KB |
Output is correct |
58 |
Correct |
156 ms |
18048 KB |
Output is correct |
59 |
Correct |
140 ms |
17520 KB |
Output is correct |
60 |
Correct |
115 ms |
16956 KB |
Output is correct |
61 |
Correct |
12 ms |
14412 KB |
Output is correct |
62 |
Correct |
757 ms |
74376 KB |
Output is correct |
63 |
Correct |
752 ms |
74456 KB |
Output is correct |
64 |
Correct |
705 ms |
74688 KB |
Output is correct |
65 |
Correct |
363 ms |
21488 KB |
Output is correct |
66 |
Correct |
263 ms |
20272 KB |
Output is correct |
67 |
Correct |
215 ms |
18760 KB |
Output is correct |
68 |
Correct |
1080 ms |
82784 KB |
Output is correct |