#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 200005
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
int a[N];
int l[N], r[N];
vector<pair<int, int> > st[N], tmp;
map<int, ll> ans[N];
ll c[N];
ll res = 0;
void insert(int v, int y, ll s){
if((--ans[v].upper_bound(y)) -> ss >= s) return;
ans[v][y] = s;
auto it = ans[v].upper_bound(y);
while(it != ans[v].end() && it -> ss <= s)
it = ans[v].erase(it);
}
void dfs(int v){
int l = ::l[v];
int r = ::r[v];
if(l)
dfs(l);
if(r)
dfs(r);
ll L, R;
auto it = --ans[l].upper_bound(a[v]);
while(ans[l].begin() != it)
ans[l].erase(ans[l].begin());
L = c[l] + it -> ss;
it = --ans[r].upper_bound(a[v]);
while(ans[r].begin() != it)
ans[r].erase(ans[r].begin());
R = c[r] + it -> ss;
c[l] += R;
c[r] += L;
if(ans[l].size() > ans[r].size())
swap(l, r);
swap(ans[r], ans[v]);
swap(c[r], c[v]);
if(!l)
c[0] = 0;
else
for(auto& x : ans[l])
insert(v, x.ff, x.ss + c[l] - c[v]);
for(auto& x : st[v])
insert(v, x.ff, x.ss + L + R - c[v]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
ans[0][1] = 0;
for(int i = 1; i <= n; ++i){
ans[i][1] = 0;
cin>>a[i];
while(!tmp.empty() && tmp.back().ff < a[i]){
l[i] = tmp.back().ss;
tmp.pop_back();
}
if(!tmp.empty())
r[tmp.back().ss] = i;
tmp.push_back({a[i], i});
}
int q;
cin>>q;
while(q--){
int x, y, s;
cin>>x>>y>>s;
res += s;
st[x].push_back({y, s});
}
dfs(tmp[0].ss);
res -= c[tmp[0].ss] + (--ans[tmp[0].ss].end()) -> ss;
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14532 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14532 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14592 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
14 ms |
14720 KB |
Output is correct |
26 |
Correct |
14 ms |
14720 KB |
Output is correct |
27 |
Correct |
15 ms |
14720 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
23 ms |
14720 KB |
Output is correct |
30 |
Correct |
15 ms |
14720 KB |
Output is correct |
31 |
Correct |
15 ms |
14592 KB |
Output is correct |
32 |
Correct |
15 ms |
14848 KB |
Output is correct |
33 |
Correct |
14 ms |
14848 KB |
Output is correct |
34 |
Correct |
14 ms |
14720 KB |
Output is correct |
35 |
Correct |
15 ms |
14720 KB |
Output is correct |
36 |
Correct |
14 ms |
14720 KB |
Output is correct |
37 |
Correct |
17 ms |
14968 KB |
Output is correct |
38 |
Correct |
14 ms |
14848 KB |
Output is correct |
39 |
Correct |
15 ms |
14720 KB |
Output is correct |
40 |
Correct |
14 ms |
14848 KB |
Output is correct |
41 |
Correct |
14 ms |
14720 KB |
Output is correct |
42 |
Correct |
14 ms |
14720 KB |
Output is correct |
43 |
Correct |
15 ms |
14848 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14532 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14592 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
14 ms |
14720 KB |
Output is correct |
26 |
Correct |
14 ms |
14720 KB |
Output is correct |
27 |
Correct |
15 ms |
14720 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
23 ms |
14720 KB |
Output is correct |
30 |
Correct |
15 ms |
14720 KB |
Output is correct |
31 |
Correct |
15 ms |
14592 KB |
Output is correct |
32 |
Correct |
15 ms |
14848 KB |
Output is correct |
33 |
Correct |
14 ms |
14848 KB |
Output is correct |
34 |
Correct |
14 ms |
14720 KB |
Output is correct |
35 |
Correct |
15 ms |
14720 KB |
Output is correct |
36 |
Correct |
14 ms |
14720 KB |
Output is correct |
37 |
Correct |
17 ms |
14968 KB |
Output is correct |
38 |
Correct |
14 ms |
14848 KB |
Output is correct |
39 |
Correct |
15 ms |
14720 KB |
Output is correct |
40 |
Correct |
14 ms |
14848 KB |
Output is correct |
41 |
Correct |
14 ms |
14720 KB |
Output is correct |
42 |
Correct |
14 ms |
14720 KB |
Output is correct |
43 |
Correct |
15 ms |
14848 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
45 |
Correct |
210 ms |
36216 KB |
Output is correct |
46 |
Correct |
193 ms |
35832 KB |
Output is correct |
47 |
Correct |
197 ms |
35704 KB |
Output is correct |
48 |
Correct |
197 ms |
36132 KB |
Output is correct |
49 |
Correct |
214 ms |
35424 KB |
Output is correct |
50 |
Correct |
190 ms |
35448 KB |
Output is correct |
51 |
Correct |
198 ms |
35576 KB |
Output is correct |
52 |
Correct |
206 ms |
36088 KB |
Output is correct |
53 |
Correct |
196 ms |
35832 KB |
Output is correct |
54 |
Correct |
204 ms |
53352 KB |
Output is correct |
55 |
Correct |
218 ms |
49904 KB |
Output is correct |
56 |
Correct |
208 ms |
47856 KB |
Output is correct |
57 |
Correct |
204 ms |
46452 KB |
Output is correct |
58 |
Correct |
186 ms |
45928 KB |
Output is correct |
59 |
Correct |
184 ms |
45548 KB |
Output is correct |
60 |
Correct |
150 ms |
55160 KB |
Output is correct |
61 |
Correct |
235 ms |
45672 KB |
Output is correct |
62 |
Correct |
290 ms |
56300 KB |
Output is correct |
63 |
Correct |
213 ms |
43768 KB |
Output is correct |
64 |
Correct |
229 ms |
44968 KB |
Output is correct |
65 |
Correct |
282 ms |
56680 KB |
Output is correct |
66 |
Correct |
223 ms |
44408 KB |
Output is correct |