#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
struct el {
int y,c;
ll mydp;
bool operator<(const el& o ) const {
return y>o.y;
}
};
struct DS {
ll lazy=0, mn = 0;
// mn is min dp value inside this DS.
// sum is the sum of the currently "bad" stars.
priority_queue<el> pq;
void addStart(int y, int c, ll mydp=0) {
lazy+=c;
pq.push({y,c,mydp+mn-lazy});
mn+=c;
}
void add(int y, int c, ll mydp=0) {
pq.push({y,c,mydp-lazy});
}
void clean(int h) {
// everything below h is safe.
while(!pq.empty() and pq.top().y<=h) {
mn = min(mn,lazy+pq.top().mydp);
pq.pop();
}
}
int size() {
return pq.size();
}
};
void merge(DS& a, DS& b) {
if(a.size()<b.size()) swap(a,b);
a.lazy+=b.mn;
auto oldmn = a.mn;
a.mn+=b.mn;
while(b.size()) {
auto e = b.pq.top();
e.mydp+=b.lazy+oldmn;
a.add(e.y,e.c,e.mydp);
b.pq.pop();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vi a(n);
for(auto& i : a) cin >> i;
#ifdef DEBUG
vector<vi> adj(n);
#else
vector<basic_string<int>> adj(n);
#endif
int root=-1;
{
vi l(n);
for(int i=0;i<n;++i) {
l[i] = i-1;
while(l[i]>=0 and a[l[i]]<a[i]) {
l[i] = l[l[i]];
}
}
vi r(n);
for(int i=n-1;i>=0;--i) {
r[i] = i+1;
while(r[i]<n and a[r[i]]<=a[i]) {
r[i]=r[r[i]];
}
}
for(int i=0;i<n;++i) {
if(l[i]<0 and r[i]>=n) root=i;
else if(r[i]<n and (l[i]<0 or a[l[i]]>a[r[i]])) adj[r[i]].push_back(i);
else adj[l[i]].push_back(i);
}
}
vector<vector<pi>> stars(n);
int m; cin >> m;
while(m--) {
int x,y,c; cin >> x >> y >> c;
--x;
stars[x].push_back({y,c});
}
vector<DS> ds(n);
auto dfs = [&](auto&& self, int at) -> void {
for(auto [y,c] : stars[at]) {
ds[at].addStart(y,c);
}
for(int to : adj[at]) {
self(self,to);
ds[to].clean(a[at]);
merge(ds[at],ds[to]);
}
};
dfs(dfs,root);
auto& dsr = ds[root];
dsr.clean(1e9);
ll ans=dsr.mn;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
636 KB |
Output is correct |
25 |
Correct |
2 ms |
668 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
3 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
636 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
716 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
760 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
712 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
844 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
636 KB |
Output is correct |
25 |
Correct |
2 ms |
668 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
3 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
636 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
716 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
760 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
712 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
844 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
45 |
Correct |
171 ms |
38572 KB |
Output is correct |
46 |
Correct |
157 ms |
38200 KB |
Output is correct |
47 |
Correct |
156 ms |
38348 KB |
Output is correct |
48 |
Correct |
162 ms |
38316 KB |
Output is correct |
49 |
Correct |
153 ms |
37696 KB |
Output is correct |
50 |
Correct |
150 ms |
37608 KB |
Output is correct |
51 |
Correct |
168 ms |
37812 KB |
Output is correct |
52 |
Correct |
169 ms |
38584 KB |
Output is correct |
53 |
Correct |
142 ms |
38176 KB |
Output is correct |
54 |
Correct |
184 ms |
55460 KB |
Output is correct |
55 |
Correct |
200 ms |
50428 KB |
Output is correct |
56 |
Correct |
171 ms |
47436 KB |
Output is correct |
57 |
Correct |
199 ms |
45952 KB |
Output is correct |
58 |
Correct |
181 ms |
50176 KB |
Output is correct |
59 |
Correct |
164 ms |
50068 KB |
Output is correct |
60 |
Correct |
112 ms |
61984 KB |
Output is correct |
61 |
Correct |
187 ms |
46636 KB |
Output is correct |
62 |
Correct |
173 ms |
55324 KB |
Output is correct |
63 |
Correct |
166 ms |
46408 KB |
Output is correct |
64 |
Correct |
172 ms |
44984 KB |
Output is correct |
65 |
Correct |
174 ms |
55644 KB |
Output is correct |
66 |
Correct |
183 ms |
47112 KB |
Output is correct |