#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pb push_back
const int N=200050;
const int L=20;
int a[N],lgs[N],n;
pii rmq[N][L];
pii Get(int l,int r){
int k=lgs[r-l+1];
return max(rmq[l][k],rmq[r-(1<<k)+1][k]);
}
pii GetRng(int x,int y){
int top=x,bot=1,mid,L=0,R=n+1;
while(top>=bot){
mid=top+bot>>1;
if(Get(mid,x).first>=y)L=mid,bot=mid+1;
else top=mid-1;
}
top=n,bot=x;
while(top>=bot){
mid=top+bot>>1;
if(Get(x,mid).first>=y)R=mid,top=mid-1;
else bot=mid+1;
}
return {L,R};
}
int x[N],y[N],c[N],ord[N];
vector<int> stars[N];
int ls[N],rs[N];
int Build(int l,int r){
if(r-l<2)return 0;
pii m=Get(l+1,r-1);
ls[m.second]=Build(l,m.second);
rs[m.second]=Build(m.second,r);
return m.second;
}
ll dp[N],sum[N];
int lid[N],rid[N],tid;
ll bit[N];
void Add(int i,ll x){for(;i<=n;i+=i&-i)bit[i]+=x;}
void Add(int l,int r,ll x){Add(l,x);Add(r+1,-x);}
ll Get(int i){ll ans=0;for(;i>=1;i-=i&-i)ans+=bit[i];return ans;}
void Solve(int u){
if(!u)return;
lid[u]=++tid;
Solve(ls[u]);
Solve(rs[u]);
rid[u]=tid;
sum[u]=dp[u]=dp[ls[u]]+dp[rs[u]];
for(int i:stars[u]){
dp[u]=max(dp[u],sum[u]+Get(lid[x[i]])+c[i]);
}
Add(lid[u],rid[u],sum[u]-dp[u]);
}
int main(){
scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&a[i]),rmq[i][0]={a[i],i};
a[0]=a[n+1]=n;
for(int j=1;j<L;j++){
for(int i=1;i<=n-(1<<j)+1;i++){
rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<j-1)][j-1]);
}
}
for(int i=2;i<=n;i++)lgs[i]=lgs[i>>1]+1;
ll sum=0;
int m;
scanf("%i",&m);
for(int i=1;i<=m;i++){
scanf("%i %i %i",&x[i],&y[i],&c[i]);
pii rng=GetRng(x[i],y[i]);
stars[Get(rng.first+1,rng.second-1).second].pb(i);
sum+=c[i];
}
int root=Build(0,n+1);
Solve(root);
printf("%lld\n",sum-dp[root]);
return 0;
}
Compilation message
constellation3.cpp: In function 'std::pair<int, int> GetRng(int, int)':
constellation3.cpp:17:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | mid=top+bot>>1;
| ~~~^~~~
constellation3.cpp:23:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | mid=top+bot>>1;
| ~~~^~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:63:41: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
63 | rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<j-1)][j-1]);
| ~^~
constellation3.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
constellation3.cpp:59:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
59 | for(int i=1;i<=n;i++)scanf("%i",&a[i]),rmq[i][0]={a[i],i};
| ~~~~~^~~~~~~~~~~~
constellation3.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%i",&m);
| ~~~~~^~~~~~~~~
constellation3.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%i %i %i",&x[i],&y[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
5 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
5 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
6 ms |
5504 KB |
Output is correct |
24 |
Correct |
7 ms |
5504 KB |
Output is correct |
25 |
Correct |
8 ms |
5504 KB |
Output is correct |
26 |
Correct |
7 ms |
5632 KB |
Output is correct |
27 |
Correct |
6 ms |
5504 KB |
Output is correct |
28 |
Correct |
6 ms |
5632 KB |
Output is correct |
29 |
Correct |
6 ms |
5632 KB |
Output is correct |
30 |
Correct |
6 ms |
5632 KB |
Output is correct |
31 |
Correct |
8 ms |
5632 KB |
Output is correct |
32 |
Correct |
6 ms |
5632 KB |
Output is correct |
33 |
Correct |
8 ms |
5632 KB |
Output is correct |
34 |
Correct |
7 ms |
5632 KB |
Output is correct |
35 |
Correct |
6 ms |
5632 KB |
Output is correct |
36 |
Correct |
7 ms |
5632 KB |
Output is correct |
37 |
Correct |
6 ms |
5632 KB |
Output is correct |
38 |
Correct |
7 ms |
5632 KB |
Output is correct |
39 |
Correct |
8 ms |
5504 KB |
Output is correct |
40 |
Correct |
8 ms |
5616 KB |
Output is correct |
41 |
Correct |
6 ms |
5504 KB |
Output is correct |
42 |
Correct |
7 ms |
5632 KB |
Output is correct |
43 |
Correct |
6 ms |
5632 KB |
Output is correct |
44 |
Correct |
6 ms |
5536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
5 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
6 ms |
5504 KB |
Output is correct |
24 |
Correct |
7 ms |
5504 KB |
Output is correct |
25 |
Correct |
8 ms |
5504 KB |
Output is correct |
26 |
Correct |
7 ms |
5632 KB |
Output is correct |
27 |
Correct |
6 ms |
5504 KB |
Output is correct |
28 |
Correct |
6 ms |
5632 KB |
Output is correct |
29 |
Correct |
6 ms |
5632 KB |
Output is correct |
30 |
Correct |
6 ms |
5632 KB |
Output is correct |
31 |
Correct |
8 ms |
5632 KB |
Output is correct |
32 |
Correct |
6 ms |
5632 KB |
Output is correct |
33 |
Correct |
8 ms |
5632 KB |
Output is correct |
34 |
Correct |
7 ms |
5632 KB |
Output is correct |
35 |
Correct |
6 ms |
5632 KB |
Output is correct |
36 |
Correct |
7 ms |
5632 KB |
Output is correct |
37 |
Correct |
6 ms |
5632 KB |
Output is correct |
38 |
Correct |
7 ms |
5632 KB |
Output is correct |
39 |
Correct |
8 ms |
5504 KB |
Output is correct |
40 |
Correct |
8 ms |
5616 KB |
Output is correct |
41 |
Correct |
6 ms |
5504 KB |
Output is correct |
42 |
Correct |
7 ms |
5632 KB |
Output is correct |
43 |
Correct |
6 ms |
5632 KB |
Output is correct |
44 |
Correct |
6 ms |
5536 KB |
Output is correct |
45 |
Correct |
563 ms |
53752 KB |
Output is correct |
46 |
Correct |
558 ms |
53368 KB |
Output is correct |
47 |
Correct |
580 ms |
52604 KB |
Output is correct |
48 |
Correct |
565 ms |
53880 KB |
Output is correct |
49 |
Correct |
558 ms |
52216 KB |
Output is correct |
50 |
Correct |
552 ms |
52344 KB |
Output is correct |
51 |
Correct |
577 ms |
52572 KB |
Output is correct |
52 |
Correct |
592 ms |
53368 KB |
Output is correct |
53 |
Correct |
564 ms |
53368 KB |
Output is correct |
54 |
Correct |
767 ms |
57720 KB |
Output is correct |
55 |
Correct |
759 ms |
57976 KB |
Output is correct |
56 |
Correct |
739 ms |
56568 KB |
Output is correct |
57 |
Correct |
758 ms |
55800 KB |
Output is correct |
58 |
Correct |
687 ms |
56056 KB |
Output is correct |
59 |
Correct |
699 ms |
55800 KB |
Output is correct |
60 |
Correct |
231 ms |
62260 KB |
Output is correct |
61 |
Correct |
624 ms |
55040 KB |
Output is correct |
62 |
Correct |
834 ms |
59256 KB |
Output is correct |
63 |
Correct |
666 ms |
54172 KB |
Output is correct |
64 |
Correct |
622 ms |
53268 KB |
Output is correct |
65 |
Correct |
727 ms |
59712 KB |
Output is correct |
66 |
Correct |
635 ms |
53644 KB |
Output is correct |