#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair<ll,ll>
#define ll long long
#define sz(x) (ll)x.size()
using namespace std;
const int mxn=2e5+5;
pii t[20][mxn];
int up[20][mxn];
int lv[mxn]{0},rv[mxn]{0};
int ti[mxn],to[mxn],cur=0;
ll fw[mxn]{0};
ll dp[mxn]{0};
vector<pii>g[mxn];
void add(int i,ll amt){
for(;i<mxn;i+=i&-i)fw[i]+=amt;
}
ll qr(int i,ll rs=0){
for(;i;i-=i&-i)rs+=fw[i];return rs;
}
int get(int l,int r){
int lg=log2(r-l+1);
return max(t[lg][l],t[lg][r-(1<<lg)+1]).s;
}
int build(int l,int r){
if(l==r)return l;
int m=get(l,r);
if(m>l)lv[m]=build(l,m-1),up[0][lv[m]]=m;
if(m<r)rv[m]=build(m+1,r),up[0][rv[m]]=m;
return m;
}
void dfs(int u){
ti[u]=++cur;
if(lv[u])dfs(lv[u]);
if(rv[u])dfs(rv[u]);
to[u]=cur;
if(lv[u]){
add(ti[u],dp[lv[u]]);
add(to[u]+1,-dp[lv[u]]);
add(ti[lv[u]],-dp[lv[u]]);
add(to[lv[u]]+1,dp[lv[u]]);
}
if(rv[u]){
add(ti[u],dp[rv[u]]);
add(to[u]+1,-dp[rv[u]]);
add(ti[rv[u]],-dp[rv[u]]);
add(to[rv[u]]+1,dp[rv[u]]);
}dp[u]=dp[lv[u]]+dp[rv[u]];
for(auto v:g[u]){
dp[u]=max(dp[u],v.s+qr(ti[v.f]));
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
int a[n+1];for(int i=1;i<=n;i++)cin>>a[i],t[0][i]={a[i],i};
for(int j=1;j<20;j++)for(int i=1;i+(1<<(j-1))<=n;i++)t[j][i]=max(t[j-1][i],t[j-1][i+(1<<(j-1))]);
int root=build(1,n);up[0][root]=root;
for(int j=1;j<20;j++)for(int i=1;i<=n;i++)up[j][i]=up[j-1][up[j-1][i]];
int m;cin>>m;ll tt=0;
for(int i=1;i<=m;i++){
int x,y,c;cin>>x>>y>>c;int u=x;
for(int j=19;j>=0;j--)if(a[up[j][u]]<y)u=up[j][u];
g[u].pb({x,c});tt+=c;
}dfs(root);cout<<tt-dp[root];
}
Compilation message
constellation3.cpp: In function 'long long int qr(int, long long int)':
constellation3.cpp:21:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
21 | for(;i;i-=i&-i)rs+=fw[i];return rs;
| ^~~
constellation3.cpp:21:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
21 | for(;i;i-=i&-i)rs+=fw[i];return rs;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
43608 KB |
Output is correct |
2 |
Correct |
8 ms |
43608 KB |
Output is correct |
3 |
Correct |
7 ms |
43612 KB |
Output is correct |
4 |
Correct |
6 ms |
43612 KB |
Output is correct |
5 |
Correct |
6 ms |
43608 KB |
Output is correct |
6 |
Correct |
6 ms |
43624 KB |
Output is correct |
7 |
Correct |
6 ms |
43612 KB |
Output is correct |
8 |
Correct |
6 ms |
43608 KB |
Output is correct |
9 |
Correct |
6 ms |
43612 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
8 ms |
43612 KB |
Output is correct |
12 |
Correct |
7 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
43612 KB |
Output is correct |
14 |
Correct |
7 ms |
43608 KB |
Output is correct |
15 |
Correct |
6 ms |
43612 KB |
Output is correct |
16 |
Correct |
6 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
43612 KB |
Output is correct |
18 |
Correct |
6 ms |
43612 KB |
Output is correct |
19 |
Correct |
6 ms |
43612 KB |
Output is correct |
20 |
Correct |
6 ms |
43612 KB |
Output is correct |
21 |
Correct |
6 ms |
43612 KB |
Output is correct |
22 |
Correct |
6 ms |
43612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
43608 KB |
Output is correct |
2 |
Correct |
8 ms |
43608 KB |
Output is correct |
3 |
Correct |
7 ms |
43612 KB |
Output is correct |
4 |
Correct |
6 ms |
43612 KB |
Output is correct |
5 |
Correct |
6 ms |
43608 KB |
Output is correct |
6 |
Correct |
6 ms |
43624 KB |
Output is correct |
7 |
Correct |
6 ms |
43612 KB |
Output is correct |
8 |
Correct |
6 ms |
43608 KB |
Output is correct |
9 |
Correct |
6 ms |
43612 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
8 ms |
43612 KB |
Output is correct |
12 |
Correct |
7 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
43612 KB |
Output is correct |
14 |
Correct |
7 ms |
43608 KB |
Output is correct |
15 |
Correct |
6 ms |
43612 KB |
Output is correct |
16 |
Correct |
6 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
43612 KB |
Output is correct |
18 |
Correct |
6 ms |
43612 KB |
Output is correct |
19 |
Correct |
6 ms |
43612 KB |
Output is correct |
20 |
Correct |
6 ms |
43612 KB |
Output is correct |
21 |
Correct |
6 ms |
43612 KB |
Output is correct |
22 |
Correct |
6 ms |
43612 KB |
Output is correct |
23 |
Correct |
8 ms |
50012 KB |
Output is correct |
24 |
Correct |
8 ms |
50012 KB |
Output is correct |
25 |
Correct |
8 ms |
50012 KB |
Output is correct |
26 |
Correct |
8 ms |
50008 KB |
Output is correct |
27 |
Correct |
9 ms |
50008 KB |
Output is correct |
28 |
Correct |
9 ms |
50012 KB |
Output is correct |
29 |
Correct |
8 ms |
50008 KB |
Output is correct |
30 |
Correct |
8 ms |
50012 KB |
Output is correct |
31 |
Correct |
8 ms |
50012 KB |
Output is correct |
32 |
Correct |
8 ms |
49872 KB |
Output is correct |
33 |
Correct |
8 ms |
50012 KB |
Output is correct |
34 |
Correct |
8 ms |
49812 KB |
Output is correct |
35 |
Correct |
8 ms |
50016 KB |
Output is correct |
36 |
Correct |
7 ms |
50012 KB |
Output is correct |
37 |
Correct |
8 ms |
49888 KB |
Output is correct |
38 |
Correct |
7 ms |
50008 KB |
Output is correct |
39 |
Correct |
8 ms |
50012 KB |
Output is correct |
40 |
Correct |
8 ms |
50264 KB |
Output is correct |
41 |
Correct |
8 ms |
50012 KB |
Output is correct |
42 |
Correct |
8 ms |
49756 KB |
Output is correct |
43 |
Correct |
8 ms |
50012 KB |
Output is correct |
44 |
Correct |
7 ms |
50012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
43608 KB |
Output is correct |
2 |
Correct |
8 ms |
43608 KB |
Output is correct |
3 |
Correct |
7 ms |
43612 KB |
Output is correct |
4 |
Correct |
6 ms |
43612 KB |
Output is correct |
5 |
Correct |
6 ms |
43608 KB |
Output is correct |
6 |
Correct |
6 ms |
43624 KB |
Output is correct |
7 |
Correct |
6 ms |
43612 KB |
Output is correct |
8 |
Correct |
6 ms |
43608 KB |
Output is correct |
9 |
Correct |
6 ms |
43612 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
8 ms |
43612 KB |
Output is correct |
12 |
Correct |
7 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
43612 KB |
Output is correct |
14 |
Correct |
7 ms |
43608 KB |
Output is correct |
15 |
Correct |
6 ms |
43612 KB |
Output is correct |
16 |
Correct |
6 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
43612 KB |
Output is correct |
18 |
Correct |
6 ms |
43612 KB |
Output is correct |
19 |
Correct |
6 ms |
43612 KB |
Output is correct |
20 |
Correct |
6 ms |
43612 KB |
Output is correct |
21 |
Correct |
6 ms |
43612 KB |
Output is correct |
22 |
Correct |
6 ms |
43612 KB |
Output is correct |
23 |
Correct |
8 ms |
50012 KB |
Output is correct |
24 |
Correct |
8 ms |
50012 KB |
Output is correct |
25 |
Correct |
8 ms |
50012 KB |
Output is correct |
26 |
Correct |
8 ms |
50008 KB |
Output is correct |
27 |
Correct |
9 ms |
50008 KB |
Output is correct |
28 |
Correct |
9 ms |
50012 KB |
Output is correct |
29 |
Correct |
8 ms |
50008 KB |
Output is correct |
30 |
Correct |
8 ms |
50012 KB |
Output is correct |
31 |
Correct |
8 ms |
50012 KB |
Output is correct |
32 |
Correct |
8 ms |
49872 KB |
Output is correct |
33 |
Correct |
8 ms |
50012 KB |
Output is correct |
34 |
Correct |
8 ms |
49812 KB |
Output is correct |
35 |
Correct |
8 ms |
50016 KB |
Output is correct |
36 |
Correct |
7 ms |
50012 KB |
Output is correct |
37 |
Correct |
8 ms |
49888 KB |
Output is correct |
38 |
Correct |
7 ms |
50008 KB |
Output is correct |
39 |
Correct |
8 ms |
50012 KB |
Output is correct |
40 |
Correct |
8 ms |
50264 KB |
Output is correct |
41 |
Correct |
8 ms |
50012 KB |
Output is correct |
42 |
Correct |
8 ms |
49756 KB |
Output is correct |
43 |
Correct |
8 ms |
50012 KB |
Output is correct |
44 |
Correct |
7 ms |
50012 KB |
Output is correct |
45 |
Correct |
374 ms |
95876 KB |
Output is correct |
46 |
Correct |
430 ms |
95844 KB |
Output is correct |
47 |
Correct |
412 ms |
96336 KB |
Output is correct |
48 |
Correct |
358 ms |
95572 KB |
Output is correct |
49 |
Correct |
404 ms |
95824 KB |
Output is correct |
50 |
Correct |
396 ms |
95608 KB |
Output is correct |
51 |
Correct |
403 ms |
95684 KB |
Output is correct |
52 |
Correct |
364 ms |
96064 KB |
Output is correct |
53 |
Correct |
394 ms |
95908 KB |
Output is correct |
54 |
Correct |
342 ms |
102184 KB |
Output is correct |
55 |
Correct |
340 ms |
100400 KB |
Output is correct |
56 |
Correct |
323 ms |
99152 KB |
Output is correct |
57 |
Correct |
336 ms |
98688 KB |
Output is correct |
58 |
Correct |
223 ms |
99828 KB |
Output is correct |
59 |
Correct |
236 ms |
100056 KB |
Output is correct |
60 |
Correct |
121 ms |
104020 KB |
Output is correct |
61 |
Correct |
409 ms |
94992 KB |
Output is correct |
62 |
Correct |
326 ms |
102740 KB |
Output is correct |
63 |
Correct |
316 ms |
94700 KB |
Output is correct |
64 |
Correct |
286 ms |
94672 KB |
Output is correct |
65 |
Correct |
344 ms |
102740 KB |
Output is correct |
66 |
Correct |
323 ms |
94740 KB |
Output is correct |