// In the name of God
// sorry M
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
#define int ll
const int N = 4e5 + 5, L = 20;
int n, m, A[N], H[N];
int id[N], sprc[N][L], ver, tin[N], tout[N], T;
set<pair<int, pair<int, int> > > st[N];
vector<int> adj[N];
ll dp[N], fen[N];
ll query(int i) {
ll res = 0;
for (i; i > 0; i -= i & -i)
res += fen[i];
return res;
}
void upd(int i, int val) {
for (i; i < N; i += i & -i)
fen[i] += val;
}
void range_upd(int l, int r, int val) {
upd(l, val);
upd(r + 1, -val);
}
void _merge(int v, int u) {
if (st[v].size() < st[u].size())
st[v].swap(st[u]);
for (auto x : st[u])
st[v].insert(x);
}
void dfs(int v, int p = 0) {
tin[v] = ++T;
int lc = 0, rc = 0;
for (auto u : adj[v]) {
dfs(u, v);
_merge(v, u);
if (lc == 0)
lc = u;
else
rc = u;
}
if (rc) {
range_upd(tin[lc], tout[lc], dp[rc]);
range_upd(tin[rc], tout[rc], dp[lc]);
}
dp[v] = dp[lc] + dp[rc];
range_upd(tin[v], tin[v], dp[v]);
while (!st[v].empty() && st[v].begin()->fi <= H[p]) {
dp[v] = max(dp[v], query(tin[st[v].begin()->se.fi]) + st[v].begin()->se.se);
//cout << "rem " << v << ' ' << st[v].begin()->se.fi << ' ' << query(tin[st[v].begin()->se.fi]) << endl;
st[v].erase(st[v].begin());
}
tout[v] = ++T;
//cout << v << ' ' << dp[v] << endl;
}
int build(int l, int r) {
if (r < l)
return -1;
int lg = __lg(r - l + 1);
int v = sprc[l][lg];
if (A[v] < A[sprc[r - (1 << lg) + 1][lg]])
v = sprc[r - (1 << lg) + 1][lg];
id[v] = ++ver;
int u = ver;
H[u] = A[v];
int lc = build(l, v - 1);
int rc = build(v + 1, r);
if (lc > -1)
adj[u].pb(lc);
if (rc > -1)
adj[u].pb(rc);
return u;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> A[i];
for (int i = n; i >= 1; i--) {
sprc[i][0] = i;
for (int l = 1; l < L; l++) {
sprc[i][l] = sprc[i][l - 1];
if (i + (1 << (l - 1)) <= n && A[sprc[i][l]] < A[sprc[i + (1 << (l - 1))][l - 1]])
sprc[i][l] = sprc[i + (1 << (l - 1))][l - 1];
}
}
build(1, n);
ll sum = 0;
cin >> m;
for (int i = 0; i < m; i++) {
int x, y, c; cin >> x >> y >> c;
st[id[x]].insert(mp(y, mp(id[x], c)));
sum += c;
}
H[0] = 1e9 + 10;
dfs(1);
//cout << dp[1] << endl;
cout << sum - dp[1];
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message
constellation3.cpp: In function 'll query(ll)':
constellation3.cpp:20:10: warning: statement has no effect [-Wunused-value]
20 | for (i; i > 0; i -= i & -i)
| ^
constellation3.cpp: In function 'void upd(ll, ll)':
constellation3.cpp:25:10: warning: statement has no effect [-Wunused-value]
25 | for (i; i < N; i += i & -i)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28628 KB |
Output is correct |
2 |
Correct |
18 ms |
28620 KB |
Output is correct |
3 |
Correct |
17 ms |
28628 KB |
Output is correct |
4 |
Correct |
16 ms |
28632 KB |
Output is correct |
5 |
Correct |
15 ms |
28628 KB |
Output is correct |
6 |
Correct |
14 ms |
28656 KB |
Output is correct |
7 |
Correct |
15 ms |
28628 KB |
Output is correct |
8 |
Correct |
14 ms |
28628 KB |
Output is correct |
9 |
Correct |
15 ms |
28648 KB |
Output is correct |
10 |
Correct |
14 ms |
28628 KB |
Output is correct |
11 |
Correct |
14 ms |
28628 KB |
Output is correct |
12 |
Correct |
16 ms |
28652 KB |
Output is correct |
13 |
Correct |
15 ms |
28628 KB |
Output is correct |
14 |
Correct |
16 ms |
28632 KB |
Output is correct |
15 |
Correct |
14 ms |
28628 KB |
Output is correct |
16 |
Correct |
16 ms |
28788 KB |
Output is correct |
17 |
Correct |
14 ms |
28628 KB |
Output is correct |
18 |
Correct |
14 ms |
28640 KB |
Output is correct |
19 |
Correct |
22 ms |
28684 KB |
Output is correct |
20 |
Correct |
14 ms |
28628 KB |
Output is correct |
21 |
Correct |
14 ms |
28628 KB |
Output is correct |
22 |
Correct |
15 ms |
28676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28628 KB |
Output is correct |
2 |
Correct |
18 ms |
28620 KB |
Output is correct |
3 |
Correct |
17 ms |
28628 KB |
Output is correct |
4 |
Correct |
16 ms |
28632 KB |
Output is correct |
5 |
Correct |
15 ms |
28628 KB |
Output is correct |
6 |
Correct |
14 ms |
28656 KB |
Output is correct |
7 |
Correct |
15 ms |
28628 KB |
Output is correct |
8 |
Correct |
14 ms |
28628 KB |
Output is correct |
9 |
Correct |
15 ms |
28648 KB |
Output is correct |
10 |
Correct |
14 ms |
28628 KB |
Output is correct |
11 |
Correct |
14 ms |
28628 KB |
Output is correct |
12 |
Correct |
16 ms |
28652 KB |
Output is correct |
13 |
Correct |
15 ms |
28628 KB |
Output is correct |
14 |
Correct |
16 ms |
28632 KB |
Output is correct |
15 |
Correct |
14 ms |
28628 KB |
Output is correct |
16 |
Correct |
16 ms |
28788 KB |
Output is correct |
17 |
Correct |
14 ms |
28628 KB |
Output is correct |
18 |
Correct |
14 ms |
28640 KB |
Output is correct |
19 |
Correct |
22 ms |
28684 KB |
Output is correct |
20 |
Correct |
14 ms |
28628 KB |
Output is correct |
21 |
Correct |
14 ms |
28628 KB |
Output is correct |
22 |
Correct |
15 ms |
28676 KB |
Output is correct |
23 |
Correct |
25 ms |
29192 KB |
Output is correct |
24 |
Correct |
17 ms |
29140 KB |
Output is correct |
25 |
Correct |
18 ms |
29140 KB |
Output is correct |
26 |
Correct |
17 ms |
29144 KB |
Output is correct |
27 |
Correct |
16 ms |
29140 KB |
Output is correct |
28 |
Correct |
18 ms |
29140 KB |
Output is correct |
29 |
Correct |
16 ms |
29140 KB |
Output is correct |
30 |
Correct |
16 ms |
29176 KB |
Output is correct |
31 |
Correct |
16 ms |
29176 KB |
Output is correct |
32 |
Correct |
16 ms |
29268 KB |
Output is correct |
33 |
Correct |
16 ms |
29268 KB |
Output is correct |
34 |
Correct |
16 ms |
29304 KB |
Output is correct |
35 |
Correct |
17 ms |
29276 KB |
Output is correct |
36 |
Correct |
16 ms |
29396 KB |
Output is correct |
37 |
Correct |
16 ms |
29380 KB |
Output is correct |
38 |
Correct |
15 ms |
29396 KB |
Output is correct |
39 |
Correct |
17 ms |
29396 KB |
Output is correct |
40 |
Correct |
16 ms |
29360 KB |
Output is correct |
41 |
Correct |
17 ms |
29404 KB |
Output is correct |
42 |
Correct |
16 ms |
29320 KB |
Output is correct |
43 |
Correct |
15 ms |
29396 KB |
Output is correct |
44 |
Correct |
16 ms |
29248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28628 KB |
Output is correct |
2 |
Correct |
18 ms |
28620 KB |
Output is correct |
3 |
Correct |
17 ms |
28628 KB |
Output is correct |
4 |
Correct |
16 ms |
28632 KB |
Output is correct |
5 |
Correct |
15 ms |
28628 KB |
Output is correct |
6 |
Correct |
14 ms |
28656 KB |
Output is correct |
7 |
Correct |
15 ms |
28628 KB |
Output is correct |
8 |
Correct |
14 ms |
28628 KB |
Output is correct |
9 |
Correct |
15 ms |
28648 KB |
Output is correct |
10 |
Correct |
14 ms |
28628 KB |
Output is correct |
11 |
Correct |
14 ms |
28628 KB |
Output is correct |
12 |
Correct |
16 ms |
28652 KB |
Output is correct |
13 |
Correct |
15 ms |
28628 KB |
Output is correct |
14 |
Correct |
16 ms |
28632 KB |
Output is correct |
15 |
Correct |
14 ms |
28628 KB |
Output is correct |
16 |
Correct |
16 ms |
28788 KB |
Output is correct |
17 |
Correct |
14 ms |
28628 KB |
Output is correct |
18 |
Correct |
14 ms |
28640 KB |
Output is correct |
19 |
Correct |
22 ms |
28684 KB |
Output is correct |
20 |
Correct |
14 ms |
28628 KB |
Output is correct |
21 |
Correct |
14 ms |
28628 KB |
Output is correct |
22 |
Correct |
15 ms |
28676 KB |
Output is correct |
23 |
Correct |
25 ms |
29192 KB |
Output is correct |
24 |
Correct |
17 ms |
29140 KB |
Output is correct |
25 |
Correct |
18 ms |
29140 KB |
Output is correct |
26 |
Correct |
17 ms |
29144 KB |
Output is correct |
27 |
Correct |
16 ms |
29140 KB |
Output is correct |
28 |
Correct |
18 ms |
29140 KB |
Output is correct |
29 |
Correct |
16 ms |
29140 KB |
Output is correct |
30 |
Correct |
16 ms |
29176 KB |
Output is correct |
31 |
Correct |
16 ms |
29176 KB |
Output is correct |
32 |
Correct |
16 ms |
29268 KB |
Output is correct |
33 |
Correct |
16 ms |
29268 KB |
Output is correct |
34 |
Correct |
16 ms |
29304 KB |
Output is correct |
35 |
Correct |
17 ms |
29276 KB |
Output is correct |
36 |
Correct |
16 ms |
29396 KB |
Output is correct |
37 |
Correct |
16 ms |
29380 KB |
Output is correct |
38 |
Correct |
15 ms |
29396 KB |
Output is correct |
39 |
Correct |
17 ms |
29396 KB |
Output is correct |
40 |
Correct |
16 ms |
29360 KB |
Output is correct |
41 |
Correct |
17 ms |
29404 KB |
Output is correct |
42 |
Correct |
16 ms |
29320 KB |
Output is correct |
43 |
Correct |
15 ms |
29396 KB |
Output is correct |
44 |
Correct |
16 ms |
29248 KB |
Output is correct |
45 |
Correct |
241 ms |
91296 KB |
Output is correct |
46 |
Correct |
252 ms |
90464 KB |
Output is correct |
47 |
Correct |
276 ms |
90000 KB |
Output is correct |
48 |
Correct |
289 ms |
91100 KB |
Output is correct |
49 |
Correct |
260 ms |
89412 KB |
Output is correct |
50 |
Correct |
249 ms |
89364 KB |
Output is correct |
51 |
Correct |
315 ms |
89740 KB |
Output is correct |
52 |
Correct |
288 ms |
90896 KB |
Output is correct |
53 |
Correct |
241 ms |
90572 KB |
Output is correct |
54 |
Correct |
336 ms |
110128 KB |
Output is correct |
55 |
Correct |
348 ms |
107416 KB |
Output is correct |
56 |
Correct |
347 ms |
105932 KB |
Output is correct |
57 |
Correct |
375 ms |
105496 KB |
Output is correct |
58 |
Correct |
362 ms |
110168 KB |
Output is correct |
59 |
Correct |
363 ms |
110044 KB |
Output is correct |
60 |
Correct |
192 ms |
109812 KB |
Output is correct |
61 |
Correct |
349 ms |
121816 KB |
Output is correct |
62 |
Correct |
336 ms |
111052 KB |
Output is correct |
63 |
Correct |
360 ms |
120168 KB |
Output is correct |
64 |
Correct |
364 ms |
118100 KB |
Output is correct |
65 |
Correct |
338 ms |
111332 KB |
Output is correct |
66 |
Correct |
366 ms |
123380 KB |
Output is correct |