#include <bits/stdc++.h>
using namespace std;
#define f first
#define se second
#define pb push_back
#define int long long
#define pii pair<int, int>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline T range(T l, T r) {
return uniform_int_distribution <T>(l, r)(rng);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2e5 + 15;
int n, m, a[N];
struct fenw {
int t[N];
inline void inc(int i, int val) {
for(; i <= n; i = (i | (i + 1)))
t[i] += val;
}
inline int get(int i) {
int res = 0;
for(; i >= 1; i = (i & (i + 1)) - 1)
res += t[i];
return res;
}
inline void add(int l, int r, int x) {
inc(l, x);
inc(r + 1, -x);
}
} t;
struct dsu {
int p[N], sz[N], dp[N], ext[N], l[N], r[N];
inline void init() {
for(int i = 0; i < N; ++i)
p[i] = i, sz[i] = 1, dp[i] = 0, ext[i] = 0, l[i] = r[i] = i;
}
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
inline void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
t.add(l[b], r[b], dp[a] + ext[a]);
t.add(l[a], r[a], dp[b] + ext[b]);
Min(l[a], l[b]);
Max(r[a], r[b]);
dp[a] += ext[a] + ext[b] + dp[b];
ext[a] = ext[b] = 0;
p[b] = a;
sz[a] += sz[b];
}
}
} st;
vector <pii> star[N];
vector <int> building[N];
int sum;
main() {
st.init();
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
building[a[i]].pb(i);
}
cin >> m;
for(int i = 1; i <= m; ++i) {
int x, y, c;
cin >> x >> y >> c;
star[y].pb({x, c});
sum += c;
}
for(int y = 1; y <= n; ++y) {
for(pii x : star[y]) {
int p = st.find(x.f);
Max(st.ext[p], t.get(x.f) + x.se - st.dp[p]);
}
for(int x : building[y]) {
if(x > 1 && a[x - 1] <= y)
st.unio(x-1, x);
if(x < n && a[x + 1] <= y)
st.unio(x+1, x);
}
}
for(int i = 2; i <= n; ++i)
st.unio(i, i - 1);
cout << sum - st.dp[st.find(1)] - st.ext[st.find(1)] << endl;
return 0;
}
Compilation message
constellation3.cpp:85:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
16 ms |
19200 KB |
Output is correct |
3 |
Correct |
15 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19196 KB |
Output is correct |
5 |
Correct |
15 ms |
19192 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
16 ms |
19072 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19200 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
11 |
Correct |
20 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
15 ms |
19200 KB |
Output is correct |
14 |
Correct |
16 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
15 ms |
19200 KB |
Output is correct |
17 |
Correct |
15 ms |
19200 KB |
Output is correct |
18 |
Correct |
15 ms |
19200 KB |
Output is correct |
19 |
Correct |
15 ms |
19200 KB |
Output is correct |
20 |
Correct |
17 ms |
19456 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
16 ms |
19200 KB |
Output is correct |
3 |
Correct |
15 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19196 KB |
Output is correct |
5 |
Correct |
15 ms |
19192 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
16 ms |
19072 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19200 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
11 |
Correct |
20 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
15 ms |
19200 KB |
Output is correct |
14 |
Correct |
16 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
15 ms |
19200 KB |
Output is correct |
17 |
Correct |
15 ms |
19200 KB |
Output is correct |
18 |
Correct |
15 ms |
19200 KB |
Output is correct |
19 |
Correct |
15 ms |
19200 KB |
Output is correct |
20 |
Correct |
17 ms |
19456 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
17 ms |
19328 KB |
Output is correct |
24 |
Correct |
16 ms |
19328 KB |
Output is correct |
25 |
Correct |
17 ms |
19328 KB |
Output is correct |
26 |
Correct |
18 ms |
19328 KB |
Output is correct |
27 |
Correct |
17 ms |
19328 KB |
Output is correct |
28 |
Correct |
16 ms |
19328 KB |
Output is correct |
29 |
Correct |
16 ms |
19328 KB |
Output is correct |
30 |
Correct |
17 ms |
19328 KB |
Output is correct |
31 |
Correct |
16 ms |
19328 KB |
Output is correct |
32 |
Correct |
16 ms |
19328 KB |
Output is correct |
33 |
Correct |
16 ms |
19328 KB |
Output is correct |
34 |
Correct |
16 ms |
19328 KB |
Output is correct |
35 |
Correct |
17 ms |
19328 KB |
Output is correct |
36 |
Correct |
16 ms |
19328 KB |
Output is correct |
37 |
Correct |
16 ms |
19320 KB |
Output is correct |
38 |
Correct |
16 ms |
19328 KB |
Output is correct |
39 |
Correct |
16 ms |
19328 KB |
Output is correct |
40 |
Correct |
17 ms |
19328 KB |
Output is correct |
41 |
Correct |
16 ms |
19328 KB |
Output is correct |
42 |
Correct |
16 ms |
19328 KB |
Output is correct |
43 |
Correct |
16 ms |
19328 KB |
Output is correct |
44 |
Correct |
16 ms |
19328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
16 ms |
19200 KB |
Output is correct |
3 |
Correct |
15 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19196 KB |
Output is correct |
5 |
Correct |
15 ms |
19192 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
16 ms |
19072 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19200 KB |
Output is correct |
10 |
Correct |
18 ms |
19200 KB |
Output is correct |
11 |
Correct |
20 ms |
19200 KB |
Output is correct |
12 |
Correct |
15 ms |
19200 KB |
Output is correct |
13 |
Correct |
15 ms |
19200 KB |
Output is correct |
14 |
Correct |
16 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
15 ms |
19200 KB |
Output is correct |
17 |
Correct |
15 ms |
19200 KB |
Output is correct |
18 |
Correct |
15 ms |
19200 KB |
Output is correct |
19 |
Correct |
15 ms |
19200 KB |
Output is correct |
20 |
Correct |
17 ms |
19456 KB |
Output is correct |
21 |
Correct |
15 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19200 KB |
Output is correct |
23 |
Correct |
17 ms |
19328 KB |
Output is correct |
24 |
Correct |
16 ms |
19328 KB |
Output is correct |
25 |
Correct |
17 ms |
19328 KB |
Output is correct |
26 |
Correct |
18 ms |
19328 KB |
Output is correct |
27 |
Correct |
17 ms |
19328 KB |
Output is correct |
28 |
Correct |
16 ms |
19328 KB |
Output is correct |
29 |
Correct |
16 ms |
19328 KB |
Output is correct |
30 |
Correct |
17 ms |
19328 KB |
Output is correct |
31 |
Correct |
16 ms |
19328 KB |
Output is correct |
32 |
Correct |
16 ms |
19328 KB |
Output is correct |
33 |
Correct |
16 ms |
19328 KB |
Output is correct |
34 |
Correct |
16 ms |
19328 KB |
Output is correct |
35 |
Correct |
17 ms |
19328 KB |
Output is correct |
36 |
Correct |
16 ms |
19328 KB |
Output is correct |
37 |
Correct |
16 ms |
19320 KB |
Output is correct |
38 |
Correct |
16 ms |
19328 KB |
Output is correct |
39 |
Correct |
16 ms |
19328 KB |
Output is correct |
40 |
Correct |
17 ms |
19328 KB |
Output is correct |
41 |
Correct |
16 ms |
19328 KB |
Output is correct |
42 |
Correct |
16 ms |
19328 KB |
Output is correct |
43 |
Correct |
16 ms |
19328 KB |
Output is correct |
44 |
Correct |
16 ms |
19328 KB |
Output is correct |
45 |
Correct |
292 ms |
36984 KB |
Output is correct |
46 |
Correct |
253 ms |
36856 KB |
Output is correct |
47 |
Correct |
274 ms |
37368 KB |
Output is correct |
48 |
Correct |
252 ms |
36856 KB |
Output is correct |
49 |
Correct |
248 ms |
36728 KB |
Output is correct |
50 |
Correct |
286 ms |
36728 KB |
Output is correct |
51 |
Correct |
300 ms |
36788 KB |
Output is correct |
52 |
Correct |
281 ms |
37240 KB |
Output is correct |
53 |
Correct |
258 ms |
36968 KB |
Output is correct |
54 |
Correct |
194 ms |
39160 KB |
Output is correct |
55 |
Correct |
194 ms |
39148 KB |
Output is correct |
56 |
Correct |
184 ms |
38652 KB |
Output is correct |
57 |
Correct |
187 ms |
38776 KB |
Output is correct |
58 |
Correct |
190 ms |
34680 KB |
Output is correct |
59 |
Correct |
170 ms |
34552 KB |
Output is correct |
60 |
Correct |
139 ms |
37752 KB |
Output is correct |
61 |
Correct |
140 ms |
33640 KB |
Output is correct |
62 |
Correct |
192 ms |
36728 KB |
Output is correct |
63 |
Correct |
133 ms |
32568 KB |
Output is correct |
64 |
Correct |
138 ms |
33524 KB |
Output is correct |
65 |
Correct |
181 ms |
36472 KB |
Output is correct |
66 |
Correct |
142 ms |
32836 KB |
Output is correct |