#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;
ll t2[MAXN << 2];
pii t[MAXN << 2];
vector<pii> S[MAXN];
ll dp[MAXN], ext[MAXN];
vector<int> adj[MAXN];
pii seg[MAXN];
int n, m, sz;
int arr[MAXN];
void segUpd(int v, int tl, int tr, int l, int r, ll x) {
if (l > r || tl > r || tr < l) {
return;
}
if (l <= tl && tr <= r) {
t2[v] += x;
return;
}
int mid = (tl + tr) >> 1;
int c1 = (v << 1), c2 = (c1 | 1);
segUpd(c1, tl, mid, l, r, x);
segUpd(c2, mid + 1, tr, l, r, x);
}
ll getVal(int v, int tl, int tr, int p) {
if (tl == tr) {
return t2[v];
}
int mid = (tl + tr) >> 1;
int c1 = (v << 1), c2 = (c1 | 1);
if (p <= mid) {
return t2[v] + getVal(c1, tl, mid, p);
}
else {
return t2[v] + getVal(c2, mid + 1, tr, p);
}
}
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = mp(arr[tl], tl);
return;
}
int mid = (tl + tr) >> 1;
int c1 = (v << 1), c2 = (c1 | 1);
build(c1, tl, mid);
build(c2, mid + 1, tr);
t[v] = max(t[c1], t[c2]);
}
pii segMax(int v, int tl, int tr, int l, int r) {
if (l > r || tl > r || tr < l) {
return mp(-1, 0);
}
if (l <= tl && tr <= r) {
return t[v];
}
int mid = (tl + tr) >> 1;
int c1 = (v << 1), c2 = (c1 | 1);
return max(segMax(c1, tl, mid, l, r), segMax(c2, mid + 1, tr, l, r));
}
void del(int v, int tl, int tr, int p) {
if (tl == tr) {
t[v] = mp(-1, tl);
return;
}
int mid = (tl + tr) >> 1;
int c1 = (v << 1), c2 = (c1 | 1);
if (p <= mid) {
del(c1, tl, mid, p);
}
else {
del(c2, mid + 1, tr, p);
}
t[v] = max(t[c1], t[c2]);
}
ll go(int v, int x) {
if (seg[v].fi > x || seg[v].se < x) {
return dp[v];
}
ll ret = 0;
for (int to : adj[v]) {
ret += go(to, x);
}
return ret;
}
int calc(int l, int r, int h = n) {
int v = ++sz;
seg[v] = mp(l, r);
pii tmp = segMax(1, 1, n, l, r);
int mx = tmp.fi;
vector<int> hi;
hi.pb(tmp.se);
hi.pb(l - 1);
hi.pb(r + 1);
del(1, 1, n, tmp.se);
while (1) {
tmp = segMax(1, 1, n, l, r);
if (tmp.fi < mx) {
break;
}
hi.pb(tmp.se);
del(1, 1, n, tmp.se);
}
sort(all(hi));
ll best = 0;
vector<int> opt(r - l + 1, -1);
int last = l - 1;
for (int i = 0; i + 1 < hi.size(); ++i) {
if (i > 0) {
int x = hi[i];
while (!S[x].empty()) {
best = max(best, (ll)S[x].back().se);
S[x].pop_back();
}
}
int cl = hi[i] + 1;
int cr = hi[i + 1] - 1;
if (cl <= cr) {
for (int x = cl; x <= cr; ++x) {
while (!S[x].empty() && S[x].back().fi > mx) {
opt[x - l] = max(opt[x - l], S[x].back().se);
S[x].pop_back();
}
}
int to = calc(cl, cr, mx);
adj[v].pb(to);
dp[v] += dp[to];
for (int x = cl; x <= cr; ++x) {
if (opt[x - l] != -1) {
best = max(best, -dp[to] + opt[x - l] + getVal(1, 1, n, x));
}
}
}
}
for (int to : adj[v]) {
int cl, cr;
tie(cl, cr) = seg[to];
segUpd(1, 1, n, l, cl - 1, ext[to]);
segUpd(1, 1, n, cr + 1, r, ext[to]);
ext[v] += ext[to];
}
dp[v] += best;
ext[v] += best;
//printf("%d %d - %lld %lld\n", l, r, dp[v], ext[v]);
return v;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
ll sum = 0;
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
S[x].pb(mp(y, c));
sum += c;
}
for (int i = 1; i <= n; ++i) {
sort(all(S[i]));
}
build(1, 1, n);
printf("%lld\n", sum - dp[calc(1, n)]);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
constellation3.cpp: In function 'int calc(int, int, int)':
constellation3.cpp:156:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < hi.size(); ++i) {
~~~~~~^~~~~~~~~~~
constellation3.cpp:154:9: warning: unused variable 'last' [-Wunused-variable]
int last = l - 1;
^~~~
constellation3.cpp: In function 'void solve()':
constellation3.cpp:204:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:207:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
constellation3.cpp:211:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
constellation3.cpp:215: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 |
10 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 |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
10112 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9984 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 |
9984 KB |
Output is correct |
22 |
Correct |
7 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 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 |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
10112 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9984 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 |
9984 KB |
Output is correct |
22 |
Correct |
7 ms |
9984 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
13 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
12 ms |
9984 KB |
Output is correct |
31 |
Correct |
12 ms |
9984 KB |
Output is correct |
32 |
Correct |
21 ms |
15104 KB |
Output is correct |
33 |
Correct |
19 ms |
13952 KB |
Output is correct |
34 |
Correct |
18 ms |
13056 KB |
Output is correct |
35 |
Correct |
17 ms |
12672 KB |
Output is correct |
36 |
Correct |
11 ms |
9856 KB |
Output is correct |
37 |
Correct |
12 ms |
9856 KB |
Output is correct |
38 |
Correct |
22 ms |
18176 KB |
Output is correct |
39 |
Correct |
12 ms |
9984 KB |
Output is correct |
40 |
Correct |
19 ms |
14848 KB |
Output is correct |
41 |
Correct |
13 ms |
9984 KB |
Output is correct |
42 |
Correct |
12 ms |
9984 KB |
Output is correct |
43 |
Correct |
22 ms |
14976 KB |
Output is correct |
44 |
Correct |
12 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 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 |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9984 KB |
Output is correct |
11 |
Correct |
10 ms |
9984 KB |
Output is correct |
12 |
Correct |
10 ms |
9856 KB |
Output is correct |
13 |
Correct |
10 ms |
9856 KB |
Output is correct |
14 |
Correct |
10 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
10112 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
10 ms |
9984 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 |
9984 KB |
Output is correct |
22 |
Correct |
7 ms |
9984 KB |
Output is correct |
23 |
Correct |
13 ms |
9984 KB |
Output is correct |
24 |
Correct |
13 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
12 ms |
9984 KB |
Output is correct |
31 |
Correct |
12 ms |
9984 KB |
Output is correct |
32 |
Correct |
21 ms |
15104 KB |
Output is correct |
33 |
Correct |
19 ms |
13952 KB |
Output is correct |
34 |
Correct |
18 ms |
13056 KB |
Output is correct |
35 |
Correct |
17 ms |
12672 KB |
Output is correct |
36 |
Correct |
11 ms |
9856 KB |
Output is correct |
37 |
Correct |
12 ms |
9856 KB |
Output is correct |
38 |
Correct |
22 ms |
18176 KB |
Output is correct |
39 |
Correct |
12 ms |
9984 KB |
Output is correct |
40 |
Correct |
19 ms |
14848 KB |
Output is correct |
41 |
Correct |
13 ms |
9984 KB |
Output is correct |
42 |
Correct |
12 ms |
9984 KB |
Output is correct |
43 |
Correct |
22 ms |
14976 KB |
Output is correct |
44 |
Correct |
12 ms |
9856 KB |
Output is correct |
45 |
Correct |
424 ms |
33452 KB |
Output is correct |
46 |
Correct |
446 ms |
32876 KB |
Output is correct |
47 |
Correct |
420 ms |
34044 KB |
Output is correct |
48 |
Correct |
433 ms |
33036 KB |
Output is correct |
49 |
Correct |
406 ms |
33548 KB |
Output is correct |
50 |
Correct |
433 ms |
33048 KB |
Output is correct |
51 |
Correct |
416 ms |
33368 KB |
Output is correct |
52 |
Correct |
438 ms |
34344 KB |
Output is correct |
53 |
Correct |
429 ms |
33252 KB |
Output is correct |
54 |
Runtime error |
1374 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
55 |
Halted |
0 ms |
0 KB |
- |