#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
const int LG = 18;
using ll = long long;
int N, M;
int A[MAXN];
int rmq[LG][MAXN];
int best(int x, int y) {
if (A[x] > A[y]) return x;
else return y;
}
ll ans[MAXN], offset[MAXN];
multiset<pair<int, ll>> vals[MAXN];
int get(int l, int r) {
int lg = __lg(r - l + 1);
return best(rmq[lg][l], rmq[lg][r - (1 << lg) + 1]);
}
int solve(int l, int r, int lim) {
int v;
if (l == r) {
v = l;
} else {
int md = get(l, r);
v = solve(md, md, A[md]);
if (l < md) {
int u = solve(l, md - 1, A[md]);
if (vals[v].size() < vals[u].size()) {
swap(v, u);
}
for (auto z : vals[u]) {
vals[v].emplace(z.first, z.second + ans[v] - ans[u]);
}
offset[v] += offset[u] + ans[u];
}
if (md < r) {
int u = solve(md + 1, r, A[md]);
if (vals[v].size() < vals[u].size()) {
swap(v, u);
}
for (auto z : vals[u]) {
vals[v].emplace(z.first, z.second + ans[v] - ans[u]);
}
offset[v] += offset[u] + ans[u];
}
}
while (!vals[v].empty() && vals[v].begin()->first <= lim) {
ans[v] = max(ans[v], vals[v].begin()->second);
vals[v].erase(vals[v].begin());
}
return v;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
rmq[0][i] = i;
}
for (int i = 1; i < LG; ++i) {
for (int j = 0; j + (1 << i) <= N; ++j) {
rmq[i][j] = best(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
}
cin >> M;
ll sum = 0;
while (M--) {
int x, y; ll c;
cin >> x >> y >> c;
vals[--x].emplace(y, c);
sum += c;
}
int v = solve(0, N - 1, N + 1);
cout << sum - (ans[v] + offset[v]) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
11 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
11 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9856 KB |
Output is correct |
21 |
Correct |
10 ms |
10112 KB |
Output is correct |
22 |
Correct |
11 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
11 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
11 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9856 KB |
Output is correct |
21 |
Correct |
10 ms |
10112 KB |
Output is correct |
22 |
Correct |
11 ms |
9856 KB |
Output is correct |
23 |
Correct |
11 ms |
10112 KB |
Output is correct |
24 |
Correct |
11 ms |
10112 KB |
Output is correct |
25 |
Correct |
11 ms |
10112 KB |
Output is correct |
26 |
Correct |
11 ms |
10112 KB |
Output is correct |
27 |
Correct |
11 ms |
10112 KB |
Output is correct |
28 |
Correct |
11 ms |
10112 KB |
Output is correct |
29 |
Correct |
11 ms |
10112 KB |
Output is correct |
30 |
Correct |
11 ms |
10112 KB |
Output is correct |
31 |
Correct |
12 ms |
10112 KB |
Output is correct |
32 |
Correct |
14 ms |
10368 KB |
Output is correct |
33 |
Correct |
12 ms |
10368 KB |
Output is correct |
34 |
Correct |
12 ms |
10240 KB |
Output is correct |
35 |
Correct |
11 ms |
10240 KB |
Output is correct |
36 |
Correct |
11 ms |
10368 KB |
Output is correct |
37 |
Correct |
12 ms |
10368 KB |
Output is correct |
38 |
Correct |
11 ms |
10368 KB |
Output is correct |
39 |
Correct |
12 ms |
10240 KB |
Output is correct |
40 |
Correct |
11 ms |
10368 KB |
Output is correct |
41 |
Correct |
12 ms |
10240 KB |
Output is correct |
42 |
Correct |
12 ms |
10240 KB |
Output is correct |
43 |
Correct |
12 ms |
10368 KB |
Output is correct |
44 |
Correct |
11 ms |
10240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
11 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
11 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9984 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9856 KB |
Output is correct |
21 |
Correct |
10 ms |
10112 KB |
Output is correct |
22 |
Correct |
11 ms |
9856 KB |
Output is correct |
23 |
Correct |
11 ms |
10112 KB |
Output is correct |
24 |
Correct |
11 ms |
10112 KB |
Output is correct |
25 |
Correct |
11 ms |
10112 KB |
Output is correct |
26 |
Correct |
11 ms |
10112 KB |
Output is correct |
27 |
Correct |
11 ms |
10112 KB |
Output is correct |
28 |
Correct |
11 ms |
10112 KB |
Output is correct |
29 |
Correct |
11 ms |
10112 KB |
Output is correct |
30 |
Correct |
11 ms |
10112 KB |
Output is correct |
31 |
Correct |
12 ms |
10112 KB |
Output is correct |
32 |
Correct |
14 ms |
10368 KB |
Output is correct |
33 |
Correct |
12 ms |
10368 KB |
Output is correct |
34 |
Correct |
12 ms |
10240 KB |
Output is correct |
35 |
Correct |
11 ms |
10240 KB |
Output is correct |
36 |
Correct |
11 ms |
10368 KB |
Output is correct |
37 |
Correct |
12 ms |
10368 KB |
Output is correct |
38 |
Correct |
11 ms |
10368 KB |
Output is correct |
39 |
Correct |
12 ms |
10240 KB |
Output is correct |
40 |
Correct |
11 ms |
10368 KB |
Output is correct |
41 |
Correct |
12 ms |
10240 KB |
Output is correct |
42 |
Correct |
12 ms |
10240 KB |
Output is correct |
43 |
Correct |
12 ms |
10368 KB |
Output is correct |
44 |
Correct |
11 ms |
10240 KB |
Output is correct |
45 |
Correct |
218 ms |
39032 KB |
Output is correct |
46 |
Correct |
220 ms |
38520 KB |
Output is correct |
47 |
Correct |
224 ms |
38648 KB |
Output is correct |
48 |
Correct |
207 ms |
38776 KB |
Output is correct |
49 |
Correct |
216 ms |
38264 KB |
Output is correct |
50 |
Correct |
204 ms |
38012 KB |
Output is correct |
51 |
Correct |
210 ms |
38392 KB |
Output is correct |
52 |
Correct |
226 ms |
38904 KB |
Output is correct |
53 |
Correct |
215 ms |
38532 KB |
Output is correct |
54 |
Correct |
303 ms |
65144 KB |
Output is correct |
55 |
Correct |
339 ms |
60408 KB |
Output is correct |
56 |
Correct |
323 ms |
57720 KB |
Output is correct |
57 |
Correct |
361 ms |
56184 KB |
Output is correct |
58 |
Correct |
310 ms |
63352 KB |
Output is correct |
59 |
Correct |
317 ms |
63352 KB |
Output is correct |
60 |
Correct |
160 ms |
72952 KB |
Output is correct |
61 |
Correct |
309 ms |
65144 KB |
Output is correct |
62 |
Correct |
312 ms |
68344 KB |
Output is correct |
63 |
Correct |
300 ms |
60412 KB |
Output is correct |
64 |
Correct |
303 ms |
62712 KB |
Output is correct |
65 |
Correct |
311 ms |
68472 KB |
Output is correct |
66 |
Correct |
315 ms |
63352 KB |
Output is correct |