#define DBG 1
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (DBG) cerr << #x << " = " << x << endl;
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)2e5 + 5;
vector<int> pos[MAXN];
vector<pii> stars[MAXN];
ll dp[MAXN], ext[MAXN];
ll fenw[MAXN];
int par[MAXN], L[MAXN], R[MAXN], sz[MAXN];
int arr[MAXN];
bool active[MAXN];
ll sum;
int n, m;
void fenwUpd(int p, ll x) {
for (; p <= n; p |= (p + 1)) {
fenw[p] += x;
}
}
void update(int l, int r, ll x) {
fenwUpd(l, x);
fenwUpd(r + 1, -x);
}
ll getVal(int p) {
ll ret = 0;
for (; p > 0; --p) {
ret += fenw[p];
p &= (p + 1);
}
return ret;
}
int getPar(int x) {
if (x == par[x]) {
return x;
}
return par[x] = getPar(par[x]);
}
void uni(int a, int b) {
a = getPar(a);
b = getPar(b);
if (a == b) {
return;
}
if (sz[a] < sz[b]) {
swap(a, b);
}
dp[a] += ext[a];
ext[a] = 0;
dp[b] += ext[b];
ext[b] = 0;
update(L[a], R[a], dp[b]);
update(L[b], R[b], dp[a]);
dp[a] += dp[b];
sz[a] += sz[b];
par[b] = a;
L[a] = min(L[a], L[b]);
R[a] = max(R[a], R[b]);
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
pos[arr[i]].pb(i);
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
stars[y].pb(mp(x, c));
sum += c;
}
for (int i = 1; i <= n; ++i) {
par[i] = L[i] = R[i] = i;
}
for (int y = 1; y <= n; ++y) {
for (auto it : stars[y]) {
int x, c;
tie(x, c) = it;
int p = getPar(x);
ext[p] = max(ext[p], c + getVal(x) - dp[p]);
}
for (int x : pos[y]) {
active[x] = 1;
if (active[x - 1]) {
uni(x - 1, x);
}
if (active[x + 1]) {
uni(x, x + 1);
}
}
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
if (getPar(i) == i) {
ans = max(ans, dp[i] + ext[i]);
}
}
printf("%lld\n", sum - ans);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
constellation3.cpp: In function 'void solve()':
constellation3.cpp:91:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
constellation3.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
constellation3.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &y, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
14 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9600 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
14 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9600 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
11 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9980 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
12 ms |
9984 KB |
Output is correct |
27 |
Correct |
11 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9984 KB |
Output is correct |
30 |
Correct |
14 ms |
10104 KB |
Output is correct |
31 |
Correct |
16 ms |
9984 KB |
Output is correct |
32 |
Correct |
12 ms |
9984 KB |
Output is correct |
33 |
Correct |
11 ms |
9984 KB |
Output is correct |
34 |
Correct |
15 ms |
9984 KB |
Output is correct |
35 |
Correct |
11 ms |
9984 KB |
Output is correct |
36 |
Correct |
15 ms |
9984 KB |
Output is correct |
37 |
Correct |
12 ms |
9984 KB |
Output is correct |
38 |
Correct |
15 ms |
9984 KB |
Output is correct |
39 |
Correct |
11 ms |
9984 KB |
Output is correct |
40 |
Correct |
11 ms |
9984 KB |
Output is correct |
41 |
Correct |
11 ms |
9984 KB |
Output is correct |
42 |
Correct |
11 ms |
9984 KB |
Output is correct |
43 |
Correct |
13 ms |
9984 KB |
Output is correct |
44 |
Correct |
11 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
14 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9856 KB |
Output is correct |
11 |
Correct |
10 ms |
9856 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
11 ms |
9600 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9728 KB |
Output is correct |
19 |
Correct |
10 ms |
9728 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
11 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9980 KB |
Output is correct |
25 |
Correct |
12 ms |
9984 KB |
Output is correct |
26 |
Correct |
12 ms |
9984 KB |
Output is correct |
27 |
Correct |
11 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9984 KB |
Output is correct |
30 |
Correct |
14 ms |
10104 KB |
Output is correct |
31 |
Correct |
16 ms |
9984 KB |
Output is correct |
32 |
Correct |
12 ms |
9984 KB |
Output is correct |
33 |
Correct |
11 ms |
9984 KB |
Output is correct |
34 |
Correct |
15 ms |
9984 KB |
Output is correct |
35 |
Correct |
11 ms |
9984 KB |
Output is correct |
36 |
Correct |
15 ms |
9984 KB |
Output is correct |
37 |
Correct |
12 ms |
9984 KB |
Output is correct |
38 |
Correct |
15 ms |
9984 KB |
Output is correct |
39 |
Correct |
11 ms |
9984 KB |
Output is correct |
40 |
Correct |
11 ms |
9984 KB |
Output is correct |
41 |
Correct |
11 ms |
9984 KB |
Output is correct |
42 |
Correct |
11 ms |
9984 KB |
Output is correct |
43 |
Correct |
13 ms |
9984 KB |
Output is correct |
44 |
Correct |
11 ms |
9984 KB |
Output is correct |
45 |
Correct |
305 ms |
26336 KB |
Output is correct |
46 |
Correct |
260 ms |
25976 KB |
Output is correct |
47 |
Correct |
262 ms |
25852 KB |
Output is correct |
48 |
Correct |
259 ms |
26452 KB |
Output is correct |
49 |
Correct |
248 ms |
25720 KB |
Output is correct |
50 |
Correct |
267 ms |
25720 KB |
Output is correct |
51 |
Correct |
273 ms |
25976 KB |
Output is correct |
52 |
Correct |
255 ms |
26104 KB |
Output is correct |
53 |
Correct |
262 ms |
26232 KB |
Output is correct |
54 |
Correct |
228 ms |
28156 KB |
Output is correct |
55 |
Correct |
243 ms |
28180 KB |
Output is correct |
56 |
Correct |
200 ms |
28096 KB |
Output is correct |
57 |
Correct |
208 ms |
27896 KB |
Output is correct |
58 |
Correct |
196 ms |
22904 KB |
Output is correct |
59 |
Correct |
190 ms |
23032 KB |
Output is correct |
60 |
Correct |
155 ms |
27260 KB |
Output is correct |
61 |
Correct |
150 ms |
21752 KB |
Output is correct |
62 |
Correct |
217 ms |
25792 KB |
Output is correct |
63 |
Correct |
147 ms |
21736 KB |
Output is correct |
64 |
Correct |
146 ms |
21624 KB |
Output is correct |
65 |
Correct |
211 ms |
25848 KB |
Output is correct |
66 |
Correct |
150 ms |
21360 KB |
Output is correct |