#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n, m;
ll ans = 0;
ll segtree[800001], lazy[800001];
void push_lazy(int node, int l, int r) {
segtree[node] += lazy[node];
if (l != r) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int a, int b, ll val, int node = 1, int l = 1, int r = n) {
push_lazy(node, l, r);
if (l > b || r < a) return;
if (l >= a && r <= b) {
lazy[node] = val;
push_lazy(node, l, r);
} else {
int mid = (l + r) / 2;
update(a, b, val, node * 2, l, mid);
update(a, b, val, node * 2 + 1, mid + 1, r);
segtree[node] = max(segtree[node * 2], segtree[node * 2 + 1]);
}
}
ll query(int a, int b, int node = 1, int l = 1, int r = n) {
push_lazy(node, l, r);
if (l > b || r < a) return 0;
if (l >= a && r <= b) return segtree[node];
int mid = (l + r) / 2;
return max(query(a, b, node * 2, l, mid), query(a, b, node * 2 + 1, mid + 1, r));
}
int cmp[200001];
pair<int, int> range[200001];
int find(int A) {
while (A != cmp[A]) cmp[A] = cmp[cmp[A]], A = cmp[A];
return A;
}
void onion(int A, int B) {
A = find(A), B = find(B);
range[A].first = min(range[A].first, range[B].first);
range[A].second = max(range[A].second, range[B].second);
cmp[B] = A;
}
bool active[200001];
int a[200001];
vector<int> cmp_start[200002];
vector<pair<int, ll>> col[200001], row[200001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
FOR(i, 1, n + 1) {
cin >> a[i];
cmp_start[a[i] + 1].push_back(i);
}
cin >> m;
FOR(i, 1, m + 1) {
int x, y;
ll v;
cin >> x >> y >> v;
col[x].push_back({y, v});
ans += v;
}
FOR(i, 1, n + 1) {
sort(col[i].begin(), col[i].end());
vector<pair<int, ll>> keep;
ll last = 0;
for (pair<int, ll> j : col[i]) if (j.second > last) {
keep.push_back(j);
last = j.second;
}
last = 0;
for (pair<int, ll> j : keep) {
row[j.first].push_back({i, j.second - last});
last = j.second;
}
}
iota(cmp + 1, cmp + n + 1, 1);
FOR(i, 1, n + 1) range[i] = {i, i};
FOR(i, 1, n + 1) {
for (int x : cmp_start[i]) {
ll l_mx = 0, r_mx = 0, dp = 0;
if (x - 1 && active[x - 1]) {
l_mx = query(range[find(x - 1)].first, range[find(x - 1)].second);
dp += l_mx;
}
if (x + 1 <= n && active[x + 1]) {
r_mx = query(range[find(x + 1)].first, range[find(x + 1)].second);
dp += r_mx;
}
if (x - 1 && active[x - 1])
update(range[find(x - 1)].first, range[find(x - 1)].second, r_mx);
if (x + 1 <= n && active[x + 1])
update(range[find(x + 1)].first, range[find(x + 1)].second, l_mx);
if (x - 1 && active[x - 1]) onion(x - 1, x);
if (x + 1 <= n && active[x + 1]) onion(x, x + 1);
active[x] = true;
update(x, x, dp);
}
for (pair<int, ll> star : row[i]) update(star.first, star.first, star.second);
}
ll mx = 0;
FOR(i, 1, n + 1) {
if (a[i] == n) {
ans -= mx;
mx = 0;
} else mx = max(mx, query(i, i));
}
ans -= mx;
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
15 ms |
14720 KB |
Output is correct |
24 |
Correct |
16 ms |
14720 KB |
Output is correct |
25 |
Correct |
16 ms |
14720 KB |
Output is correct |
26 |
Correct |
16 ms |
14720 KB |
Output is correct |
27 |
Correct |
16 ms |
14720 KB |
Output is correct |
28 |
Correct |
16 ms |
14720 KB |
Output is correct |
29 |
Correct |
16 ms |
14720 KB |
Output is correct |
30 |
Correct |
16 ms |
14720 KB |
Output is correct |
31 |
Correct |
17 ms |
14720 KB |
Output is correct |
32 |
Correct |
16 ms |
14720 KB |
Output is correct |
33 |
Correct |
16 ms |
14720 KB |
Output is correct |
34 |
Correct |
19 ms |
14720 KB |
Output is correct |
35 |
Correct |
16 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14720 KB |
Output is correct |
37 |
Correct |
16 ms |
14720 KB |
Output is correct |
38 |
Correct |
16 ms |
14720 KB |
Output is correct |
39 |
Correct |
16 ms |
14720 KB |
Output is correct |
40 |
Correct |
16 ms |
14720 KB |
Output is correct |
41 |
Correct |
15 ms |
14720 KB |
Output is correct |
42 |
Correct |
15 ms |
14720 KB |
Output is correct |
43 |
Correct |
15 ms |
14720 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14592 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14592 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14592 KB |
Output is correct |
23 |
Correct |
15 ms |
14720 KB |
Output is correct |
24 |
Correct |
16 ms |
14720 KB |
Output is correct |
25 |
Correct |
16 ms |
14720 KB |
Output is correct |
26 |
Correct |
16 ms |
14720 KB |
Output is correct |
27 |
Correct |
16 ms |
14720 KB |
Output is correct |
28 |
Correct |
16 ms |
14720 KB |
Output is correct |
29 |
Correct |
16 ms |
14720 KB |
Output is correct |
30 |
Correct |
16 ms |
14720 KB |
Output is correct |
31 |
Correct |
17 ms |
14720 KB |
Output is correct |
32 |
Correct |
16 ms |
14720 KB |
Output is correct |
33 |
Correct |
16 ms |
14720 KB |
Output is correct |
34 |
Correct |
19 ms |
14720 KB |
Output is correct |
35 |
Correct |
16 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14720 KB |
Output is correct |
37 |
Correct |
16 ms |
14720 KB |
Output is correct |
38 |
Correct |
16 ms |
14720 KB |
Output is correct |
39 |
Correct |
16 ms |
14720 KB |
Output is correct |
40 |
Correct |
16 ms |
14720 KB |
Output is correct |
41 |
Correct |
15 ms |
14720 KB |
Output is correct |
42 |
Correct |
15 ms |
14720 KB |
Output is correct |
43 |
Correct |
15 ms |
14720 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
45 |
Correct |
593 ms |
44412 KB |
Output is correct |
46 |
Correct |
592 ms |
43896 KB |
Output is correct |
47 |
Correct |
599 ms |
44152 KB |
Output is correct |
48 |
Correct |
597 ms |
44152 KB |
Output is correct |
49 |
Correct |
573 ms |
44028 KB |
Output is correct |
50 |
Correct |
580 ms |
43768 KB |
Output is correct |
51 |
Correct |
580 ms |
44024 KB |
Output is correct |
52 |
Correct |
599 ms |
44280 KB |
Output is correct |
53 |
Correct |
591 ms |
44024 KB |
Output is correct |
54 |
Correct |
521 ms |
46840 KB |
Output is correct |
55 |
Correct |
556 ms |
46528 KB |
Output is correct |
56 |
Correct |
515 ms |
46328 KB |
Output is correct |
57 |
Correct |
516 ms |
46200 KB |
Output is correct |
58 |
Correct |
527 ms |
41208 KB |
Output is correct |
59 |
Correct |
482 ms |
41080 KB |
Output is correct |
60 |
Correct |
390 ms |
46200 KB |
Output is correct |
61 |
Correct |
456 ms |
41168 KB |
Output is correct |
62 |
Correct |
540 ms |
45292 KB |
Output is correct |
63 |
Correct |
455 ms |
40860 KB |
Output is correct |
64 |
Correct |
438 ms |
40704 KB |
Output is correct |
65 |
Correct |
557 ms |
45236 KB |
Output is correct |
66 |
Correct |
439 ms |
40912 KB |
Output is correct |