#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll=long long;
using pp=pair<int,int>;
const int maxn = int(2e5) + 10;
int n;
int a[maxn];
map<pp,int> gnum; int gn;
struct Sparse {
pp tx[20][maxn];
void build() {
for (int i=1; i<=n; ++i) tx[0][i] = {a[i], i};
for (int i=1; i<=19; ++i) for (int j=1; j<=n; ++j) {
tx[i][j] = tx[i-1][j];
if (j+(1<<(i-1)) <= n) {
tx[i][j] = max(tx[i][j], tx[i-1][j+(1<<(i-1))]);
}
}
}
int maxi(int l, int r) {
int w = r-l+1;
for (int i=0;;++i) if (2*(1<<i) >= w) {
return max(tx[i][l], tx[i][r-(1<<i)+1]).second;
}
}
int wrap_v(int x, int y) {
int l = x, r = x;
for (int i=19; 0<=i; --i) {
int tl = l - (1<<i);
if (1 <= tl && tx[i][tl].first < y) l = tl;
int tr = r + (1<<i);
if (tr <= n && tx[i][r+1].first < y) r = tr;
}
return gnum[{l, r}];
}
} tree;
vector<int> te[maxn];
int root;
vector<pp> paths[maxn];
int gdfs(int l, int r) {
int me = ++gn; gnum[{l, r}] = me;
if (l == r) return me;
int i = tree.maxi(l, r);
if (l < i) te[me].push_back(gdfs(l, i-1));
if (i < r) te[me].push_back(gdfs(i+1, r));
return me;
}
int tin[maxn], tout[maxn], nt;
void tdfs1(int x) {
tin[x] = ++nt; for (int y : te[x]) tdfs1(y); tout[x] = nt;
}
ll dp[maxn];
struct It {
const static int M = 262144;
ll t[M<<1];
void upd(int l, int r, ll v) {
for (l+=M, r+=M; l<=r; l/=2, r/=2) {
if (l%2==1) t[l++] += v;
if (r%2==0) t[r--] += v;
}
}
ll get(int p) { ll ret = 0;
for (p+=M; p; p/=2) ret += t[p];
return ret;
}
} giveup;
void tdfs2(int x) {
ll cds = 0;
for (int y:te[x]) tdfs2(y), cds += dp[y];
ll mdf = 0;
for (auto &tmp:paths[x]) {
int to, c; tie(to, c) = tmp;
mdf = max(mdf, c + giveup.get(tin[to]));
}
dp[x] = cds + mdf;
giveup.upd(tin[x], tout[x], -mdf);
}
int main() { cin.tie(0)->sync_with_stdio(0);
cin >> n; for (int i=1; i<=n; ++i) cin >> a[i];
tree.build();
ll csum = 0;
root = gdfs(1, n);
{ int m; cin >> m;
for (;m--;) {
int x, y, c; cin >> x >> y >> c; csum += c;
int up = tree.wrap_v(x, y), down = tree.wrap_v(x, a[x]+1);
paths[up].push_back({down, c});
} }
tdfs1(root);
tdfs2(root);
ll ans = csum - dp[root];
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9932 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
6 ms |
9996 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Correct |
6 ms |
9932 KB |
Output is correct |
6 |
Correct |
6 ms |
9932 KB |
Output is correct |
7 |
Correct |
6 ms |
9932 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9932 KB |
Output is correct |
10 |
Correct |
6 ms |
9932 KB |
Output is correct |
11 |
Correct |
6 ms |
9932 KB |
Output is correct |
12 |
Correct |
6 ms |
9932 KB |
Output is correct |
13 |
Correct |
6 ms |
9932 KB |
Output is correct |
14 |
Correct |
6 ms |
9996 KB |
Output is correct |
15 |
Correct |
6 ms |
9932 KB |
Output is correct |
16 |
Correct |
6 ms |
10004 KB |
Output is correct |
17 |
Correct |
6 ms |
9932 KB |
Output is correct |
18 |
Correct |
6 ms |
9988 KB |
Output is correct |
19 |
Correct |
6 ms |
9992 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9932 KB |
Output is correct |
22 |
Correct |
6 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9932 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
6 ms |
9996 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Correct |
6 ms |
9932 KB |
Output is correct |
6 |
Correct |
6 ms |
9932 KB |
Output is correct |
7 |
Correct |
6 ms |
9932 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9932 KB |
Output is correct |
10 |
Correct |
6 ms |
9932 KB |
Output is correct |
11 |
Correct |
6 ms |
9932 KB |
Output is correct |
12 |
Correct |
6 ms |
9932 KB |
Output is correct |
13 |
Correct |
6 ms |
9932 KB |
Output is correct |
14 |
Correct |
6 ms |
9996 KB |
Output is correct |
15 |
Correct |
6 ms |
9932 KB |
Output is correct |
16 |
Correct |
6 ms |
10004 KB |
Output is correct |
17 |
Correct |
6 ms |
9932 KB |
Output is correct |
18 |
Correct |
6 ms |
9988 KB |
Output is correct |
19 |
Correct |
6 ms |
9992 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9932 KB |
Output is correct |
22 |
Correct |
6 ms |
9932 KB |
Output is correct |
23 |
Correct |
8 ms |
10484 KB |
Output is correct |
24 |
Correct |
9 ms |
10528 KB |
Output is correct |
25 |
Correct |
8 ms |
10512 KB |
Output is correct |
26 |
Correct |
9 ms |
10444 KB |
Output is correct |
27 |
Correct |
9 ms |
10444 KB |
Output is correct |
28 |
Correct |
9 ms |
10432 KB |
Output is correct |
29 |
Correct |
9 ms |
10424 KB |
Output is correct |
30 |
Correct |
9 ms |
10444 KB |
Output is correct |
31 |
Correct |
8 ms |
10512 KB |
Output is correct |
32 |
Correct |
8 ms |
10620 KB |
Output is correct |
33 |
Correct |
8 ms |
10508 KB |
Output is correct |
34 |
Correct |
8 ms |
10516 KB |
Output is correct |
35 |
Correct |
9 ms |
10572 KB |
Output is correct |
36 |
Correct |
8 ms |
10572 KB |
Output is correct |
37 |
Correct |
9 ms |
10664 KB |
Output is correct |
38 |
Correct |
8 ms |
10644 KB |
Output is correct |
39 |
Correct |
8 ms |
10444 KB |
Output is correct |
40 |
Correct |
8 ms |
10572 KB |
Output is correct |
41 |
Correct |
10 ms |
10512 KB |
Output is correct |
42 |
Correct |
8 ms |
10468 KB |
Output is correct |
43 |
Correct |
9 ms |
10572 KB |
Output is correct |
44 |
Correct |
9 ms |
10444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9932 KB |
Output is correct |
2 |
Correct |
6 ms |
9932 KB |
Output is correct |
3 |
Correct |
6 ms |
9996 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Correct |
6 ms |
9932 KB |
Output is correct |
6 |
Correct |
6 ms |
9932 KB |
Output is correct |
7 |
Correct |
6 ms |
9932 KB |
Output is correct |
8 |
Correct |
6 ms |
9932 KB |
Output is correct |
9 |
Correct |
6 ms |
9932 KB |
Output is correct |
10 |
Correct |
6 ms |
9932 KB |
Output is correct |
11 |
Correct |
6 ms |
9932 KB |
Output is correct |
12 |
Correct |
6 ms |
9932 KB |
Output is correct |
13 |
Correct |
6 ms |
9932 KB |
Output is correct |
14 |
Correct |
6 ms |
9996 KB |
Output is correct |
15 |
Correct |
6 ms |
9932 KB |
Output is correct |
16 |
Correct |
6 ms |
10004 KB |
Output is correct |
17 |
Correct |
6 ms |
9932 KB |
Output is correct |
18 |
Correct |
6 ms |
9988 KB |
Output is correct |
19 |
Correct |
6 ms |
9992 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
6 ms |
9932 KB |
Output is correct |
22 |
Correct |
6 ms |
9932 KB |
Output is correct |
23 |
Correct |
8 ms |
10484 KB |
Output is correct |
24 |
Correct |
9 ms |
10528 KB |
Output is correct |
25 |
Correct |
8 ms |
10512 KB |
Output is correct |
26 |
Correct |
9 ms |
10444 KB |
Output is correct |
27 |
Correct |
9 ms |
10444 KB |
Output is correct |
28 |
Correct |
9 ms |
10432 KB |
Output is correct |
29 |
Correct |
9 ms |
10424 KB |
Output is correct |
30 |
Correct |
9 ms |
10444 KB |
Output is correct |
31 |
Correct |
8 ms |
10512 KB |
Output is correct |
32 |
Correct |
8 ms |
10620 KB |
Output is correct |
33 |
Correct |
8 ms |
10508 KB |
Output is correct |
34 |
Correct |
8 ms |
10516 KB |
Output is correct |
35 |
Correct |
9 ms |
10572 KB |
Output is correct |
36 |
Correct |
8 ms |
10572 KB |
Output is correct |
37 |
Correct |
9 ms |
10664 KB |
Output is correct |
38 |
Correct |
8 ms |
10644 KB |
Output is correct |
39 |
Correct |
8 ms |
10444 KB |
Output is correct |
40 |
Correct |
8 ms |
10572 KB |
Output is correct |
41 |
Correct |
10 ms |
10512 KB |
Output is correct |
42 |
Correct |
8 ms |
10468 KB |
Output is correct |
43 |
Correct |
9 ms |
10572 KB |
Output is correct |
44 |
Correct |
9 ms |
10444 KB |
Output is correct |
45 |
Correct |
892 ms |
73532 KB |
Output is correct |
46 |
Correct |
850 ms |
72988 KB |
Output is correct |
47 |
Correct |
911 ms |
72132 KB |
Output is correct |
48 |
Correct |
875 ms |
73724 KB |
Output is correct |
49 |
Correct |
842 ms |
71684 KB |
Output is correct |
50 |
Correct |
833 ms |
71628 KB |
Output is correct |
51 |
Correct |
870 ms |
71744 KB |
Output is correct |
52 |
Correct |
864 ms |
73196 KB |
Output is correct |
53 |
Correct |
855 ms |
73112 KB |
Output is correct |
54 |
Correct |
1218 ms |
83784 KB |
Output is correct |
55 |
Correct |
1193 ms |
79572 KB |
Output is correct |
56 |
Correct |
1278 ms |
77116 KB |
Output is correct |
57 |
Correct |
1185 ms |
75760 KB |
Output is correct |
58 |
Correct |
930 ms |
79636 KB |
Output is correct |
59 |
Correct |
972 ms |
79428 KB |
Output is correct |
60 |
Correct |
324 ms |
90132 KB |
Output is correct |
61 |
Correct |
962 ms |
73172 KB |
Output is correct |
62 |
Correct |
1199 ms |
84432 KB |
Output is correct |
63 |
Correct |
932 ms |
71868 KB |
Output is correct |
64 |
Correct |
903 ms |
70952 KB |
Output is correct |
65 |
Correct |
1197 ms |
84968 KB |
Output is correct |
66 |
Correct |
909 ms |
71236 KB |
Output is correct |