#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 200005;
llint n, m, ofs = 1, sum;
llint h[MAXN], x[MAXN], y[MAXN], cost[MAXN], val[MAXN];
llint mx[MAXN * 4], star[MAXN * 4], t[MAXN * 4], prop[MAXN * 4];
vector <int> v[MAXN];
inline int spoji_mx (int a, int b) {return h[a] >= h[b] ? a : b;}
inline int spoji_star (int a, int b) {return val[a] >= val[b] ? a : b;}
void tour_init () {
for (int i = 0; i < 2*ofs; i++) {
mx[i] = star[i] = n;
}
for (int i = 0; i < n; i++) {
mx[i + ofs] = star[i + ofs] = i;
if (v[i].empty()) val[i] = 0; else val[i] = y[v[i].back()];
}
for (int i = ofs-1; i > 0; i--) {
mx[i] = spoji_mx(mx[i * 2], mx[i * 2 + 1]);
star[i] = spoji_star(star[i * 2], star[i * 2 + 1]);
}
}
void propagate (int x) {
if (prop[x] == 0) return;
if (x < ofs) {
prop[2 * x] += prop[x];
prop[2 * x + 1] += prop[x];
} else {
t[x] += prop[x];
}
prop[x] = 0;
}
int upit_mx (int x, int from, int to, int lo, int hi) {
if (from <= lo && hi <= to) return mx[x];
if (to < lo || hi < from) return n;
return spoji_mx(upit_mx(2 * x, from, to, lo, (lo + hi) / 2), upit_mx(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi));
}
int upit_star (int x, int from, int to, int lo, int hi) {
if (from <= lo && hi <= to) return star[x];
if (to < lo || hi < from) return n;
return spoji_star(upit_star(2 * x, from, to, lo, (lo + hi) / 2), upit_star(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi));
}
llint upit_t (int x, int pos, int lo, int hi) {
propagate(x);
if (lo == hi) return t[x];
if (pos <= (lo + hi) / 2) return upit_t(2 * x, pos, lo, (lo + hi) / 2);
return upit_t(2 * x + 1, pos, (lo + hi) / 2 + 1, hi);
}
void update_star (int pos) {
v[pos].pop_back();
if (v[pos].empty()) val[pos] = 0; else val[pos] = y[v[pos].back()];
pos = (pos + ofs) / 2;
while (pos > 0) {
star[pos] = spoji_star(star[pos * 2], star[pos * 2 + 1]);
pos /= 2;
}
}
void update_t (int x, int from, int to, int lo, int hi, llint d) {
propagate(x);
if (from <= lo && hi <= to) {
prop[x] += d;
propagate(x);
return;
}
if (to < lo || hi < from) return;
update_t(2 * x, from, to, lo, (lo + hi) / 2, d);
update_t(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi, d);
}
bool cmp (int a, int b) {
return y[a] < y[b];
}
llint solve (int a, int b) {
if (a > b) return 0;
//cout << "sad na " << a << " " << b << endl;
int mid = upit_mx(1, a, b, 0, ofs - 1);
vector <int> p;
while (1) {
int pos = upit_star(1, a, b, 0, ofs - 1);
if (val[pos] <= h[mid]) break;
p.push_back(v[pos].back());
update_star(pos);
}
llint lef = solve(a, mid - 1);
llint rig = solve(mid + 1, b);
llint res = lef + rig;
if (a < mid) update_t(1, a, mid - 1, 0, ofs - 1, rig);
if (mid < b) update_t(1, mid + 1, b, 0, ofs - 1, lef);
update_t(1, mid, mid, 0, ofs - 1, lef + rig);
for (auto ind : p) {
llint tmp = upit_t(1, x[ind], 0, ofs - 1) + cost[ind];
res = max(res, tmp);
}
return res;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
while (ofs < n) ofs *= 2;
for (int i = 0; i < n; i++) {
cin >> h[i];
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> cost[i];
x[i]--;
v[x[i]].push_back(i);
sum += cost[i];
}
for (int i = 0; i < n; i++) {
sort(v[i].begin(), v[i].end(), cmp);
}
tour_init();
cout << sum - solve(0, n - 1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
11 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
11 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
11 ms |
5376 KB |
Output is correct |
24 |
Correct |
10 ms |
5376 KB |
Output is correct |
25 |
Correct |
10 ms |
5376 KB |
Output is correct |
26 |
Correct |
11 ms |
5376 KB |
Output is correct |
27 |
Correct |
11 ms |
5376 KB |
Output is correct |
28 |
Correct |
11 ms |
5376 KB |
Output is correct |
29 |
Correct |
10 ms |
5248 KB |
Output is correct |
30 |
Correct |
10 ms |
5376 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
11 ms |
5504 KB |
Output is correct |
33 |
Correct |
11 ms |
5504 KB |
Output is correct |
34 |
Correct |
10 ms |
5504 KB |
Output is correct |
35 |
Correct |
11 ms |
5504 KB |
Output is correct |
36 |
Correct |
10 ms |
5504 KB |
Output is correct |
37 |
Correct |
10 ms |
5504 KB |
Output is correct |
38 |
Correct |
10 ms |
5632 KB |
Output is correct |
39 |
Correct |
10 ms |
5376 KB |
Output is correct |
40 |
Correct |
11 ms |
5504 KB |
Output is correct |
41 |
Correct |
11 ms |
5376 KB |
Output is correct |
42 |
Correct |
11 ms |
5376 KB |
Output is correct |
43 |
Correct |
10 ms |
5504 KB |
Output is correct |
44 |
Correct |
11 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5120 KB |
Output is correct |
14 |
Correct |
11 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
11 ms |
5376 KB |
Output is correct |
24 |
Correct |
10 ms |
5376 KB |
Output is correct |
25 |
Correct |
10 ms |
5376 KB |
Output is correct |
26 |
Correct |
11 ms |
5376 KB |
Output is correct |
27 |
Correct |
11 ms |
5376 KB |
Output is correct |
28 |
Correct |
11 ms |
5376 KB |
Output is correct |
29 |
Correct |
10 ms |
5248 KB |
Output is correct |
30 |
Correct |
10 ms |
5376 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
11 ms |
5504 KB |
Output is correct |
33 |
Correct |
11 ms |
5504 KB |
Output is correct |
34 |
Correct |
10 ms |
5504 KB |
Output is correct |
35 |
Correct |
11 ms |
5504 KB |
Output is correct |
36 |
Correct |
10 ms |
5504 KB |
Output is correct |
37 |
Correct |
10 ms |
5504 KB |
Output is correct |
38 |
Correct |
10 ms |
5632 KB |
Output is correct |
39 |
Correct |
10 ms |
5376 KB |
Output is correct |
40 |
Correct |
11 ms |
5504 KB |
Output is correct |
41 |
Correct |
11 ms |
5376 KB |
Output is correct |
42 |
Correct |
11 ms |
5376 KB |
Output is correct |
43 |
Correct |
10 ms |
5504 KB |
Output is correct |
44 |
Correct |
11 ms |
5376 KB |
Output is correct |
45 |
Correct |
464 ms |
29688 KB |
Output is correct |
46 |
Correct |
471 ms |
29304 KB |
Output is correct |
47 |
Correct |
460 ms |
29432 KB |
Output is correct |
48 |
Correct |
480 ms |
29560 KB |
Output is correct |
49 |
Correct |
471 ms |
29176 KB |
Output is correct |
50 |
Correct |
438 ms |
29176 KB |
Output is correct |
51 |
Correct |
460 ms |
29304 KB |
Output is correct |
52 |
Correct |
474 ms |
29560 KB |
Output is correct |
53 |
Correct |
439 ms |
29432 KB |
Output is correct |
54 |
Correct |
577 ms |
46840 KB |
Output is correct |
55 |
Correct |
572 ms |
42360 KB |
Output is correct |
56 |
Correct |
539 ms |
39800 KB |
Output is correct |
57 |
Correct |
531 ms |
38136 KB |
Output is correct |
58 |
Correct |
516 ms |
39884 KB |
Output is correct |
59 |
Correct |
529 ms |
39672 KB |
Output is correct |
60 |
Correct |
365 ms |
54008 KB |
Output is correct |
61 |
Correct |
518 ms |
29816 KB |
Output is correct |
62 |
Correct |
598 ms |
49016 KB |
Output is correct |
63 |
Correct |
484 ms |
29944 KB |
Output is correct |
64 |
Correct |
466 ms |
29304 KB |
Output is correct |
65 |
Correct |
612 ms |
49272 KB |
Output is correct |
66 |
Correct |
475 ms |
29816 KB |
Output is correct |