/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
const int M_MAX = 200000;
const int LOG_N = 18;
int N;
int A[N_MAX + 2];
int M;
struct Star {
int x, y;
int cost;
};
Star S[M_MAX + 2];
int L[N_MAX + 2], R[N_MAX + 2];
int st[N_MAX + 2], cnt;
void get_LR () {
cnt = 0; st[0] = 0;
for (int x = 1; x <= N; x++) {
while (cnt > 0 && A[x] >= A[st[cnt]]) {
cnt--;
}
L[x] = st[cnt] + 1;
st[++cnt] = x;
}
cnt = 0; st[0] = N + 1;
for (int x = N; x >= 1; x--) {
while (cnt > 0 && A[x] >= A[st[cnt]]) {
cnt--;
}
R[x] = st[cnt] - 1;
st[++cnt] = x;
}
}
int node[N_MAX + 2];
int parent[N_MAX + 2];
vector <int> sons[N_MAX + 2];
int anc[N_MAX + 2][LOG_N];
int root;
vector <int> stars[N_MAX + 2];
void get_tree () {
iota(node + 1, node + N + 1, 1);
for (int x = 1; x <= N; x++) {
int l = L[x] - 1, r = R[x] + 1;
if (1 <= l && r <= N && A[l] == A[r]) {
node[r] = node[l];
}
if (A[x] == A[x - 1]) {
node[x] = node[x - 1];
}
}
for (int x = 1; x <= N; x++) {
int l = L[x] - 1, r = R[x] + 1;
parent[node[x]] = (A[l] <= A[r] ? node[l] : node[r]);
}
for (int u = 1; u <= N; u++) {
if (node[u] == u) {
sons[parent[u]].push_back(u);
anc[u][0] = parent[u];
if (parent[u] == 0) {
root = u;
}
}
}
for (int k = 1; k < LOG_N; k++) {
for (int u = 1; u <= N; u++) {
anc[u][k] = anc[anc[u][k - 1]][k - 1];
}
}
for (int i = 1; i <= M; i++) {
int u = node[S[i].x];
for (int k = LOG_N - 1; k >= 0; k--) {
if (A[anc[u][k]] < S[i].y) {
u = anc[u][k];
}
}
stars[u].push_back(i);
}
}
int eul[N_MAX + 2], eur[N_MAX + 2];
int curr;
void dfs (int u) {
eul[u] = ++curr;
for (int v : sons[u]) {
dfs(v);
}
eur[u] = curr;
}
ll max_keep[N_MAX + 2];
ll sum_up[N_MAX + 2];
ll sum_sons_up[N_MAX + 2];
void update (ll Fen[], int pos, ll val) {
for (int i = pos; i <= N; i += i & -i) {
Fen[i] += val;
}
}
void update (ll Fen[], int l, int r, ll val) {
update(Fen, l, val);
update(Fen, r + 1, -val);
}
ll query (ll Fen[], int pos) {
ll sum = 0;
for (int i = pos; i >= 1; i -= i & -i) {
sum += Fen[i];
}
return sum;
}
ll chain_sum (int u) {
return query(sum_sons_up, eul[u]) - query(sum_up, eul[u]);
}
void solve (int u) {
ll sum_sons = 0;
for (int v : sons[u]) {
solve(v);
sum_sons += max_keep[v];
}
update(sum_sons_up, eul[u], eur[u], sum_sons);
max_keep[u] = sum_sons;
for (int i : stars[u]) {
max_keep[u] = max(max_keep[u], S[i].cost + chain_sum(node[S[i].x]));
}
update(sum_up, eul[u], eur[u], max_keep[u]);
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int x = 1; x <= N; x++) {
cin >> A[x];
}
A[0] = A[N + 1] = N + 1;
cin >> M;
for (int i = 1; i <= M; i++) {
cin >> S[i].x >> S[i].y >> S[i].cost;
}
get_LR();
get_tree();
dfs(root);
solve(root);
ll answer = 0;
for (int i = 1; i <= M; i++) {
answer += S[i].cost;
}
answer -= max_keep[root];
cout << answer << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10124 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9844 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9896 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9908 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9832 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10124 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9844 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9896 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9908 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9832 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
23 |
Correct |
6 ms |
10076 KB |
Output is correct |
24 |
Correct |
5 ms |
10076 KB |
Output is correct |
25 |
Correct |
6 ms |
10304 KB |
Output is correct |
26 |
Correct |
9 ms |
10076 KB |
Output is correct |
27 |
Correct |
6 ms |
10076 KB |
Output is correct |
28 |
Correct |
5 ms |
10076 KB |
Output is correct |
29 |
Correct |
6 ms |
10076 KB |
Output is correct |
30 |
Correct |
6 ms |
10144 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
6 ms |
10400 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
6 ms |
10332 KB |
Output is correct |
35 |
Correct |
6 ms |
10332 KB |
Output is correct |
36 |
Correct |
5 ms |
10076 KB |
Output is correct |
37 |
Correct |
5 ms |
10076 KB |
Output is correct |
38 |
Correct |
6 ms |
10736 KB |
Output is correct |
39 |
Correct |
5 ms |
10080 KB |
Output is correct |
40 |
Correct |
6 ms |
10340 KB |
Output is correct |
41 |
Correct |
5 ms |
10080 KB |
Output is correct |
42 |
Correct |
5 ms |
10068 KB |
Output is correct |
43 |
Correct |
5 ms |
10332 KB |
Output is correct |
44 |
Correct |
7 ms |
10144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10076 KB |
Output is correct |
2 |
Correct |
4 ms |
10124 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
9844 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9896 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9908 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9832 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9820 KB |
Output is correct |
23 |
Correct |
6 ms |
10076 KB |
Output is correct |
24 |
Correct |
5 ms |
10076 KB |
Output is correct |
25 |
Correct |
6 ms |
10304 KB |
Output is correct |
26 |
Correct |
9 ms |
10076 KB |
Output is correct |
27 |
Correct |
6 ms |
10076 KB |
Output is correct |
28 |
Correct |
5 ms |
10076 KB |
Output is correct |
29 |
Correct |
6 ms |
10076 KB |
Output is correct |
30 |
Correct |
6 ms |
10144 KB |
Output is correct |
31 |
Correct |
6 ms |
10076 KB |
Output is correct |
32 |
Correct |
6 ms |
10400 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
6 ms |
10332 KB |
Output is correct |
35 |
Correct |
6 ms |
10332 KB |
Output is correct |
36 |
Correct |
5 ms |
10076 KB |
Output is correct |
37 |
Correct |
5 ms |
10076 KB |
Output is correct |
38 |
Correct |
6 ms |
10736 KB |
Output is correct |
39 |
Correct |
5 ms |
10080 KB |
Output is correct |
40 |
Correct |
6 ms |
10340 KB |
Output is correct |
41 |
Correct |
5 ms |
10080 KB |
Output is correct |
42 |
Correct |
5 ms |
10068 KB |
Output is correct |
43 |
Correct |
5 ms |
10332 KB |
Output is correct |
44 |
Correct |
7 ms |
10144 KB |
Output is correct |
45 |
Correct |
199 ms |
48912 KB |
Output is correct |
46 |
Correct |
200 ms |
48596 KB |
Output is correct |
47 |
Correct |
203 ms |
48464 KB |
Output is correct |
48 |
Correct |
198 ms |
48720 KB |
Output is correct |
49 |
Correct |
196 ms |
47764 KB |
Output is correct |
50 |
Correct |
204 ms |
48016 KB |
Output is correct |
51 |
Correct |
186 ms |
47952 KB |
Output is correct |
52 |
Correct |
207 ms |
48724 KB |
Output is correct |
53 |
Correct |
190 ms |
48640 KB |
Output is correct |
54 |
Correct |
268 ms |
59164 KB |
Output is correct |
55 |
Correct |
295 ms |
55332 KB |
Output is correct |
56 |
Correct |
282 ms |
53056 KB |
Output is correct |
57 |
Correct |
257 ms |
52124 KB |
Output is correct |
58 |
Correct |
135 ms |
39248 KB |
Output is correct |
59 |
Correct |
113 ms |
39304 KB |
Output is correct |
60 |
Correct |
122 ms |
66640 KB |
Output is correct |
61 |
Correct |
161 ms |
45124 KB |
Output is correct |
62 |
Correct |
284 ms |
57516 KB |
Output is correct |
63 |
Correct |
186 ms |
43232 KB |
Output is correct |
64 |
Correct |
156 ms |
43816 KB |
Output is correct |
65 |
Correct |
295 ms |
57936 KB |
Output is correct |
66 |
Correct |
166 ms |
43084 KB |
Output is correct |