#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=2e5+5;
ll n, m, h[nx], x, y, c, sm, l[nx], r[nx], rt, res, lz[nx];
vector<pair<ll, ll>> d[nx];
set<pair<ll, ll>> dp[nx];
stack<ll> s;
void add(int idx, pair<ll, ll> vl)
{
auto itr=dp[idx].lower_bound(make_pair(vl.first, LLONG_MIN));
if (itr->first==vl.first) vl.second=max(vl.second, itr->second), dp[idx].erase(itr);
itr=dp[idx].lower_bound(make_pair(vl.first, LLONG_MIN));
if (itr!=dp[idx].begin()&&prev(itr)->second>=vl.second) return;
while (itr!=dp[idx].end()&&itr->second<=vl.second) itr=dp[idx].erase(itr);
dp[idx].insert(vl);
}
void show(int idx)
{
cout<<"show "<<idx<<'\n';
for (auto [x, y]:dp[idx]) cout<<x<<' '<<y+lz[idx]<<'\n';
}
void solve(int u)
{
if (l[u]) solve(l[u]);
if (r[u]) solve(r[u]);
ll vl=0, vr=0;
if (l[u]||r[u])
{
if (!l[u]) swap(l[u], r[u]);
if (dp[l[u]].size()<dp[r[u]].size()) swap(l[u], r[u]);
while (!dp[l[u]].empty()&&dp[l[u]].begin()->first<=h[u]) vl=dp[l[u]].begin()->second+lz[l[u]], dp[l[u]].erase(dp[l[u]].begin());
while (!dp[r[u]].empty()&&dp[r[u]].begin()->first<=h[u]) vr=dp[r[u]].begin()->second+lz[r[u]], dp[r[u]].erase(dp[r[u]].begin());
//cout<<"debug "<<u<<' '<<vl<<' '<<vr<<'\n';
//cout<<"l "<<u<<' '<<l[u]<<' '<<r[u]<<'\n';
swap(dp[l[u]], dp[u]);
lz[u]=lz[l[u]];
lz[u]+=vr;
add(u, {h[u], vl+vr-lz[u]});
for (auto [y, c]:dp[r[u]]) add(u, {y, c+lz[r[u]]+vl-lz[u]});
}
for (auto [y, c]:d[u]) add(u, {y, c+vl+vr-lz[u]});
//show(u);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
cin>>m;
for (int i=1; i<=m; i++) cin>>x>>y>>c, d[x].push_back({y, c}), sm+=c;
for (int i=1; i<=n; i++)
{
while (!s.empty()&&h[s.top()]<h[i]) l[i]=s.top(), s.pop();
if (!s.empty()) r[s.top()]=i;
else rt=i;
s.push(i);
}
solve(rt);
//cout<<"here "<<rt<<' '<<prev(dp[rt].end())->second<<'\n';
if (dp[rt].empty()) cout<<sm;
else cout<<sm-(prev(dp[rt].end())->second+lz[rt]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19064 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19120 KB |
Output is correct |
5 |
Correct |
4 ms |
19024 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
3 ms |
19024 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
3 ms |
19252 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
3 ms |
19024 KB |
Output is correct |
14 |
Correct |
5 ms |
19024 KB |
Output is correct |
15 |
Correct |
4 ms |
19024 KB |
Output is correct |
16 |
Correct |
3 ms |
19024 KB |
Output is correct |
17 |
Correct |
3 ms |
19024 KB |
Output is correct |
18 |
Correct |
3 ms |
19024 KB |
Output is correct |
19 |
Correct |
3 ms |
19024 KB |
Output is correct |
20 |
Correct |
4 ms |
19024 KB |
Output is correct |
21 |
Correct |
3 ms |
19024 KB |
Output is correct |
22 |
Correct |
3 ms |
19024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19064 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19120 KB |
Output is correct |
5 |
Correct |
4 ms |
19024 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
3 ms |
19024 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
3 ms |
19252 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
3 ms |
19024 KB |
Output is correct |
14 |
Correct |
5 ms |
19024 KB |
Output is correct |
15 |
Correct |
4 ms |
19024 KB |
Output is correct |
16 |
Correct |
3 ms |
19024 KB |
Output is correct |
17 |
Correct |
3 ms |
19024 KB |
Output is correct |
18 |
Correct |
3 ms |
19024 KB |
Output is correct |
19 |
Correct |
3 ms |
19024 KB |
Output is correct |
20 |
Correct |
4 ms |
19024 KB |
Output is correct |
21 |
Correct |
3 ms |
19024 KB |
Output is correct |
22 |
Correct |
3 ms |
19024 KB |
Output is correct |
23 |
Correct |
4 ms |
19024 KB |
Output is correct |
24 |
Correct |
4 ms |
19024 KB |
Output is correct |
25 |
Correct |
4 ms |
19196 KB |
Output is correct |
26 |
Correct |
4 ms |
19024 KB |
Output is correct |
27 |
Correct |
5 ms |
19024 KB |
Output is correct |
28 |
Correct |
4 ms |
19024 KB |
Output is correct |
29 |
Correct |
4 ms |
19024 KB |
Output is correct |
30 |
Correct |
4 ms |
19192 KB |
Output is correct |
31 |
Correct |
4 ms |
19024 KB |
Output is correct |
32 |
Correct |
4 ms |
19324 KB |
Output is correct |
33 |
Correct |
5 ms |
19280 KB |
Output is correct |
34 |
Correct |
4 ms |
19280 KB |
Output is correct |
35 |
Correct |
5 ms |
19076 KB |
Output is correct |
36 |
Correct |
4 ms |
19192 KB |
Output is correct |
37 |
Correct |
4 ms |
19280 KB |
Output is correct |
38 |
Correct |
4 ms |
19280 KB |
Output is correct |
39 |
Correct |
4 ms |
19024 KB |
Output is correct |
40 |
Correct |
4 ms |
19280 KB |
Output is correct |
41 |
Correct |
4 ms |
19024 KB |
Output is correct |
42 |
Correct |
5 ms |
19164 KB |
Output is correct |
43 |
Correct |
5 ms |
19368 KB |
Output is correct |
44 |
Correct |
4 ms |
19024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19064 KB |
Output is correct |
3 |
Correct |
4 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19120 KB |
Output is correct |
5 |
Correct |
4 ms |
19024 KB |
Output is correct |
6 |
Correct |
3 ms |
19024 KB |
Output is correct |
7 |
Correct |
3 ms |
19024 KB |
Output is correct |
8 |
Correct |
3 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
3 ms |
19252 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
3 ms |
19024 KB |
Output is correct |
14 |
Correct |
5 ms |
19024 KB |
Output is correct |
15 |
Correct |
4 ms |
19024 KB |
Output is correct |
16 |
Correct |
3 ms |
19024 KB |
Output is correct |
17 |
Correct |
3 ms |
19024 KB |
Output is correct |
18 |
Correct |
3 ms |
19024 KB |
Output is correct |
19 |
Correct |
3 ms |
19024 KB |
Output is correct |
20 |
Correct |
4 ms |
19024 KB |
Output is correct |
21 |
Correct |
3 ms |
19024 KB |
Output is correct |
22 |
Correct |
3 ms |
19024 KB |
Output is correct |
23 |
Correct |
4 ms |
19024 KB |
Output is correct |
24 |
Correct |
4 ms |
19024 KB |
Output is correct |
25 |
Correct |
4 ms |
19196 KB |
Output is correct |
26 |
Correct |
4 ms |
19024 KB |
Output is correct |
27 |
Correct |
5 ms |
19024 KB |
Output is correct |
28 |
Correct |
4 ms |
19024 KB |
Output is correct |
29 |
Correct |
4 ms |
19024 KB |
Output is correct |
30 |
Correct |
4 ms |
19192 KB |
Output is correct |
31 |
Correct |
4 ms |
19024 KB |
Output is correct |
32 |
Correct |
4 ms |
19324 KB |
Output is correct |
33 |
Correct |
5 ms |
19280 KB |
Output is correct |
34 |
Correct |
4 ms |
19280 KB |
Output is correct |
35 |
Correct |
5 ms |
19076 KB |
Output is correct |
36 |
Correct |
4 ms |
19192 KB |
Output is correct |
37 |
Correct |
4 ms |
19280 KB |
Output is correct |
38 |
Correct |
4 ms |
19280 KB |
Output is correct |
39 |
Correct |
4 ms |
19024 KB |
Output is correct |
40 |
Correct |
4 ms |
19280 KB |
Output is correct |
41 |
Correct |
4 ms |
19024 KB |
Output is correct |
42 |
Correct |
5 ms |
19164 KB |
Output is correct |
43 |
Correct |
5 ms |
19368 KB |
Output is correct |
44 |
Correct |
4 ms |
19024 KB |
Output is correct |
45 |
Correct |
118 ms |
33104 KB |
Output is correct |
46 |
Correct |
108 ms |
32920 KB |
Output is correct |
47 |
Correct |
111 ms |
33356 KB |
Output is correct |
48 |
Correct |
104 ms |
32844 KB |
Output is correct |
49 |
Correct |
86 ms |
32964 KB |
Output is correct |
50 |
Correct |
87 ms |
32876 KB |
Output is correct |
51 |
Correct |
83 ms |
32844 KB |
Output is correct |
52 |
Correct |
103 ms |
33356 KB |
Output is correct |
53 |
Correct |
87 ms |
33100 KB |
Output is correct |
54 |
Correct |
103 ms |
47192 KB |
Output is correct |
55 |
Correct |
104 ms |
44880 KB |
Output is correct |
56 |
Correct |
110 ms |
43240 KB |
Output is correct |
57 |
Correct |
126 ms |
42420 KB |
Output is correct |
58 |
Correct |
109 ms |
40780 KB |
Output is correct |
59 |
Correct |
91 ms |
40744 KB |
Output is correct |
60 |
Correct |
79 ms |
48716 KB |
Output is correct |
61 |
Correct |
115 ms |
41804 KB |
Output is correct |
62 |
Correct |
201 ms |
50344 KB |
Output is correct |
63 |
Correct |
137 ms |
40068 KB |
Output is correct |
64 |
Correct |
154 ms |
41292 KB |
Output is correct |
65 |
Correct |
206 ms |
50260 KB |
Output is correct |
66 |
Correct |
139 ms |
41036 KB |
Output is correct |