#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
using namespace std;
// sunt putin penal ca mergea aib
namespace SegmTree {
int n;
vector<int64_t> tree, lazy;
void Init(int n_) {
n = n_;
tree.assign(4 * n, 0);
lazy.assign(4 * n, 0);
}
void Push(int node) {
if (lazy[node] == 0) return;
for (int son : {2 * node + 1, 2 * node + 2}) {
tree[son] += lazy[node];
lazy[son] += lazy[node];
}
lazy[node] = 0;
}
void Add(int node, int left, int right, int x, int y, int64_t val) {
if (x <= left && right <= y) {
tree[node] += val;
lazy[node] += val;
return;
}
Push(node);
int mid = left + (right - left) / 2;
if (x <= mid)
Add(2 * node + 1, left, mid, x, y, val);
if (mid < y)
Add(2 * node + 2, mid + 1, right, x, y, val);
}
int64_t Query(int node, int left, int right, int pos) {
if (left == right) {
return tree[node];
}
Push(node);
int mid = left + (right - left) / 2;
if (pos <= mid)
return Query(2 * node + 1, left, mid, pos);
else
return Query(2 * node + 2, mid + 1, right, pos);
}
void Add(int left, int right, int64_t val) {
Add(0, 0, n - 1, left, right, val);
}
int64_t Query(int pos) {
return Query(0, 0, n - 1, pos);
}
}
namespace DSU {
int n;
vector<int> dad, sz, lft, rgt;
vector<int64_t> dp;
void Init(int n_) {
n = n_;
dad.resize(n), sz.resize(n), lft.resize(n), rgt.resize(n), dp.resize(n);
for (int i = 0; i < n; ++i) {
lft[i] = rgt[i] = i;
dad[i] = -1;
sz[i] = 1;
dp[i] = 0;
}
}
int Find(int x) {
if (dad[x] == -1) return x;
return dad[x] = Find(dad[x]);
}
void Union(int x, int y) {
x = Find(x), y = Find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
// dbg() "Union: " << name(x) name(y) endl;
sz[x] += sz[y];
dad[y] = x;
SegmTree::Add(lft[x], rgt[x], dp[y]);
SegmTree::Add(lft[y], rgt[y], dp[x]);
dp[x] += dp[y];
lft[x] = min(lft[x], lft[y]);
rgt[x] = max(rgt[x], rgt[y]);
}
void Update(int x, int64_t val) {
// dbg() "DSU Upd: " << name(x) name(val) endl;
x = Find(x);
dp[x] = max(dp[x], val);
}
int64_t Get(int node) {
return dp[Find(node)];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<int> h(n);
vector<vector<int>> walls(n + 1);
for (int i = 0; i < n; ++i) {
cin >> h[i];
walls[h[i]].emplace_back(i);
}
int m; cin >> m;
vector<vector<pair<int, int>>> stars(n + 1);
int64_t ans = 0;
for (int i = 0; i < m; ++i) {
int x, y, c; cin >> x >> y >> c; --x, --y;
stars[y].emplace_back(x, c);
ans += c;
}
// le sterg pe toate si incerc sa pastrez o suma cat mai mare
DSU::Init(n);
SegmTree::Init(n);
for (int y = 0; y <= n; ++y) {
for (int &x : walls[y]) {
if (x - 1 >= 0 && h[x - 1] <= y) {
DSU::Union(x - 1, x);
}
if (x + 1 < n && h[x + 1] <= y) {
DSU::Union(x, x + 1);
}
}
// for (int i = 0; i < n; ++i) {
// dbg() name(i) name(DSU::Get(i)) endl;
// }
for (auto &star : stars[y]) {
int x, c; tie(x, c) = star;
DSU::Update(x, c + SegmTree::Query(x));
}
}
for (int i = 1; i < n; ++i) {
DSU::Union(i - 1, i);
}
cout << ans - DSU::Get(0) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
8 ms |
768 KB |
Output is correct |
27 |
Correct |
7 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
768 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
768 KB |
Output is correct |
32 |
Correct |
7 ms |
768 KB |
Output is correct |
33 |
Correct |
8 ms |
768 KB |
Output is correct |
34 |
Correct |
7 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
8 ms |
660 KB |
Output is correct |
38 |
Correct |
6 ms |
768 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
8 ms |
768 KB |
Output is correct |
27 |
Correct |
7 ms |
768 KB |
Output is correct |
28 |
Correct |
7 ms |
768 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
7 ms |
768 KB |
Output is correct |
32 |
Correct |
7 ms |
768 KB |
Output is correct |
33 |
Correct |
8 ms |
768 KB |
Output is correct |
34 |
Correct |
7 ms |
768 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
8 ms |
660 KB |
Output is correct |
38 |
Correct |
6 ms |
768 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
375 ms |
35448 KB |
Output is correct |
46 |
Correct |
375 ms |
35192 KB |
Output is correct |
47 |
Correct |
354 ms |
34552 KB |
Output is correct |
48 |
Correct |
371 ms |
35576 KB |
Output is correct |
49 |
Correct |
355 ms |
34296 KB |
Output is correct |
50 |
Correct |
354 ms |
34296 KB |
Output is correct |
51 |
Correct |
350 ms |
34424 KB |
Output is correct |
52 |
Correct |
356 ms |
35064 KB |
Output is correct |
53 |
Correct |
358 ms |
35064 KB |
Output is correct |
54 |
Correct |
298 ms |
37864 KB |
Output is correct |
55 |
Correct |
298 ms |
37064 KB |
Output is correct |
56 |
Correct |
280 ms |
36600 KB |
Output is correct |
57 |
Correct |
289 ms |
36472 KB |
Output is correct |
58 |
Correct |
258 ms |
31480 KB |
Output is correct |
59 |
Correct |
263 ms |
31352 KB |
Output is correct |
60 |
Correct |
213 ms |
37752 KB |
Output is correct |
61 |
Correct |
235 ms |
31096 KB |
Output is correct |
62 |
Correct |
283 ms |
34168 KB |
Output is correct |
63 |
Correct |
237 ms |
30720 KB |
Output is correct |
64 |
Correct |
238 ms |
30200 KB |
Output is correct |
65 |
Correct |
281 ms |
34552 KB |
Output is correct |
66 |
Correct |
238 ms |
29936 KB |
Output is correct |