#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo n,m;
//#define endl '\n'
llo it[200001];
llo aa,bb,cc;
llo tree[800001];
llo build(llo no,llo l,llo r){
if(l==r){
tree[no]=it[l];
}
else{
llo mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
tree[no]=max(tree[no*2+1],tree[no*2+2]);
}
}
llo query(llo no,llo l,llo r,llo aa,llo bb,llo x){
if(r<aa or l>bb or aa>bb){
return n;
}
if(tree[no]<x){
return n;
}
if(l==r){
return l;
}
llo mid=(l+r)/2;
llo xx=query(no*2+1,l,mid,aa,bb,x);
if(xx<n){
return xx;
}
return query(no*2+2,mid+1,r,aa,bb,x);
}
llo query2(llo no,llo l,llo r,llo aa,llo bb,llo x){
if(r<aa or l>bb or aa>bb){
return -1;
}
if(tree[no]<x){
return -1;
}
if(l==r){
return l;
}
llo mid=(l+r)/2;
llo xx=query2(no*2+2,mid+1,r,aa,bb,x);
if(xx>-1){
return xx;
}
return query2(no*2+1,l,mid,aa,bb,x);
}
vector<llo> adj[200001];
vector<pair<pair<llo,llo>,pair<llo,llo>>> dd;
llo dp[200001];
llo vis[200001];
llo tree2[200001];
void u(llo i,llo j){
while(i<=200000){
tree2[i]+=j;
i+=(i&(-i));
}
}
llo ss(llo i){
llo tot=0;
while(i>0){
tot+=tree2[i];
i-=(i&-i);
}
return tot;
}
llo dfs(llo no,llo par=-1){
dp[no]=0;
vis[no]=1;
for(auto j:adj[no]){
dfs(j,no);
dp[no]+=dp[j];
}
for(auto j:adj[no]){
u(dd[j].a.b+2,dp[j]);
u(dd[no].a.b+2,-dp[j]);
// cout<<dd[j].a.b+2<<" "<<dd[no].a.b+1<<endl;
u(dd[no].a.a+1,dp[j]);
u(dd[j].a.a+1,-dp[j]);
// cout<<dd[no].a.a+1<<" "<<dd[j].a.a<<endl;
}
dp[no]=max(dp[no],ss(dd[no].b.a+1)+dd[no].b.b);
// cout<<dd[no].b.a+1<<":::"<<ss(dd[no].b.a+1)<<endl;
// cout<<no<<","<<dp[no]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
llo su2=0;
for(llo i=0;i<n;i++){
cin>>it[i];
}
cin>>m;
build(0,0,n-1);
for(llo i=0;i<m;i++){
cin>>aa>>bb>>cc;
su2+=cc;
llo ind=query2(0,0,n-1,0,aa-2,bb);
llo ind2=query(0,0,n-1,aa,n-1,bb);
// cout<<ind<<"<"<<ind2<<endl;
dd.pb({{ind+1,-(ind2-1)},{aa-1,cc}});
}
sort(dd.begin(),dd.end());
for(llo i=0;i<m;i++){
dd[i].a.b=-dd[i].a.b;
}
/* for(auto j:dd){
cout<<j.a.a<<","<<j.a.b<<","<<j.b.a<<","<<j.b.b<<endl;
}*/
stack<pair<pair<llo,llo>,llo>> ac;
for(llo i=0;i<m;i++){
pair<pair<llo,llo>,pair<llo,llo>> kk=dd[i];
while(ac.size()){
if(ac.top().a.b<kk.a.b){
ac.pop();
}
else{
adj[ac.top().b].pb(i);
// cout<<ac.top().b<<":"<<i<<endl;
break;
}
}
ac.push({kk.a,i});
}
llo ans=0;
for(llo i=0;i<m;i++){
if(vis[i]==0){
dfs(i);
ans+=dp[i];
}
}
cout<<su2-ans<<endl;
return 0;
}
Compilation message
constellation3.cpp: In function 'llo build(llo, llo, llo)':
constellation3.cpp:24:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
constellation3.cpp: In function 'llo dfs(llo, llo)':
constellation3.cpp:98:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5248 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5248 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
10 ms |
5376 KB |
Output is correct |
24 |
Correct |
10 ms |
5376 KB |
Output is correct |
25 |
Correct |
9 ms |
5376 KB |
Output is correct |
26 |
Correct |
10 ms |
5376 KB |
Output is correct |
27 |
Correct |
10 ms |
5376 KB |
Output is correct |
28 |
Correct |
10 ms |
5376 KB |
Output is correct |
29 |
Correct |
10 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5376 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
11 ms |
5504 KB |
Output is correct |
33 |
Correct |
10 ms |
5504 KB |
Output is correct |
34 |
Correct |
9 ms |
5504 KB |
Output is correct |
35 |
Correct |
9 ms |
5504 KB |
Output is correct |
36 |
Correct |
9 ms |
5632 KB |
Output is correct |
37 |
Correct |
9 ms |
5632 KB |
Output is correct |
38 |
Correct |
9 ms |
5632 KB |
Output is correct |
39 |
Correct |
9 ms |
5376 KB |
Output is correct |
40 |
Correct |
9 ms |
5504 KB |
Output is correct |
41 |
Correct |
13 ms |
5352 KB |
Output is correct |
42 |
Correct |
9 ms |
5376 KB |
Output is correct |
43 |
Correct |
11 ms |
5608 KB |
Output is correct |
44 |
Correct |
12 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5248 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
10 ms |
5376 KB |
Output is correct |
24 |
Correct |
10 ms |
5376 KB |
Output is correct |
25 |
Correct |
9 ms |
5376 KB |
Output is correct |
26 |
Correct |
10 ms |
5376 KB |
Output is correct |
27 |
Correct |
10 ms |
5376 KB |
Output is correct |
28 |
Correct |
10 ms |
5376 KB |
Output is correct |
29 |
Correct |
10 ms |
5376 KB |
Output is correct |
30 |
Correct |
9 ms |
5376 KB |
Output is correct |
31 |
Correct |
11 ms |
5376 KB |
Output is correct |
32 |
Correct |
11 ms |
5504 KB |
Output is correct |
33 |
Correct |
10 ms |
5504 KB |
Output is correct |
34 |
Correct |
9 ms |
5504 KB |
Output is correct |
35 |
Correct |
9 ms |
5504 KB |
Output is correct |
36 |
Correct |
9 ms |
5632 KB |
Output is correct |
37 |
Correct |
9 ms |
5632 KB |
Output is correct |
38 |
Correct |
9 ms |
5632 KB |
Output is correct |
39 |
Correct |
9 ms |
5376 KB |
Output is correct |
40 |
Correct |
9 ms |
5504 KB |
Output is correct |
41 |
Correct |
13 ms |
5352 KB |
Output is correct |
42 |
Correct |
9 ms |
5376 KB |
Output is correct |
43 |
Correct |
11 ms |
5608 KB |
Output is correct |
44 |
Correct |
12 ms |
5376 KB |
Output is correct |
45 |
Correct |
287 ms |
27324 KB |
Output is correct |
46 |
Correct |
288 ms |
26844 KB |
Output is correct |
47 |
Correct |
294 ms |
27648 KB |
Output is correct |
48 |
Correct |
294 ms |
26924 KB |
Output is correct |
49 |
Correct |
282 ms |
27000 KB |
Output is correct |
50 |
Correct |
323 ms |
26780 KB |
Output is correct |
51 |
Correct |
288 ms |
27196 KB |
Output is correct |
52 |
Correct |
297 ms |
27320 KB |
Output is correct |
53 |
Correct |
279 ms |
26812 KB |
Output is correct |
54 |
Correct |
308 ms |
48724 KB |
Output is correct |
55 |
Correct |
312 ms |
48428 KB |
Output is correct |
56 |
Correct |
312 ms |
48052 KB |
Output is correct |
57 |
Correct |
327 ms |
48352 KB |
Output is correct |
58 |
Correct |
303 ms |
47548 KB |
Output is correct |
59 |
Correct |
277 ms |
47780 KB |
Output is correct |
60 |
Correct |
209 ms |
48284 KB |
Output is correct |
61 |
Correct |
271 ms |
30872 KB |
Output is correct |
62 |
Correct |
294 ms |
48212 KB |
Output is correct |
63 |
Correct |
265 ms |
31444 KB |
Output is correct |
64 |
Correct |
255 ms |
29908 KB |
Output is correct |
65 |
Correct |
283 ms |
47192 KB |
Output is correct |
66 |
Correct |
270 ms |
32392 KB |
Output is correct |