#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, inf = 1e9 + 7;
int n, m;
int a[N], c[N], tin[N], tout[N];
long long ft[N], dp[N];
array<int, 2> s[4 * N], buc[4 * N];
vector<array<int, 2>> pt[N];
void bld1(int id = 1, int l = 1, int r = n) {
if (l == r) {
s[id] = {-a[l], l};
return;
}
int md = (l + r) / 2;
bld1(id * 2, l, md);
bld1(id * 2 + 1, md + 1, r);
s[id] = min(s[id * 2], s[id * 2 + 1]);
}
void bld2(int id = 1, int l = 1, int r = n) {
if (l == r) {
buc[id] = {pt[l].back()[0], l};
return;
}
int md = (l + r) / 2;
bld2(id * 2, l, md);
bld2(id * 2 + 1, md + 1, r);
buc[id] = min(buc[id * 2], buc[id * 2 + 1]);
}
array<int, 2> qry(int u, int v, array<int, 2> *tree, int id = 1, int l = 1, int r = n) {
if (u <= l && r <= v) {
return tree[id];
}
int md = (l + r) / 2;
if (v <= md) {
return qry(u, v, tree, id * 2, l, md);
}
if (md < u) {
return qry(u, v, tree, id * 2 + 1, md + 1, r);
}
return min(qry(u, v, tree, id * 2, l, md), qry(u, v, tree, id * 2 + 1, md + 1, r));
}
void pop(int i, int id = 1, int l = 1, int r = n) {
if (l == r) {
pt[l].pop_back();
assert(pt[l].size());
buc[id] = {pt[l].back()[0], l};
return;
}
int md = (l + r) / 2;
if (i <= md) {
pop(i, id * 2, l, md);
} else {
pop(i, id * 2 + 1, md + 1, r);
}
buc[id] = min(buc[id * 2], buc[id * 2 + 1]);
}
void up(int i, long long x) {
for (; i <= n; i += i & -i) {
ft[i] += x;
}
}
void up(int l, int r, long long x) {
up(l, x);
up(r + 1, -x);
}
long long qry(int i) {
long long res = 0;
for (; i; i -= i & -i) {
res += ft[i];
}
return res;
}
int timer;
int dfs(int l, int r, int p) {
int u = qry(l, r, s)[1];
tin[u] = ++timer;
long long sum = 0;
if (l < u) {
sum += dp[dfs(l, u - 1, a[u])];
}
if (u < r) {
sum += dp[dfs(u + 1, r, a[u])];
}
dp[u] = sum;
while (1) {
auto x = qry(l, r, buc);
if (x[0] > p) {
break;
}
int v = x[1], c = pt[x[1]].back()[1];
pop(x[1]);
dp[u] = max(dp[u], sum + qry(tin[v]) + c);
}
tout[u] = timer;
up(tin[u], tout[u], sum - dp[u]);
return u;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
bld1();
cin >> m;
long long tot = 0;
for (int i = 1; i <= m; ++i) {
int x, y; cin >> x >> y >> c[i];
pt[x].push_back({y, c[i]});
tot += c[i];
}
for (int i = 1; i <= n; ++i) {
pt[i].push_back({inf, 0});
sort(pt[i].rbegin(), pt[i].rend());
}
bld2();
cout << tot - dp[dfs(1, n, 1e9)];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12944 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12944 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
13044 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
2 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
13148 KB |
Output is correct |
37 |
Correct |
3 ms |
13144 KB |
Output is correct |
38 |
Correct |
2 ms |
13148 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
13148 KB |
Output is correct |
41 |
Correct |
3 ms |
14940 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
3 ms |
13188 KB |
Output is correct |
44 |
Correct |
3 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12944 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12888 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
13044 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
2 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
13148 KB |
Output is correct |
37 |
Correct |
3 ms |
13144 KB |
Output is correct |
38 |
Correct |
2 ms |
13148 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
13148 KB |
Output is correct |
41 |
Correct |
3 ms |
14940 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
3 ms |
13188 KB |
Output is correct |
44 |
Correct |
3 ms |
12888 KB |
Output is correct |
45 |
Correct |
154 ms |
31920 KB |
Output is correct |
46 |
Correct |
146 ms |
32848 KB |
Output is correct |
47 |
Correct |
137 ms |
32592 KB |
Output is correct |
48 |
Correct |
137 ms |
32592 KB |
Output is correct |
49 |
Correct |
141 ms |
32340 KB |
Output is correct |
50 |
Correct |
139 ms |
32336 KB |
Output is correct |
51 |
Correct |
138 ms |
32336 KB |
Output is correct |
52 |
Correct |
140 ms |
32720 KB |
Output is correct |
53 |
Correct |
152 ms |
32672 KB |
Output is correct |
54 |
Correct |
223 ms |
45140 KB |
Output is correct |
55 |
Correct |
207 ms |
41828 KB |
Output is correct |
56 |
Correct |
207 ms |
39508 KB |
Output is correct |
57 |
Correct |
204 ms |
38448 KB |
Output is correct |
58 |
Correct |
192 ms |
41024 KB |
Output is correct |
59 |
Correct |
185 ms |
41132 KB |
Output is correct |
60 |
Correct |
135 ms |
51284 KB |
Output is correct |
61 |
Correct |
147 ms |
32140 KB |
Output is correct |
62 |
Correct |
228 ms |
46928 KB |
Output is correct |
63 |
Correct |
145 ms |
32148 KB |
Output is correct |
64 |
Correct |
152 ms |
33216 KB |
Output is correct |
65 |
Correct |
219 ms |
47188 KB |
Output is correct |
66 |
Correct |
178 ms |
32216 KB |
Output is correct |