// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
int n, a[N], root;
ii rmq[N][LG];
vi adj[N];
ii get(int l, int r){
int lg = log2(r - l + 1);
return max(rmq[l][lg], rmq[r - (1 << lg) + 1][lg]);
}
void getEd(int p, int l, int r){
if (l > r) return;
int u = get(l, r).se;
if (!p) root = u; else adj[p].pb(u);
getEd(u, l, u - 1); getEd(u, u + 1, r);
}
int timer, t_in[N], t_out[N], jump[N][LG];
void dfs1(int u = root){
t_in[u] = ++ timer;
for (int j = 1; j < LG; ++ j){
int to = jump[u][j - 1];
if (!to) break;
jump[u][j] = jump[to][j - 1];
}
for (int v : adj[u]) {
jump[v][0] = u; dfs1(v);
}
t_out[u] = timer;
}
vector <ii> stars[N];
ll dp[N], bit[N];
void upd(int l, int r, ll v){
for (; l <= n; l += l & -l) bit[l] += v;
for (r = r + 1; r <= n; r += r & -r) bit[r] -= v;
}
ll qry(int u){
ll ans = 0;
for (; u; u -= u & -u) ans += bit[u];
return ans;
}
void dfs2(int u = root){
int s = 0;
for (int v : adj[u]) dfs2(v), s += v, dp[u] += dp[v];
for (int v : adj[u]) if (v ^ s) upd(t_in[v], t_out[v], dp[s - v]);
for (auto [x, c] : stars[u]){
ll cost = c;
cost += qry(t_in[x]);
for (int v : adj[x]) cost += dp[v];
maxl(dp[u], cost);
}
}
void solve(){
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i]; rmq[i][0] = {a[i], i};
}
for (int i = n; i >= 1; -- i){
for (int j = 1; j < LG; ++ j){
int u = i + (1 << (j - 1));
if (u > n) break;
rmq[i][j] = max(rmq[i][j - 1], rmq[u][j - 1]);
}
}
getEd(0, 1, n); dfs1();
int m; cin >> m; ll ans = 0;
while (m --){
int x, y, c; cin >> x >> y >> c; ans += c;
int u = x; for (int j = LG - 1; j >= 0; -- j){
int to = jump[u][j];
if (to && a[to] < y) u = to;
}
stars[u].pb({x, c});
}
dfs2();
cout << ans - dp[root];
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message
constellation3.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
constellation3.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
constellation3.cpp:20:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
constellation3.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen("pqh.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen("pqh.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9784 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9872 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
5 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
5 ms |
9952 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9784 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9872 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
5 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
5 ms |
9952 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
7 ms |
10332 KB |
Output is correct |
27 |
Correct |
5 ms |
10328 KB |
Output is correct |
28 |
Correct |
6 ms |
10332 KB |
Output is correct |
29 |
Correct |
5 ms |
10332 KB |
Output is correct |
30 |
Correct |
6 ms |
10480 KB |
Output is correct |
31 |
Correct |
5 ms |
10420 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
6 ms |
10588 KB |
Output is correct |
34 |
Correct |
5 ms |
10584 KB |
Output is correct |
35 |
Correct |
5 ms |
10588 KB |
Output is correct |
36 |
Correct |
5 ms |
10656 KB |
Output is correct |
37 |
Correct |
5 ms |
10588 KB |
Output is correct |
38 |
Correct |
5 ms |
10588 KB |
Output is correct |
39 |
Correct |
6 ms |
10336 KB |
Output is correct |
40 |
Correct |
5 ms |
10588 KB |
Output is correct |
41 |
Correct |
6 ms |
10584 KB |
Output is correct |
42 |
Correct |
5 ms |
10332 KB |
Output is correct |
43 |
Correct |
5 ms |
10648 KB |
Output is correct |
44 |
Correct |
5 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9784 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9872 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
5 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
5 ms |
9952 KB |
Output is correct |
20 |
Correct |
5 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9816 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
7 ms |
10332 KB |
Output is correct |
27 |
Correct |
5 ms |
10328 KB |
Output is correct |
28 |
Correct |
6 ms |
10332 KB |
Output is correct |
29 |
Correct |
5 ms |
10332 KB |
Output is correct |
30 |
Correct |
6 ms |
10480 KB |
Output is correct |
31 |
Correct |
5 ms |
10420 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
6 ms |
10588 KB |
Output is correct |
34 |
Correct |
5 ms |
10584 KB |
Output is correct |
35 |
Correct |
5 ms |
10588 KB |
Output is correct |
36 |
Correct |
5 ms |
10656 KB |
Output is correct |
37 |
Correct |
5 ms |
10588 KB |
Output is correct |
38 |
Correct |
5 ms |
10588 KB |
Output is correct |
39 |
Correct |
6 ms |
10336 KB |
Output is correct |
40 |
Correct |
5 ms |
10588 KB |
Output is correct |
41 |
Correct |
6 ms |
10584 KB |
Output is correct |
42 |
Correct |
5 ms |
10332 KB |
Output is correct |
43 |
Correct |
5 ms |
10648 KB |
Output is correct |
44 |
Correct |
5 ms |
10332 KB |
Output is correct |
45 |
Correct |
187 ms |
75092 KB |
Output is correct |
46 |
Correct |
179 ms |
74388 KB |
Output is correct |
47 |
Correct |
180 ms |
73732 KB |
Output is correct |
48 |
Correct |
179 ms |
75092 KB |
Output is correct |
49 |
Correct |
175 ms |
73300 KB |
Output is correct |
50 |
Correct |
193 ms |
73044 KB |
Output is correct |
51 |
Correct |
181 ms |
73092 KB |
Output is correct |
52 |
Correct |
182 ms |
74772 KB |
Output is correct |
53 |
Correct |
180 ms |
74660 KB |
Output is correct |
54 |
Correct |
288 ms |
87388 KB |
Output is correct |
55 |
Correct |
272 ms |
82576 KB |
Output is correct |
56 |
Correct |
268 ms |
79700 KB |
Output is correct |
57 |
Correct |
249 ms |
78040 KB |
Output is correct |
58 |
Correct |
207 ms |
82516 KB |
Output is correct |
59 |
Correct |
216 ms |
82256 KB |
Output is correct |
60 |
Correct |
145 ms |
93268 KB |
Output is correct |
61 |
Correct |
170 ms |
74580 KB |
Output is correct |
62 |
Correct |
250 ms |
87896 KB |
Output is correct |
63 |
Correct |
168 ms |
73440 KB |
Output is correct |
64 |
Correct |
164 ms |
72272 KB |
Output is correct |
65 |
Correct |
253 ms |
88860 KB |
Output is correct |
66 |
Correct |
194 ms |
72788 KB |
Output is correct |