#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll
const int N = 2e5 + 10;
int p[N], l[N], r[N], sz[N];
void init(int n) {
for (int i = 1; i <= n; i++) {
p[i] = l[i] = r[i] = i;
sz[i] = 1;
}
}
int find(int v) {
return p[v] == v ? v : p[v] = find(p[v]);
}
void merge(int a, int b) {
a = find(a), b = find(b);
if (a == b) {
return;
}
if (sz[a] < sz[b]) {
swap(a, b);
}
sz[a] += sz[b];
p[b] = a;
l[a] = min(l[a], l[b]);
r[a] = max(r[a], r[b]);
}
int t[4 * N], f[4 * N];
void push(int v) {
t[2 * v] += f[v];
t[2 * v + 1] += f[v];
f[2 * v] += f[v];
f[2 * v + 1] += f[v];
f[v] = 0;
}
void update(int v, int l, int r, int a, int b, int delta) {
if (l > b || r < a) {
return;
}
if (l >= a && r <= b) {
t[v] += delta;
f[v] += delta;
return;
}
push(v);
int mid = (l + r) / 2;
update(2 * v, l, mid, a, b, delta);
update(2 * v + 1, mid + 1, r, a, b, delta);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
int get_max(int v, int l, int r, int a, int b) {
if (l > b || r < a) {
return 0;
}
if (l >= a && r <= b) {
return t[v];
}
push(v);
int mid = (l + r) / 2;
return max(get_max(2 * v, l, mid, a, b), get_max(2 * v + 1, mid + 1, r, a, b));
}
vector<int> add[N];
vector<pair<int, int>> have[N], kek[N];
bool active[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
add[x + 1].pb(i);
}
int m;
cin >> m;
int all = 0;
for (int i = 1; i <= m; i++) {
int x, y, c;
cin >> x >> y >> c;
all += c;
have[x].pb({y, c});
}
for (int i = 1; i <= n; i++) {
sort(all(have[i]));
int prv = 0;
for (auto it : have[i]) {
if (it.S > prv) {
kek[it.F].pb({i, it.S - prv});
prv = it.S;
}
}
}
init(n);
for (int i = 1; i <= n; i++) {
for (int x : add[i]) {
int mx_l = get_max(1, 1, n, l[find(x - 1)], r[find(x - 1)]),
mx_r = get_max(1, 1, n, l[find(x + 1)], r[find(x + 1)]);
if (active[x - 1]) {
update(1, 1, n, l[find(x - 1)], r[find(x - 1)], mx_r);
merge(x - 1, x);
}
if (active[x + 1]) {
update(1, 1, n, l[find(x + 1)], r[find(x + 1)], mx_l);
merge(x, x + 1);
}
update(1, 1, n, x, x, mx_l + mx_r);
active[x] = true;
}
for (auto it : kek[i]) {
update(1, 1, n, it.F, it.F, it.S);
}
}
int sum = 0;
for (int i = 1; i <= n; i++) {
if (find(i) == i) {
sum += get_max(1, 1, n, l[i], r[i]);
}
}
cout << all - sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 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 |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
16 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
16 ms |
14464 KB |
Output is correct |
15 |
Correct |
22 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
18 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14592 KB |
Output is correct |
22 |
Correct |
22 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 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 |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
16 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
16 ms |
14464 KB |
Output is correct |
15 |
Correct |
22 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
18 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14592 KB |
Output is correct |
22 |
Correct |
22 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14720 KB |
Output is correct |
24 |
Correct |
16 ms |
14728 KB |
Output is correct |
25 |
Correct |
15 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 |
14848 KB |
Output is correct |
31 |
Correct |
16 ms |
14720 KB |
Output is correct |
32 |
Correct |
15 ms |
14772 KB |
Output is correct |
33 |
Correct |
15 ms |
14848 KB |
Output is correct |
34 |
Correct |
16 ms |
14848 KB |
Output is correct |
35 |
Correct |
15 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14720 KB |
Output is correct |
37 |
Correct |
16 ms |
14768 KB |
Output is correct |
38 |
Correct |
26 ms |
14848 KB |
Output is correct |
39 |
Correct |
15 ms |
14720 KB |
Output is correct |
40 |
Correct |
15 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 |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
16 ms |
14724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 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 |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14592 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14592 KB |
Output is correct |
11 |
Correct |
16 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
16 ms |
14464 KB |
Output is correct |
15 |
Correct |
22 ms |
14592 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
18 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14592 KB |
Output is correct |
22 |
Correct |
22 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14720 KB |
Output is correct |
24 |
Correct |
16 ms |
14728 KB |
Output is correct |
25 |
Correct |
15 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 |
14848 KB |
Output is correct |
31 |
Correct |
16 ms |
14720 KB |
Output is correct |
32 |
Correct |
15 ms |
14772 KB |
Output is correct |
33 |
Correct |
15 ms |
14848 KB |
Output is correct |
34 |
Correct |
16 ms |
14848 KB |
Output is correct |
35 |
Correct |
15 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14720 KB |
Output is correct |
37 |
Correct |
16 ms |
14768 KB |
Output is correct |
38 |
Correct |
26 ms |
14848 KB |
Output is correct |
39 |
Correct |
15 ms |
14720 KB |
Output is correct |
40 |
Correct |
15 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 |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
16 ms |
14724 KB |
Output is correct |
45 |
Correct |
636 ms |
48120 KB |
Output is correct |
46 |
Correct |
639 ms |
47864 KB |
Output is correct |
47 |
Correct |
636 ms |
48120 KB |
Output is correct |
48 |
Correct |
688 ms |
47992 KB |
Output is correct |
49 |
Correct |
621 ms |
47736 KB |
Output is correct |
50 |
Correct |
582 ms |
47608 KB |
Output is correct |
51 |
Correct |
625 ms |
47736 KB |
Output is correct |
52 |
Correct |
649 ms |
48248 KB |
Output is correct |
53 |
Correct |
637 ms |
47984 KB |
Output is correct |
54 |
Correct |
508 ms |
50424 KB |
Output is correct |
55 |
Correct |
511 ms |
50168 KB |
Output is correct |
56 |
Correct |
522 ms |
49912 KB |
Output is correct |
57 |
Correct |
491 ms |
49784 KB |
Output is correct |
58 |
Correct |
471 ms |
45560 KB |
Output is correct |
59 |
Correct |
468 ms |
45688 KB |
Output is correct |
60 |
Correct |
335 ms |
49272 KB |
Output is correct |
61 |
Correct |
439 ms |
45672 KB |
Output is correct |
62 |
Correct |
532 ms |
48760 KB |
Output is correct |
63 |
Correct |
433 ms |
45524 KB |
Output is correct |
64 |
Correct |
432 ms |
45296 KB |
Output is correct |
65 |
Correct |
547 ms |
48524 KB |
Output is correct |
66 |
Correct |
440 ms |
45284 KB |
Output is correct |