#include <bits/stdc++.h>
#define long long long
#define pii pair<long, long>
#define x first
#define y second
using namespace std;
#define var int p = 1, int l = 1, int r = n
#define mid ((l + r) >> 1)
#define lb p << 1, l, mid
#define rb p << 1 | 1, mid + 1, r
const int N = 1 << 18;
struct UnionFind {
int par[N], l[N], r[N];
UnionFind() {
iota(par, par + N, 0);
iota(l, r + N, 0);
iota(r, r + N, 0);
}
int find(int x) { return par[x] = x == par[x] ? x : find(par[x]); }
void unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
par[a] = b, l[b] = min(l[b], l[a]), r[b] = max(r[b], r[a]);
}
} dsu;
int n, k;
long t[N << 1], lz[N << 1];
void push(var) {
if(lz[p] != 0) {
t[p] += lz[p];
if(l != r) lz[p << 1] += lz[p], lz[p << 1 | 1] += lz[p];
lz[p] = 0;
}
}
void update(int x, int y, long k, var) {
push(p, l, r);
if(x > r || l > y) return;
if(x <= l && r <= y) {
lz[p] += k, push(p, l, r);
return;
}
update(x, y, k, lb), update(x, y, k, rb);
t[p] = max(t[p << 1], t[p << 1 | 1]);
}
long query(int x, int y, var) {
push(p, l, r);
if(x > r || l > y) return 0;
if(x <= l && r <= y) return t[p];
return max(query(x, y, lb), query(x, y, rb));
}
int A[N];
vector<int> Y[N];
vector<pii> star_x[N], star_y[N];
bitset<N> chk;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", A + i);
Y[A[i] + 1].emplace_back(i);
}
scanf("%d", &k);
long sum = 0;
for(int i = 1, x, y, c; i <= k; i++) {
scanf("%d %d %d", &x, &y, &c);
star_x[x].emplace_back(y, c);
sum += c;
}
for(int i = 1; i <= n; i++) {
sort(star_x[i].begin(), star_x[i].end());
int pre = 0;
for(pii p : star_x[i]) if(p.y > pre) {
star_y[p.x].emplace_back(i, p.y - pre);
pre = p.y;
}
}
for(int i = 1; i <= n + 1; i++) {
for(int x : Y[i]) {
long val_l = 0, val_r = 0;
if(x > 1 && chk[x - 1]) {
int par_l = dsu.find(x - 1);
val_l = query(dsu.l[par_l], dsu.r[par_l]);
}
if(x < n && chk[x + 1]) {
int par_r = dsu.find(x + 1);
val_r = query(dsu.l[par_r], dsu.r[par_r]);
}
if(x > 1 && chk[x - 1]) {
int par_l = dsu.find(x - 1);
update(dsu.l[par_l], dsu.r[par_l], val_r);
}
if(x < n && chk[x + 1]) {
int par_r = dsu.find(x + 1);
update(dsu.l[par_r], dsu.r[par_r], val_l);
}
update(x, x, val_l + val_r);
if(x > 1 && chk[x - 1]) dsu.unite(x - 1, x);
if(x < n && chk[x + 1]) dsu.unite(x, x + 1);
chk[x] = 1;
}
for(pii p : star_y[i]) update(p.x, p.x, p.y);
}
printf("%lld\n", sum - t[1]);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", A + i);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
constellation3.cpp:79: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21888 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
16 ms |
22016 KB |
Output is correct |
4 |
Correct |
19 ms |
21888 KB |
Output is correct |
5 |
Correct |
16 ms |
22016 KB |
Output is correct |
6 |
Correct |
16 ms |
21888 KB |
Output is correct |
7 |
Correct |
16 ms |
21888 KB |
Output is correct |
8 |
Correct |
16 ms |
21888 KB |
Output is correct |
9 |
Correct |
18 ms |
21888 KB |
Output is correct |
10 |
Correct |
16 ms |
21888 KB |
Output is correct |
11 |
Correct |
16 ms |
22016 KB |
Output is correct |
12 |
Correct |
16 ms |
21888 KB |
Output is correct |
13 |
Correct |
17 ms |
21888 KB |
Output is correct |
14 |
Correct |
16 ms |
21888 KB |
Output is correct |
15 |
Correct |
16 ms |
21888 KB |
Output is correct |
16 |
Correct |
16 ms |
22016 KB |
Output is correct |
17 |
Correct |
16 ms |
21944 KB |
Output is correct |
18 |
Correct |
16 ms |
21888 KB |
Output is correct |
19 |
Correct |
16 ms |
21888 KB |
Output is correct |
20 |
Correct |
16 ms |
21888 KB |
Output is correct |
21 |
Correct |
16 ms |
22016 KB |
Output is correct |
22 |
Correct |
16 ms |
22016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21888 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
16 ms |
22016 KB |
Output is correct |
4 |
Correct |
19 ms |
21888 KB |
Output is correct |
5 |
Correct |
16 ms |
22016 KB |
Output is correct |
6 |
Correct |
16 ms |
21888 KB |
Output is correct |
7 |
Correct |
16 ms |
21888 KB |
Output is correct |
8 |
Correct |
16 ms |
21888 KB |
Output is correct |
9 |
Correct |
18 ms |
21888 KB |
Output is correct |
10 |
Correct |
16 ms |
21888 KB |
Output is correct |
11 |
Correct |
16 ms |
22016 KB |
Output is correct |
12 |
Correct |
16 ms |
21888 KB |
Output is correct |
13 |
Correct |
17 ms |
21888 KB |
Output is correct |
14 |
Correct |
16 ms |
21888 KB |
Output is correct |
15 |
Correct |
16 ms |
21888 KB |
Output is correct |
16 |
Correct |
16 ms |
22016 KB |
Output is correct |
17 |
Correct |
16 ms |
21944 KB |
Output is correct |
18 |
Correct |
16 ms |
21888 KB |
Output is correct |
19 |
Correct |
16 ms |
21888 KB |
Output is correct |
20 |
Correct |
16 ms |
21888 KB |
Output is correct |
21 |
Correct |
16 ms |
22016 KB |
Output is correct |
22 |
Correct |
16 ms |
22016 KB |
Output is correct |
23 |
Correct |
18 ms |
22144 KB |
Output is correct |
24 |
Correct |
19 ms |
22144 KB |
Output is correct |
25 |
Correct |
18 ms |
22144 KB |
Output is correct |
26 |
Correct |
19 ms |
22144 KB |
Output is correct |
27 |
Correct |
18 ms |
22144 KB |
Output is correct |
28 |
Correct |
19 ms |
22144 KB |
Output is correct |
29 |
Correct |
18 ms |
22016 KB |
Output is correct |
30 |
Correct |
19 ms |
22144 KB |
Output is correct |
31 |
Correct |
20 ms |
22144 KB |
Output is correct |
32 |
Correct |
19 ms |
22144 KB |
Output is correct |
33 |
Correct |
20 ms |
22144 KB |
Output is correct |
34 |
Correct |
19 ms |
22144 KB |
Output is correct |
35 |
Correct |
19 ms |
22144 KB |
Output is correct |
36 |
Correct |
20 ms |
22016 KB |
Output is correct |
37 |
Correct |
18 ms |
22016 KB |
Output is correct |
38 |
Correct |
20 ms |
22144 KB |
Output is correct |
39 |
Correct |
20 ms |
22016 KB |
Output is correct |
40 |
Correct |
19 ms |
22144 KB |
Output is correct |
41 |
Correct |
18 ms |
22144 KB |
Output is correct |
42 |
Correct |
18 ms |
22016 KB |
Output is correct |
43 |
Correct |
19 ms |
22144 KB |
Output is correct |
44 |
Correct |
18 ms |
22016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21888 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
16 ms |
22016 KB |
Output is correct |
4 |
Correct |
19 ms |
21888 KB |
Output is correct |
5 |
Correct |
16 ms |
22016 KB |
Output is correct |
6 |
Correct |
16 ms |
21888 KB |
Output is correct |
7 |
Correct |
16 ms |
21888 KB |
Output is correct |
8 |
Correct |
16 ms |
21888 KB |
Output is correct |
9 |
Correct |
18 ms |
21888 KB |
Output is correct |
10 |
Correct |
16 ms |
21888 KB |
Output is correct |
11 |
Correct |
16 ms |
22016 KB |
Output is correct |
12 |
Correct |
16 ms |
21888 KB |
Output is correct |
13 |
Correct |
17 ms |
21888 KB |
Output is correct |
14 |
Correct |
16 ms |
21888 KB |
Output is correct |
15 |
Correct |
16 ms |
21888 KB |
Output is correct |
16 |
Correct |
16 ms |
22016 KB |
Output is correct |
17 |
Correct |
16 ms |
21944 KB |
Output is correct |
18 |
Correct |
16 ms |
21888 KB |
Output is correct |
19 |
Correct |
16 ms |
21888 KB |
Output is correct |
20 |
Correct |
16 ms |
21888 KB |
Output is correct |
21 |
Correct |
16 ms |
22016 KB |
Output is correct |
22 |
Correct |
16 ms |
22016 KB |
Output is correct |
23 |
Correct |
18 ms |
22144 KB |
Output is correct |
24 |
Correct |
19 ms |
22144 KB |
Output is correct |
25 |
Correct |
18 ms |
22144 KB |
Output is correct |
26 |
Correct |
19 ms |
22144 KB |
Output is correct |
27 |
Correct |
18 ms |
22144 KB |
Output is correct |
28 |
Correct |
19 ms |
22144 KB |
Output is correct |
29 |
Correct |
18 ms |
22016 KB |
Output is correct |
30 |
Correct |
19 ms |
22144 KB |
Output is correct |
31 |
Correct |
20 ms |
22144 KB |
Output is correct |
32 |
Correct |
19 ms |
22144 KB |
Output is correct |
33 |
Correct |
20 ms |
22144 KB |
Output is correct |
34 |
Correct |
19 ms |
22144 KB |
Output is correct |
35 |
Correct |
19 ms |
22144 KB |
Output is correct |
36 |
Correct |
20 ms |
22016 KB |
Output is correct |
37 |
Correct |
18 ms |
22016 KB |
Output is correct |
38 |
Correct |
20 ms |
22144 KB |
Output is correct |
39 |
Correct |
20 ms |
22016 KB |
Output is correct |
40 |
Correct |
19 ms |
22144 KB |
Output is correct |
41 |
Correct |
18 ms |
22144 KB |
Output is correct |
42 |
Correct |
18 ms |
22016 KB |
Output is correct |
43 |
Correct |
19 ms |
22144 KB |
Output is correct |
44 |
Correct |
18 ms |
22016 KB |
Output is correct |
45 |
Correct |
534 ms |
44276 KB |
Output is correct |
46 |
Correct |
530 ms |
43896 KB |
Output is correct |
47 |
Correct |
517 ms |
44152 KB |
Output is correct |
48 |
Correct |
528 ms |
44024 KB |
Output is correct |
49 |
Correct |
536 ms |
43900 KB |
Output is correct |
50 |
Correct |
521 ms |
43768 KB |
Output is correct |
51 |
Correct |
518 ms |
44152 KB |
Output is correct |
52 |
Correct |
561 ms |
44280 KB |
Output is correct |
53 |
Correct |
589 ms |
44032 KB |
Output is correct |
54 |
Correct |
493 ms |
46728 KB |
Output is correct |
55 |
Correct |
455 ms |
46456 KB |
Output is correct |
56 |
Correct |
458 ms |
46200 KB |
Output is correct |
57 |
Correct |
452 ms |
46200 KB |
Output is correct |
58 |
Correct |
451 ms |
41152 KB |
Output is correct |
59 |
Correct |
444 ms |
41080 KB |
Output is correct |
60 |
Correct |
327 ms |
46584 KB |
Output is correct |
61 |
Correct |
403 ms |
40916 KB |
Output is correct |
62 |
Correct |
478 ms |
45248 KB |
Output is correct |
63 |
Correct |
405 ms |
40676 KB |
Output is correct |
64 |
Correct |
371 ms |
40620 KB |
Output is correct |
65 |
Correct |
482 ms |
45176 KB |
Output is correct |
66 |
Correct |
394 ms |
40820 KB |
Output is correct |