#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll inf = 2e18;
struct LazySegmentTree{
vector<ll> tree;
vector<ll> lazy;
void init(ll n) {
ll sz = 1 << __lg(n-1) + 2;
tree.assign(sz, 0);
lazy.assign(sz, 0);
}
void propagate(ll node, ll s, ll e) {
tree[node] += lazy[node];
if (s != e) {
lazy[node*2] += lazy[node];
lazy[node*2+1] += lazy[node];
}
lazy[node] = 0;
}
void update(ll node, ll s, ll e, ll l, ll r, ll val) {
propagate(node, s, e);
if (l > e || s > r) return;
if (l <= s && e <= r) {
lazy[node] = val;
propagate(node, s, e);
return;
}
ll mid = s + e >> 1;
update(node*2, s, mid, l, r, val);
update(node*2+1, mid+1, e, l, r, val);
tree[node] = max(tree[node*2], tree[node*2+1]);
}
ll query(ll node, ll s, ll e, ll tar) {
propagate(node, s, e);
if (s > tar || tar > e) return -inf;
if (s == e) return tree[node];
ll mid = s + e >> 1;
return max(query(node*2, s, mid, tar), query(node*2+1, mid+1, e, tar));
}
};
struct SegmentTree{
vector<pll> tree;
void init(ll n) {
ll sz = 1 << __lg(n-1) + 2;
tree.assign(sz, make_pair(0, 0));
}
void update(ll node, ll s, ll e, ll tar, pll val) {
if (s > tar || tar > e) return;
if (s == e) {
tree[node] = val;
return;
}
ll mid = s + e >> 1;
update(node*2, s, mid, tar, val);
update(node*2+1, mid+1, e, tar, val);
tree[node] = min(tree[node*2], tree[node*2+1]);
}
pll query(ll node, ll s, ll e, ll l, ll r) {
if (l > e || s > r) return {inf, -1};
if (l <= s && e <= r) return tree[node];
ll mid = s + e >> 1;
return min(query(node*2, s, mid, l, r), query(node*2+1, mid+1, e, l, r));
}
};
ll N, M;
ll C[200010];
ll A[200010];
pll coor[200010];
vector<pll> t;
SegmentTree hseg, qseg;
LazySegmentTree seg;
ll cal(ll l, ll r) {
ll hi = hseg.query(1, 1, N, l, r).first;
ll s = lower_bound(t.begin(), t.end(), make_pair(l, -1LL)) - t.begin() + 1;
ll e = upper_bound(t.begin(), t.end(), make_pair(r, inf)) - t.begin();
vector<ll> cur;
while (true) {
pll ret = qseg.query(1, 1, M, s, e);
if (ret.first > hi) break;
cur.push_back(ret.second);
ll idx = lower_bound(t.begin(), t.end(), make_pair(coor[ret.second].first, ret.second)) - t.begin() + 1;
qseg.update(1, 1, M, idx, make_pair(inf, -1));
}
vector<ll> sep = {l-1};
while (true) {
pll ret = hseg.query(1, 1, N, l, r);
if (ret.first > hi) break;
sep.push_back(ret.second);
hseg.update(1, 1, N, ret.second, make_pair(inf, -1));
}
sep.push_back(r+1);
ll sum = 0;
for (ll i=0; i+1<sep.size(); i++) {
if (sep[i] + 1 <= sep[i+1] - 1) {
sum += cal(sep[i]+1, sep[i+1]-1);
}
}
ll mx = sum;
for (auto &i : cur) {
mx = max(mx, sum + seg.query(1, 1, N, coor[i].first) + C[i]);
}
seg.update(1, 1, N, l, r, sum - mx);
return mx;
}
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
cin >> N;
for (ll i=1; i<=N; i++) {
cin >> A[i];
A[i] = N - A[i];
}
cin >> M;
ll tot = 0;
for (ll i=1; i<=M; i++) {
cin >> coor[i].first >> coor[i].second;
cin >> C[i];
coor[i].second = N - coor[i].second + 1;
t.push_back({coor[i].first, i});
tot += C[i];
}
sort(t.begin(), t.end());
qseg.init(M);
hseg.init(N);
seg.init(N);
for (ll i=1; i<=M; i++) {
ll idx = lower_bound(t.begin(), t.end(), make_pair(coor[i].first, i)) - t.begin() + 1;
qseg.update(1, 1, M, idx, make_pair(coor[i].second, i));
}
for (ll i=1; i<=N; i++) {
hseg.update(1, 1, N, i, make_pair(A[i], i));
}
cout << tot - cal(1, N) << '\n';
}
Compilation message
constellation3.cpp: In member function 'void LazySegmentTree::init(ll)':
constellation3.cpp:14:32: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
14 | ll sz = 1 << __lg(n-1) + 2;
| ~~~~~~~~~~^~~
constellation3.cpp: In member function 'void LazySegmentTree::update(ll, ll, ll, ll, ll, ll)':
constellation3.cpp:34:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | ll mid = s + e >> 1;
| ~~^~~
constellation3.cpp: In member function 'll LazySegmentTree::query(ll, ll, ll, ll)':
constellation3.cpp:43:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll mid = s + e >> 1;
| ~~^~~
constellation3.cpp: In member function 'void SegmentTree::init(ll)':
constellation3.cpp:51:32: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
51 | ll sz = 1 << __lg(n-1) + 2;
| ~~~~~~~~~~^~~
constellation3.cpp: In member function 'void SegmentTree::update(ll, ll, ll, ll, pll)':
constellation3.cpp:60:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | ll mid = s + e >> 1;
| ~~^~~
constellation3.cpp: In member function 'pll SegmentTree::query(ll, ll, ll, ll, ll)':
constellation3.cpp:68:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | ll mid = s + e >> 1;
| ~~^~~
constellation3.cpp: In function 'll cal(ll, ll)':
constellation3.cpp:104:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (ll i=0; i+1<sep.size(); i++) {
| ~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2520 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
2780 KB |
Output is correct |
27 |
Correct |
3 ms |
2648 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
4 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
3040 KB |
Output is correct |
34 |
Correct |
3 ms |
3044 KB |
Output is correct |
35 |
Correct |
3 ms |
2904 KB |
Output is correct |
36 |
Correct |
2 ms |
2648 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 KB |
Output is correct |
38 |
Correct |
3 ms |
3428 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
3 ms |
3124 KB |
Output is correct |
41 |
Correct |
3 ms |
2780 KB |
Output is correct |
42 |
Correct |
3 ms |
2652 KB |
Output is correct |
43 |
Correct |
4 ms |
3164 KB |
Output is correct |
44 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2520 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
2780 KB |
Output is correct |
27 |
Correct |
3 ms |
2648 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
4 ms |
3164 KB |
Output is correct |
33 |
Correct |
3 ms |
3040 KB |
Output is correct |
34 |
Correct |
3 ms |
3044 KB |
Output is correct |
35 |
Correct |
3 ms |
2904 KB |
Output is correct |
36 |
Correct |
2 ms |
2648 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 KB |
Output is correct |
38 |
Correct |
3 ms |
3428 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
3 ms |
3124 KB |
Output is correct |
41 |
Correct |
3 ms |
2780 KB |
Output is correct |
42 |
Correct |
3 ms |
2652 KB |
Output is correct |
43 |
Correct |
4 ms |
3164 KB |
Output is correct |
44 |
Correct |
3 ms |
2652 KB |
Output is correct |
45 |
Correct |
367 ms |
40384 KB |
Output is correct |
46 |
Correct |
375 ms |
40128 KB |
Output is correct |
47 |
Correct |
386 ms |
40368 KB |
Output is correct |
48 |
Correct |
370 ms |
40376 KB |
Output is correct |
49 |
Correct |
343 ms |
39832 KB |
Output is correct |
50 |
Correct |
357 ms |
39868 KB |
Output is correct |
51 |
Correct |
361 ms |
39872 KB |
Output is correct |
52 |
Correct |
359 ms |
40380 KB |
Output is correct |
53 |
Correct |
365 ms |
39876 KB |
Output is correct |
54 |
Correct |
541 ms |
93100 KB |
Output is correct |
55 |
Correct |
513 ms |
80068 KB |
Output is correct |
56 |
Correct |
461 ms |
71616 KB |
Output is correct |
57 |
Correct |
473 ms |
66496 KB |
Output is correct |
58 |
Correct |
282 ms |
44740 KB |
Output is correct |
59 |
Correct |
286 ms |
44224 KB |
Output is correct |
60 |
Correct |
309 ms |
117188 KB |
Output is correct |
61 |
Correct |
342 ms |
39852 KB |
Output is correct |
62 |
Correct |
523 ms |
91068 KB |
Output is correct |
63 |
Correct |
333 ms |
40176 KB |
Output is correct |
64 |
Correct |
333 ms |
39432 KB |
Output is correct |
65 |
Correct |
496 ms |
91772 KB |
Output is correct |
66 |
Correct |
345 ms |
39872 KB |
Output is correct |