#include <iostream>
#include <array>
#include <vector>
#define ll long long
using namespace std;
vector <array<ll, 2>> V[200002];
vector <ll> H[200002];
vector <ll> U;
array<ll, 2> R[200002][2];
vector <ll> adj[200002][2];
ll n, m, l, r, mid, f, s, z, mx, opt, cnt, A[200002], bl[200002][19], br[200002][19], x, y, c;
struct SegTree_MAX{
ll st[800008];
void update(ll id, ll l, ll r, ll q, ll w) {
if (l == r) {
st[id] = max(st[id], w);
return;
}
ll mid = (l+r)/2;
if (q <= mid) update(id*2, l, mid, q, w);
else update(id*2+1, mid+1, r, q, w);
st[id] = max(st[id*2], st[id*2+1]);
}
ll query(ll id, ll l, ll r, ll ql, ll qr) {
if (qr < l || r < ql) return 0;
else if (ql <= l && r <= qr) return st[id];
ll mid = (l+r)/2;
return max(query(id*2, l, mid, ql, qr), query(id*2+1, mid+1, r, ql, qr));
}
}ST;
struct SegTree_ADD{
ll st[800008], lazy[800008];
void push(ll id) {
st[id*2] += lazy[id];
lazy[id*2] += lazy[id];
st[id*2+1] += lazy[id];
lazy[id*2+1] += lazy[id];
lazy[id] = 0;
}
void update(ll id, ll l, ll r, ll ql, ll qr, ll w) {
if (qr < l || r < ql) return;
else if (ql <= l && r <= qr) {
st[id] += w;
lazy[id] += w;
return;
}
push(id);
ll mid = (l+r)/2;
update(id*2, l, mid, ql, qr, w);
update(id*2+1, mid+1, r, ql, qr, w);
}
ll query(ll id, ll l, ll r, ll q) {
if (l == r) return st[id];
push(id);
ll mid = (l+r)/2;
if (q <= mid) return query(id*2, l, mid, q);
else return query(id*2+1, mid+1, r, q);
}
}tree[2];
void dfs(ll id, ll u) {
R[u][id][0] = cnt;
for (auto v : adj[u][id]) {
dfs(id, v);
}
R[u][id][1] = cnt++;
}
int main() {
cin >> n;
A[0] = 1e18;
for (int i=1; i<=n; ++i) {
cin >> A[i];
if (A[i] > mx) {
mx = A[i];
z = i;
}
--A[i];
}
A[n+1] = 1e18;
cin >> m;
for (int i=0; i<m; ++i) {
cin >> x >> y >> c;
--y;
f += c;
V[y].push_back({x, c});
}
V[n].push_back({z, 0});
for (int i=0; i<=n+1; ++i) {
while (!U.empty() && A[U.back()] < A[i]) U.pop_back();
if (U.empty()) bl[i][0] = i;
else bl[i][0] = U.back();
U.push_back(i);
if (i != bl[i][0]) adj[bl[i][0]][0].push_back(i);
}
U.clear();
for (int i=n+1; i>=0; --i) {
while (!U.empty() && A[U.back()] < A[i]) U.pop_back();
if (U.empty()) br[i][0] = i;
else br[i][0] = U.back();
U.push_back(i);
if (i != br[i][0]) adj[br[i][0]][1].push_back(i);
}
dfs(0, 0);
cnt = 0;
dfs(1, n+1);
for (int i=1; i<=n; ++i) {
H[A[i]].push_back(i);
}
for (int i=0; i<=n; ++i) {
for (auto [x, w] : V[i]) {
s = tree[0].query(1, 0, n+1, R[x][0][1]) + tree[1].query(1, 0, n+1, R[x][1][1]) + w;
//cout << "? " << R[x][1][1] << " " << tree[1].query(1, 0, n+1, R[x][1][1]) << endl;
//cout << x << " " << i+1 << " " << s << " " << tree[0].query(1, 0, n+1, R[x][0][1]) << " " << tree[1].query(1, 0, n+1, R[x][1][1]) << endl;
opt = max(opt, s);
ST.update(1, 0, n+1, x, s);
}
for (auto u : H[i]) {
s = ST.query(1, 0, n+1, bl[u][0], u);
//cout << i << " " << bl[u][0] << " " << u << " " << s << endl;
tree[0].update(1, 0, n+1, R[u][0][0], R[u][0][1], s);
z = ST.query(1, 0, n+1, u, br[u][0]);
//cout << u << " " << R[u][1][0] << " " << R[u][1][1] << " " << z << endl;
//cout << i << " " << u << " " << br[u][0] << " " << z << endl;
tree[1].update(1, 0, n+1, R[u][1][0], R[u][1][1], z);
//cout << u << " " << bl[u][0] << " " << br[u][0] << " " << s << " " << z << endl;
//cout << u << " " << s << " " << z << endl;
//opt = max(opt, s+z);
}
int j = 0;
for (auto u : H[i]) {
s = ST.query(1, 0, n+1, bl[u][0], u);
z = ST.query(1, 0, n+1, u, br[u][0]);
ST.update(1, 0, n+1, u, s+z);
}
}
cout << f-opt << '\n';
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:133:9: warning: unused variable 'j' [-Wunused-variable]
133 | int j = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29528 KB |
Output is correct |
2 |
Correct |
5 ms |
29532 KB |
Output is correct |
3 |
Correct |
5 ms |
29532 KB |
Output is correct |
4 |
Correct |
4 ms |
29532 KB |
Output is correct |
5 |
Correct |
5 ms |
29448 KB |
Output is correct |
6 |
Correct |
5 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
29532 KB |
Output is correct |
8 |
Correct |
5 ms |
29532 KB |
Output is correct |
9 |
Correct |
5 ms |
29532 KB |
Output is correct |
10 |
Correct |
4 ms |
29532 KB |
Output is correct |
11 |
Correct |
4 ms |
29416 KB |
Output is correct |
12 |
Correct |
4 ms |
29532 KB |
Output is correct |
13 |
Correct |
4 ms |
29532 KB |
Output is correct |
14 |
Correct |
4 ms |
29532 KB |
Output is correct |
15 |
Correct |
4 ms |
29532 KB |
Output is correct |
16 |
Correct |
5 ms |
29532 KB |
Output is correct |
17 |
Correct |
5 ms |
29532 KB |
Output is correct |
18 |
Correct |
4 ms |
29368 KB |
Output is correct |
19 |
Correct |
6 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29532 KB |
Output is correct |
21 |
Correct |
5 ms |
29360 KB |
Output is correct |
22 |
Correct |
5 ms |
29532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29528 KB |
Output is correct |
2 |
Correct |
5 ms |
29532 KB |
Output is correct |
3 |
Correct |
5 ms |
29532 KB |
Output is correct |
4 |
Correct |
4 ms |
29532 KB |
Output is correct |
5 |
Correct |
5 ms |
29448 KB |
Output is correct |
6 |
Correct |
5 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
29532 KB |
Output is correct |
8 |
Correct |
5 ms |
29532 KB |
Output is correct |
9 |
Correct |
5 ms |
29532 KB |
Output is correct |
10 |
Correct |
4 ms |
29532 KB |
Output is correct |
11 |
Correct |
4 ms |
29416 KB |
Output is correct |
12 |
Correct |
4 ms |
29532 KB |
Output is correct |
13 |
Correct |
4 ms |
29532 KB |
Output is correct |
14 |
Correct |
4 ms |
29532 KB |
Output is correct |
15 |
Correct |
4 ms |
29532 KB |
Output is correct |
16 |
Correct |
5 ms |
29532 KB |
Output is correct |
17 |
Correct |
5 ms |
29532 KB |
Output is correct |
18 |
Correct |
4 ms |
29368 KB |
Output is correct |
19 |
Correct |
6 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29532 KB |
Output is correct |
21 |
Correct |
5 ms |
29360 KB |
Output is correct |
22 |
Correct |
5 ms |
29532 KB |
Output is correct |
23 |
Correct |
9 ms |
29788 KB |
Output is correct |
24 |
Correct |
8 ms |
29820 KB |
Output is correct |
25 |
Correct |
8 ms |
29784 KB |
Output is correct |
26 |
Correct |
9 ms |
29788 KB |
Output is correct |
27 |
Correct |
8 ms |
29704 KB |
Output is correct |
28 |
Correct |
7 ms |
29784 KB |
Output is correct |
29 |
Correct |
7 ms |
29788 KB |
Output is correct |
30 |
Correct |
8 ms |
29788 KB |
Output is correct |
31 |
Correct |
8 ms |
29624 KB |
Output is correct |
32 |
Correct |
6 ms |
30040 KB |
Output is correct |
33 |
Correct |
8 ms |
29788 KB |
Output is correct |
34 |
Correct |
8 ms |
29784 KB |
Output is correct |
35 |
Correct |
7 ms |
29788 KB |
Output is correct |
36 |
Correct |
8 ms |
29788 KB |
Output is correct |
37 |
Correct |
7 ms |
29780 KB |
Output is correct |
38 |
Correct |
7 ms |
29908 KB |
Output is correct |
39 |
Correct |
7 ms |
29788 KB |
Output is correct |
40 |
Correct |
6 ms |
29788 KB |
Output is correct |
41 |
Correct |
7 ms |
29808 KB |
Output is correct |
42 |
Correct |
7 ms |
29788 KB |
Output is correct |
43 |
Correct |
7 ms |
29824 KB |
Output is correct |
44 |
Correct |
7 ms |
29712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
29528 KB |
Output is correct |
2 |
Correct |
5 ms |
29532 KB |
Output is correct |
3 |
Correct |
5 ms |
29532 KB |
Output is correct |
4 |
Correct |
4 ms |
29532 KB |
Output is correct |
5 |
Correct |
5 ms |
29448 KB |
Output is correct |
6 |
Correct |
5 ms |
29528 KB |
Output is correct |
7 |
Correct |
4 ms |
29532 KB |
Output is correct |
8 |
Correct |
5 ms |
29532 KB |
Output is correct |
9 |
Correct |
5 ms |
29532 KB |
Output is correct |
10 |
Correct |
4 ms |
29532 KB |
Output is correct |
11 |
Correct |
4 ms |
29416 KB |
Output is correct |
12 |
Correct |
4 ms |
29532 KB |
Output is correct |
13 |
Correct |
4 ms |
29532 KB |
Output is correct |
14 |
Correct |
4 ms |
29532 KB |
Output is correct |
15 |
Correct |
4 ms |
29532 KB |
Output is correct |
16 |
Correct |
5 ms |
29532 KB |
Output is correct |
17 |
Correct |
5 ms |
29532 KB |
Output is correct |
18 |
Correct |
4 ms |
29368 KB |
Output is correct |
19 |
Correct |
6 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29532 KB |
Output is correct |
21 |
Correct |
5 ms |
29360 KB |
Output is correct |
22 |
Correct |
5 ms |
29532 KB |
Output is correct |
23 |
Correct |
9 ms |
29788 KB |
Output is correct |
24 |
Correct |
8 ms |
29820 KB |
Output is correct |
25 |
Correct |
8 ms |
29784 KB |
Output is correct |
26 |
Correct |
9 ms |
29788 KB |
Output is correct |
27 |
Correct |
8 ms |
29704 KB |
Output is correct |
28 |
Correct |
7 ms |
29784 KB |
Output is correct |
29 |
Correct |
7 ms |
29788 KB |
Output is correct |
30 |
Correct |
8 ms |
29788 KB |
Output is correct |
31 |
Correct |
8 ms |
29624 KB |
Output is correct |
32 |
Correct |
6 ms |
30040 KB |
Output is correct |
33 |
Correct |
8 ms |
29788 KB |
Output is correct |
34 |
Correct |
8 ms |
29784 KB |
Output is correct |
35 |
Correct |
7 ms |
29788 KB |
Output is correct |
36 |
Correct |
8 ms |
29788 KB |
Output is correct |
37 |
Correct |
7 ms |
29780 KB |
Output is correct |
38 |
Correct |
7 ms |
29908 KB |
Output is correct |
39 |
Correct |
7 ms |
29788 KB |
Output is correct |
40 |
Correct |
6 ms |
29788 KB |
Output is correct |
41 |
Correct |
7 ms |
29808 KB |
Output is correct |
42 |
Correct |
7 ms |
29788 KB |
Output is correct |
43 |
Correct |
7 ms |
29824 KB |
Output is correct |
44 |
Correct |
7 ms |
29712 KB |
Output is correct |
45 |
Correct |
569 ms |
131240 KB |
Output is correct |
46 |
Correct |
642 ms |
131352 KB |
Output is correct |
47 |
Correct |
544 ms |
130896 KB |
Output is correct |
48 |
Correct |
558 ms |
131156 KB |
Output is correct |
49 |
Correct |
548 ms |
130856 KB |
Output is correct |
50 |
Correct |
542 ms |
130756 KB |
Output is correct |
51 |
Correct |
566 ms |
130896 KB |
Output is correct |
52 |
Correct |
564 ms |
131060 KB |
Output is correct |
53 |
Correct |
582 ms |
130900 KB |
Output is correct |
54 |
Correct |
510 ms |
135876 KB |
Output is correct |
55 |
Correct |
493 ms |
137312 KB |
Output is correct |
56 |
Correct |
499 ms |
136548 KB |
Output is correct |
57 |
Correct |
463 ms |
136644 KB |
Output is correct |
58 |
Correct |
423 ms |
138880 KB |
Output is correct |
59 |
Correct |
383 ms |
138948 KB |
Output is correct |
60 |
Correct |
311 ms |
142020 KB |
Output is correct |
61 |
Correct |
438 ms |
130224 KB |
Output is correct |
62 |
Correct |
523 ms |
136488 KB |
Output is correct |
63 |
Correct |
426 ms |
129948 KB |
Output is correct |
64 |
Correct |
416 ms |
129988 KB |
Output is correct |
65 |
Correct |
477 ms |
136384 KB |
Output is correct |
66 |
Correct |
430 ms |
130088 KB |
Output is correct |