#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;
L = c[l] + (--ans[l].upper_bound(a[v])) -> ss;
R = c[r] + (--ans[r].upper_bound(a[v])) -> 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;
ans[0].clear();
ans[0][1] = 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 |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 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 |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14512 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 |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 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 |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 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 |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14512 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 |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 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 |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
15 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 |
15 ms |
14720 KB |
Output is correct |
29 |
Correct |
14 ms |
14720 KB |
Output is correct |
30 |
Correct |
14 ms |
14720 KB |
Output is correct |
31 |
Correct |
14 ms |
14720 KB |
Output is correct |
32 |
Correct |
14 ms |
14976 KB |
Output is correct |
33 |
Correct |
15 ms |
14976 KB |
Output is correct |
34 |
Correct |
15 ms |
14848 KB |
Output is correct |
35 |
Correct |
15 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14848 KB |
Output is correct |
37 |
Correct |
14 ms |
14848 KB |
Output is correct |
38 |
Correct |
14 ms |
14976 KB |
Output is correct |
39 |
Correct |
16 ms |
14720 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 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 |
14976 KB |
Output is correct |
44 |
Correct |
14 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 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 |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14512 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 |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14592 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14592 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 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 |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
15 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 |
15 ms |
14720 KB |
Output is correct |
29 |
Correct |
14 ms |
14720 KB |
Output is correct |
30 |
Correct |
14 ms |
14720 KB |
Output is correct |
31 |
Correct |
14 ms |
14720 KB |
Output is correct |
32 |
Correct |
14 ms |
14976 KB |
Output is correct |
33 |
Correct |
15 ms |
14976 KB |
Output is correct |
34 |
Correct |
15 ms |
14848 KB |
Output is correct |
35 |
Correct |
15 ms |
14848 KB |
Output is correct |
36 |
Correct |
15 ms |
14848 KB |
Output is correct |
37 |
Correct |
14 ms |
14848 KB |
Output is correct |
38 |
Correct |
14 ms |
14976 KB |
Output is correct |
39 |
Correct |
16 ms |
14720 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 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 |
14976 KB |
Output is correct |
44 |
Correct |
14 ms |
14720 KB |
Output is correct |
45 |
Correct |
214 ms |
40056 KB |
Output is correct |
46 |
Correct |
213 ms |
39672 KB |
Output is correct |
47 |
Correct |
222 ms |
39672 KB |
Output is correct |
48 |
Correct |
213 ms |
39932 KB |
Output is correct |
49 |
Correct |
216 ms |
39416 KB |
Output is correct |
50 |
Correct |
212 ms |
39416 KB |
Output is correct |
51 |
Correct |
215 ms |
39544 KB |
Output is correct |
52 |
Correct |
235 ms |
39928 KB |
Output is correct |
53 |
Correct |
218 ms |
39672 KB |
Output is correct |
54 |
Correct |
213 ms |
61676 KB |
Output is correct |
55 |
Correct |
237 ms |
55920 KB |
Output is correct |
56 |
Correct |
209 ms |
52720 KB |
Output is correct |
57 |
Correct |
217 ms |
50416 KB |
Output is correct |
58 |
Correct |
188 ms |
51816 KB |
Output is correct |
59 |
Correct |
191 ms |
51432 KB |
Output is correct |
60 |
Correct |
266 ms |
74004 KB |
Output is correct |
61 |
Correct |
237 ms |
46712 KB |
Output is correct |
62 |
Correct |
318 ms |
66028 KB |
Output is correct |
63 |
Correct |
233 ms |
44664 KB |
Output is correct |
64 |
Correct |
236 ms |
45816 KB |
Output is correct |
65 |
Correct |
321 ms |
66684 KB |
Output is correct |
66 |
Correct |
236 ms |
45176 KB |
Output is correct |