#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int p[N];
int l[N], r[N];
int el[N], er[N];
int get(int a) {
return (a == p[a] ? a : p[a] = get(p[a]));
}
void join(int a, int b) {
a = get(a);
b = get(b);
p[a] = b;
l[b] = min(l[b], l[a]);
r[b] = max(r[b], r[a]);
}
int vert_l[N], vert_r[N];
vector <int> gs[N];
vector <int> g[N];
ll cost[N];
struct Fenwick {
vector <ll> f;
int n;
Fenwick(int _n) {
n = _n;
f.resize(n);
}
void modify(int i, ll val) {
for (; i < n; i |= i + 1) {
f[i] += val;
}
}
void update(int l, int r, ll val) {
modify(l, val);
if (r + 1 < n) modify(r + 1, -val);
}
ll get(int r) {
ll res = 0;
for (; r >= 0; r &= r + 1, --r) {
res += f[r];
}
return res;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector <int> a(n);
vector <vector <int>> nc(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i] = n - a[i] - 1;
if (a[i] != -1) {
nc[a[i]].push_back(i);
}
}
int m;
cin >> m;
vector <int> x(m), y(m);
vector <vector <pair <int, int>>> who(n);
vector <int> c(m);
for (int i = 0; i < m; ++i) {
cin >> y[i] >> x[i] >> c[i];
--y[i];
x[i] = n - x[i];
who[x[i]].push_back({y[i], i});
}
set <pair <int, int>> seg;
map <pair <int, int>, int> num;
int uk = 1;
vector <int> have(n);
for (int i = n - 1; i >= 0; --i) {
for (auto j : nc[i]) {
p[j] = j;
l[j] = j;
r[j] = j;
have[j] = 1;
if (j > 0 && have[j - 1]) {
join(j, j - 1);
}
if (j + 1 < n && have[j + 1]) {
join(j, j + 1);
}
}
set <pair <int, int>> cur;
for (auto pr : who[i]) {
int id = pr.second;
int yc = pr.first;
int cl = l[get(yc)];
int cr = r[get(yc)];
if (cur.count({cl, cr})) {
gs[num[{cl, cr}]].push_back(id);
} else {
cur.insert({cl, cr});
while (true) {
auto it = seg.lower_bound(make_pair(cl, -1));
if (it != seg.end() && it->second <= cr) {
g[uk].push_back(num[*it]);
seg.erase(it);
} else {
break;
}
}
vert_l[uk] = cl;
vert_r[uk] = cr;
gs[uk].push_back(id);
num[{cl, cr}] = uk++;
}
}
for (auto p : cur) {
seg.insert(p);
}
}
vector <bool> rt(uk, true);
for (int i = 1; i < uk; ++i) {
for (int j : g[i]) {
rt[j] = false;
}
}
for (int i = 1; i < uk; ++i) {
if (rt[i]) {
g[0].push_back(i);
}
}
vert_l[0] = 0, vert_r[0] = n - 1;
vector <int> leaf(n);
set <int> free;
for (int i = 0; i < n; ++i) free.insert(i);
vector <int> par(uk);
vector <int> e;
function <void(int, int)> dfs = [&] (int u, int p) {
e.push_back(u);
el[u] = (int)e.size() - 1;
par[u] = p;
for (auto v : g[u]) {
dfs(v, u);
}
while (true) {
auto it = free.lower_bound(vert_l[u]);
if (it != free.end() && (*it) <= vert_r[u]) {
leaf[*it] = u;
free.erase(it);
} else {
break;
}
}
er[u] = (int)e.size() - 1;
};
dfs(0, -1);
for (int i = 0; i < uk; ++i) {
for (int id : gs[i]) {
cost[i] += c[id];
}
}
vector <vector <ll>> dp(uk, vector <ll> (2));
vector <ll> sum(uk);
Fenwick f1((int)e.size()), f2((int)e.size());
auto get_vert = [&] (Fenwick &f, int v) {
if (v == -1) return 0LL;
return f.get(el[v]);
};
function <void(int)> zhfs = [&] (int u) {
for (auto v : g[u]) {
zhfs(v);
sum[u] += min(dp[v][0], dp[v][1]);
}
dp[u][0] = sum[u] + cost[u];
dp[u][1] = dp[u][0];
f1.update(el[u], er[u], sum[u] + cost[u]);
for (int id : gs[u]) {
int l = leaf[y[id]];
ll total = -c[id] + get_vert(f1, l) - get_vert(f1, par[u]) + get_vert(f2, l) - get_vert(f2, u);
dp[u][1] = min(dp[u][1], total);
}
f2.update(el[u], er[u], -min(dp[u][0], dp[u][1]));
};
zhfs(0);
cout << dp[0][0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9856 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 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 |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9856 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 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 |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9856 KB |
Output is correct |
23 |
Correct |
13 ms |
10368 KB |
Output is correct |
24 |
Correct |
14 ms |
10368 KB |
Output is correct |
25 |
Correct |
13 ms |
10496 KB |
Output is correct |
26 |
Correct |
14 ms |
10496 KB |
Output is correct |
27 |
Correct |
14 ms |
10496 KB |
Output is correct |
28 |
Correct |
13 ms |
10488 KB |
Output is correct |
29 |
Correct |
13 ms |
10368 KB |
Output is correct |
30 |
Correct |
15 ms |
10496 KB |
Output is correct |
31 |
Correct |
14 ms |
10368 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 |
10368 KB |
Output is correct |
35 |
Correct |
12 ms |
10368 KB |
Output is correct |
36 |
Correct |
12 ms |
10368 KB |
Output is correct |
37 |
Correct |
12 ms |
10368 KB |
Output is correct |
38 |
Correct |
13 ms |
10496 KB |
Output is correct |
39 |
Correct |
13 ms |
10112 KB |
Output is correct |
40 |
Correct |
14 ms |
10360 KB |
Output is correct |
41 |
Correct |
12 ms |
10112 KB |
Output is correct |
42 |
Correct |
12 ms |
10112 KB |
Output is correct |
43 |
Correct |
12 ms |
10368 KB |
Output is correct |
44 |
Correct |
12 ms |
10112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9856 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 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 |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9856 KB |
Output is correct |
18 |
Correct |
10 ms |
9856 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 |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9856 KB |
Output is correct |
23 |
Correct |
13 ms |
10368 KB |
Output is correct |
24 |
Correct |
14 ms |
10368 KB |
Output is correct |
25 |
Correct |
13 ms |
10496 KB |
Output is correct |
26 |
Correct |
14 ms |
10496 KB |
Output is correct |
27 |
Correct |
14 ms |
10496 KB |
Output is correct |
28 |
Correct |
13 ms |
10488 KB |
Output is correct |
29 |
Correct |
13 ms |
10368 KB |
Output is correct |
30 |
Correct |
15 ms |
10496 KB |
Output is correct |
31 |
Correct |
14 ms |
10368 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 |
10368 KB |
Output is correct |
35 |
Correct |
12 ms |
10368 KB |
Output is correct |
36 |
Correct |
12 ms |
10368 KB |
Output is correct |
37 |
Correct |
12 ms |
10368 KB |
Output is correct |
38 |
Correct |
13 ms |
10496 KB |
Output is correct |
39 |
Correct |
13 ms |
10112 KB |
Output is correct |
40 |
Correct |
14 ms |
10360 KB |
Output is correct |
41 |
Correct |
12 ms |
10112 KB |
Output is correct |
42 |
Correct |
12 ms |
10112 KB |
Output is correct |
43 |
Correct |
12 ms |
10368 KB |
Output is correct |
44 |
Correct |
12 ms |
10112 KB |
Output is correct |
45 |
Correct |
886 ms |
72952 KB |
Output is correct |
46 |
Correct |
838 ms |
71672 KB |
Output is correct |
47 |
Correct |
903 ms |
72988 KB |
Output is correct |
48 |
Correct |
849 ms |
71928 KB |
Output is correct |
49 |
Correct |
829 ms |
71676 KB |
Output is correct |
50 |
Correct |
820 ms |
71032 KB |
Output is correct |
51 |
Correct |
844 ms |
72184 KB |
Output is correct |
52 |
Correct |
843 ms |
73464 KB |
Output is correct |
53 |
Correct |
823 ms |
71800 KB |
Output is correct |
54 |
Correct |
362 ms |
72308 KB |
Output is correct |
55 |
Correct |
387 ms |
76016 KB |
Output is correct |
56 |
Correct |
361 ms |
74612 KB |
Output is correct |
57 |
Correct |
372 ms |
73964 KB |
Output is correct |
58 |
Correct |
298 ms |
65904 KB |
Output is correct |
59 |
Correct |
300 ms |
66032 KB |
Output is correct |
60 |
Correct |
343 ms |
77948 KB |
Output is correct |
61 |
Correct |
346 ms |
49272 KB |
Output is correct |
62 |
Correct |
355 ms |
69652 KB |
Output is correct |
63 |
Correct |
312 ms |
48356 KB |
Output is correct |
64 |
Correct |
331 ms |
48120 KB |
Output is correct |
65 |
Correct |
338 ms |
69620 KB |
Output is correct |
66 |
Correct |
317 ms |
47604 KB |
Output is correct |