#include <bits/stdc++.h>
#define int long long
#define a second
#define b first
using namespace std;
struct Star {
int x, y, c, val;
bool operator <(const Star& other) const{
if(y == other.y)
return x < other.x;
return y < other.y;
}
};
struct Ans {
set<Star> sup;
int add = 0, minInf = 0;
void upd(int h) {
while(!sup.empty()) {
auto it = sup.begin();
if((*it).y > h) return;
minInf = min(minInf, add + (*it).val);
sup.erase(it);
}
}
void merge(Ans other, int h) {
other.upd(h);
if((int)sup.size() < (int)other.sup.size()) {
swap(add, other.add);
swap(sup, other.sup);
swap(minInf, other.minInf);
}
add += other.minInf;
other.add += minInf;
minInf += other.minInf;
for(Star s : other.sup) {
s.val += other.add - add;
sup.insert(s);
}
}
void print() {
cout << add << ' ' << minInf << '\n';
for(Star s : sup)
cout << s.x << ' ' << s.y << ' ' << s.val << '\n';
cout << '\n';
}
};
const int N = 2e5 + 42, INF = 1e18 + 42;
vector<Star> stars[N];
int n, m, h[N], l[N], r[N];
Ans solve(int i) {
if(i == -1) {
Ans ans;
return ans;
}
Ans ans;
for(Star s : stars[i]) {
ans.add += s.c;
ans.sup.insert(s);
}
ans.minInf = ans.add;
ans.merge(solve(l[i]), h[i]);
ans.merge(solve(r[i]), h[i]);
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
cin >> h[i];
cin >> m;
for(int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c;
x--;
stars[x].push_back({x, y, c, -c});
}
int maxi = 0;
deque<int> imax;
for(int i = 0; i < n; i++) {
l[i] = -1;
while(!imax.empty() && h[imax[0]] < h[i]) {
l[i] = imax[0];
imax.pop_front();
}
if(h[i] > h[maxi])
maxi = i;
imax.push_front(i);
}
imax.clear();
for(int i = n-1; i > -1; i--) {
r[i] = -1;
while(!imax.empty() && h[imax[0]] <= h[i]) {
r[i] = imax[0];
imax.pop_front();
}
imax.push_front(i);
}
Ans ans = solve(maxi);
ans.upd(INF);
cout << ans.minInf;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5032 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5032 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5164 KB |
Output is correct |
24 |
Correct |
4 ms |
5212 KB |
Output is correct |
25 |
Correct |
4 ms |
5164 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
6 ms |
5164 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
5 ms |
5548 KB |
Output is correct |
33 |
Correct |
4 ms |
5460 KB |
Output is correct |
34 |
Correct |
5 ms |
5460 KB |
Output is correct |
35 |
Correct |
5 ms |
5424 KB |
Output is correct |
36 |
Correct |
4 ms |
5416 KB |
Output is correct |
37 |
Correct |
4 ms |
5460 KB |
Output is correct |
38 |
Correct |
5 ms |
5724 KB |
Output is correct |
39 |
Correct |
4 ms |
5160 KB |
Output is correct |
40 |
Correct |
6 ms |
5600 KB |
Output is correct |
41 |
Correct |
4 ms |
5212 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
4 ms |
5588 KB |
Output is correct |
44 |
Correct |
5 ms |
5164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5036 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5028 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5032 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5164 KB |
Output is correct |
24 |
Correct |
4 ms |
5212 KB |
Output is correct |
25 |
Correct |
4 ms |
5164 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
6 ms |
5164 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
5 ms |
5548 KB |
Output is correct |
33 |
Correct |
4 ms |
5460 KB |
Output is correct |
34 |
Correct |
5 ms |
5460 KB |
Output is correct |
35 |
Correct |
5 ms |
5424 KB |
Output is correct |
36 |
Correct |
4 ms |
5416 KB |
Output is correct |
37 |
Correct |
4 ms |
5460 KB |
Output is correct |
38 |
Correct |
5 ms |
5724 KB |
Output is correct |
39 |
Correct |
4 ms |
5160 KB |
Output is correct |
40 |
Correct |
6 ms |
5600 KB |
Output is correct |
41 |
Correct |
4 ms |
5212 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
4 ms |
5588 KB |
Output is correct |
44 |
Correct |
5 ms |
5164 KB |
Output is correct |
45 |
Correct |
158 ms |
23668 KB |
Output is correct |
46 |
Correct |
157 ms |
23532 KB |
Output is correct |
47 |
Correct |
156 ms |
23952 KB |
Output is correct |
48 |
Correct |
152 ms |
23508 KB |
Output is correct |
49 |
Correct |
148 ms |
23548 KB |
Output is correct |
50 |
Correct |
173 ms |
23236 KB |
Output is correct |
51 |
Correct |
147 ms |
23476 KB |
Output is correct |
52 |
Correct |
160 ms |
24064 KB |
Output is correct |
53 |
Correct |
151 ms |
23500 KB |
Output is correct |
54 |
Correct |
245 ms |
66128 KB |
Output is correct |
55 |
Correct |
256 ms |
57136 KB |
Output is correct |
56 |
Correct |
232 ms |
51680 KB |
Output is correct |
57 |
Correct |
226 ms |
48588 KB |
Output is correct |
58 |
Correct |
267 ms |
53364 KB |
Output is correct |
59 |
Correct |
224 ms |
53200 KB |
Output is correct |
60 |
Correct |
132 ms |
83252 KB |
Output is correct |
61 |
Correct |
223 ms |
25472 KB |
Output is correct |
62 |
Correct |
243 ms |
71924 KB |
Output is correct |
63 |
Correct |
210 ms |
25424 KB |
Output is correct |
64 |
Correct |
211 ms |
24300 KB |
Output is correct |
65 |
Correct |
245 ms |
71760 KB |
Output is correct |
66 |
Correct |
222 ms |
27252 KB |
Output is correct |