#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]);
c[0] = 0;
ans[0].clear();
ans[0][1] = 0;
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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
15 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
14 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 |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
17 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
15 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
14 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 |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
17 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
18 ms |
14912 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
15 ms |
14720 KB |
Output is correct |
26 |
Correct |
15 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 |
15 ms |
14720 KB |
Output is correct |
30 |
Correct |
15 ms |
14720 KB |
Output is correct |
31 |
Correct |
16 ms |
14720 KB |
Output is correct |
32 |
Correct |
14 ms |
14976 KB |
Output is correct |
33 |
Correct |
19 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 |
14 ms |
14976 KB |
Output is correct |
37 |
Correct |
15 ms |
14848 KB |
Output is correct |
38 |
Correct |
14 ms |
15104 KB |
Output is correct |
39 |
Correct |
16 ms |
14848 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 KB |
Output is correct |
41 |
Correct |
15 ms |
14720 KB |
Output is correct |
42 |
Correct |
16 ms |
14848 KB |
Output is correct |
43 |
Correct |
17 ms |
14976 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
15 ms |
14464 KB |
Output is correct |
13 |
Correct |
13 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14592 KB |
Output is correct |
15 |
Correct |
14 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 |
14592 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
17 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14592 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
18 ms |
14912 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
15 ms |
14720 KB |
Output is correct |
26 |
Correct |
15 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 |
15 ms |
14720 KB |
Output is correct |
30 |
Correct |
15 ms |
14720 KB |
Output is correct |
31 |
Correct |
16 ms |
14720 KB |
Output is correct |
32 |
Correct |
14 ms |
14976 KB |
Output is correct |
33 |
Correct |
19 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 |
14 ms |
14976 KB |
Output is correct |
37 |
Correct |
15 ms |
14848 KB |
Output is correct |
38 |
Correct |
14 ms |
15104 KB |
Output is correct |
39 |
Correct |
16 ms |
14848 KB |
Output is correct |
40 |
Correct |
15 ms |
14976 KB |
Output is correct |
41 |
Correct |
15 ms |
14720 KB |
Output is correct |
42 |
Correct |
16 ms |
14848 KB |
Output is correct |
43 |
Correct |
17 ms |
14976 KB |
Output is correct |
44 |
Correct |
15 ms |
14720 KB |
Output is correct |
45 |
Correct |
237 ms |
41720 KB |
Output is correct |
46 |
Correct |
229 ms |
41336 KB |
Output is correct |
47 |
Correct |
251 ms |
41208 KB |
Output is correct |
48 |
Correct |
245 ms |
41612 KB |
Output is correct |
49 |
Correct |
257 ms |
40956 KB |
Output is correct |
50 |
Correct |
240 ms |
40788 KB |
Output is correct |
51 |
Correct |
237 ms |
41208 KB |
Output is correct |
52 |
Correct |
237 ms |
41464 KB |
Output is correct |
53 |
Correct |
249 ms |
41208 KB |
Output is correct |
54 |
Correct |
242 ms |
63256 KB |
Output is correct |
55 |
Correct |
240 ms |
57452 KB |
Output is correct |
56 |
Correct |
239 ms |
54256 KB |
Output is correct |
57 |
Correct |
239 ms |
51956 KB |
Output is correct |
58 |
Correct |
207 ms |
53404 KB |
Output is correct |
59 |
Correct |
208 ms |
52972 KB |
Output is correct |
60 |
Correct |
289 ms |
75384 KB |
Output is correct |
61 |
Correct |
240 ms |
48248 KB |
Output is correct |
62 |
Correct |
328 ms |
67564 KB |
Output is correct |
63 |
Correct |
232 ms |
46200 KB |
Output is correct |
64 |
Correct |
247 ms |
47352 KB |
Output is correct |
65 |
Correct |
325 ms |
68332 KB |
Output is correct |
66 |
Correct |
244 ms |
46712 KB |
Output is correct |