#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct BIT{
int n;
vector<ll> sm;
BIT(int _n){
n = _n;
sm.resize(n);
}
void add(int in, ll x){
in++;
while(in <= n) sm[in-1]+=x, in+=in&-in;
}
void add(int l, int r, ll x){
add(l, x);
if(r != n-1) add(r+1, -x);
}
ll get(int in){
in++;
ll s = 0;
while(in >= 1) s+=sm[in-1], in-=in&-in;
return s;
}
};
void preprocess(vector<int>& h, vector<tuple<int, int, ll>>& stars, vector<vector<int>>& tree,
vector<vector<pair<int, ll>>>& paths){
int n = h.size(), m = stars.size();
vector<int> o(n);
for(int i = 0; i < n; i++) o[i] = i;
sort(o.begin(), o.end(), [&](int a, int b){
return h[a] > h[b];
});
sort(stars.begin(), stars.end(), [](tuple<int, int, ll> a, tuple<int, int, ll> b){
return get<1>(a) < get<1>(b);
});
tree.resize(n+1);
paths.resize(n+1);
set<tuple<int, int, int>> ranges;
set<pair<int, ll>> points;
ranges.insert({0, 0, n-1});
for(int _i = 0, i = o[_i]; _i < n; _i++, i = o[_i]){
while(!stars.empty() && get<1>(stars.back()) > h[i]){
auto [x, y, c] = stars.back();
stars.pop_back();
points.insert({x, c});
}
auto it = prev(ranges.upper_bound(make_tuple(i, 1000000000, 1000000000)));
auto [l, p, r] = *it;
ranges.erase(it);
tree[p].pb(i+1);
while(1){
auto it = points.lower_bound(make_pair(l, -1LL));
if(it == points.end() || it->f > r) break;
paths[p].pb({it->f+1, it->sc});
points.erase(it);
}
if(l < i){
int nd = tree.size();
tree.pb({}), paths.pb({});
tree[p].pb(nd);
ranges.insert({l, nd, i-1});
}
if(i < r){
int nd = tree.size();
tree.pb({}), paths.pb({});
tree[p].pb(nd);
ranges.insert({i+1, nd, r});
}
}
}
int N, M, n;
vector<vector<int>> tree;
vector<vector<pair<int, ll>>> paths;
vector<int> pos, sz;
vector<ll> dp;
BIT bit(0);
ll sum = 0;
int intime = 0;
void pre_dfs(int nd){
pos[nd] = intime++;
for(int x: tree[nd]) pre_dfs(x), sz[nd]+=sz[x];
}
void dfs(int nd){
ll sm = 0;
for(int x: tree[nd]) dfs(x), sm+=dp[x], dp[nd]+=dp[x];
for(int x: tree[nd]) bit.add(pos[x], pos[x]+sz[x]-1, sm-dp[x]);
for(auto [x, c]: paths[nd]) dp[nd] = max(dp[nd], c+bit.get(pos[x]));
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
vector<int> h(N);
for(int &x: h) cin >> x;
cin >> M;
vector<tuple<int, int, ll>> stars(M);
for(auto &[x, y, c]: stars) cin >> x >> y >> c, x--, sum+=c;
preprocess(h, stars, tree, paths);
n = tree.size();
pos.resize(n);
sz.assign(n, 1);
dp.assign(n, 0);
bit = BIT(n);
pre_dfs(0);
dfs(0);
cout << sum-dp[0] << "\n";
}
Compilation message
constellation3.cpp: In function 'void preprocess(std::vector<int>&, std::vector<std::tuple<int, int, long long int> >&, std::vector<std::vector<int> >&, std::vector<std::vector<std::pair<int, long long int> > >&)':
constellation3.cpp:34:23: warning: unused variable 'm' [-Wunused-variable]
34 | int n = h.size(), m = stars.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
4 ms |
752 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
4 ms |
724 KB |
Output is correct |
29 |
Correct |
4 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
33 |
Correct |
3 ms |
848 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
724 KB |
Output is correct |
37 |
Correct |
4 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
880 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
832 KB |
Output is correct |
44 |
Correct |
3 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
4 ms |
752 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
4 ms |
724 KB |
Output is correct |
29 |
Correct |
4 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
33 |
Correct |
3 ms |
848 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
724 KB |
Output is correct |
37 |
Correct |
4 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
880 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
832 KB |
Output is correct |
44 |
Correct |
3 ms |
748 KB |
Output is correct |
45 |
Correct |
725 ms |
47996 KB |
Output is correct |
46 |
Correct |
742 ms |
47488 KB |
Output is correct |
47 |
Correct |
701 ms |
47056 KB |
Output is correct |
48 |
Correct |
661 ms |
47680 KB |
Output is correct |
49 |
Correct |
751 ms |
46684 KB |
Output is correct |
50 |
Correct |
784 ms |
46452 KB |
Output is correct |
51 |
Correct |
818 ms |
46840 KB |
Output is correct |
52 |
Correct |
766 ms |
47596 KB |
Output is correct |
53 |
Correct |
748 ms |
47416 KB |
Output is correct |
54 |
Correct |
258 ms |
56740 KB |
Output is correct |
55 |
Correct |
269 ms |
52932 KB |
Output is correct |
56 |
Correct |
252 ms |
50924 KB |
Output is correct |
57 |
Correct |
240 ms |
49488 KB |
Output is correct |
58 |
Correct |
245 ms |
46020 KB |
Output is correct |
59 |
Correct |
264 ms |
45808 KB |
Output is correct |
60 |
Correct |
204 ms |
62080 KB |
Output is correct |
61 |
Correct |
391 ms |
46164 KB |
Output is correct |
62 |
Correct |
270 ms |
56956 KB |
Output is correct |
63 |
Correct |
497 ms |
45408 KB |
Output is correct |
64 |
Correct |
408 ms |
44620 KB |
Output is correct |
65 |
Correct |
263 ms |
57460 KB |
Output is correct |
66 |
Correct |
347 ms |
44932 KB |
Output is correct |