#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];
unordered_map<ll,vector<int>> stars;
//vector<int> idx[N];
unordered_map<ll,ll> was;
vector<int> bit[N];
void Add(int x,int y){
for(int i=y;i<=n;i+=i&-i)bit[i].pb(x);
}
int Check(int l,int r,int y){
for(int i=y;i>=1;i-=i&-i){
int j=lower_bound(bit[i].begin(),bit[i].end(),l)-bit[i].begin();
if(j<bit[i].size()&&bit[i][j]<=r)return 1;
}
return 0;
}
ll DP(int l,int r){
if(r-l<2)return 0;
if(was.count((ll)l*N+r))return was[(ll)l*N+r];
if(!Check(l+1,r-1,max(a[l],a[r]))){
return was[(ll)l*N+r]=0;
}
pii m=Get(l+1,r-1);
//int fir=upper_bound(idx[m].begin(),idx[m].end(),l)-idx[m].begin();
//int las=lower_bound(idx[m].begin(),idx[m].end(),r)-idx[m].begin();
ll ans=DP(l,m.second)+DP(m.second,r);
if(stars.count((ll)l*N+r))for(int i:stars[(ll)l*N+r])ans=max(ans,DP(l,x[i])+c[i]+DP(x[i],r));
return was[(ll)l*N+r]=ans;
/*vector<ll> dp;
int L=l;
for(int i=fir;i<las;i++){
dp.pb(DP(L,idx[m][i]));
L=idx[m][i];
}
dp.pb(DP(L,r));
ll sum=0;
for(ll d:dp)sum+=d;
vector<int> pts=stars[{l,r}];
sort(pts.begin(),pts.end(),[&](int i,int j){return x[i]<x[j];});
ll ans=sum;
L=l;
int j=0;
for(int i=fir,o=0;i<las;i++,o++){
int mx=0;
while(j<pts.size()&&x[pts[j]]<idx[m][i])mx=max(mx,c[pts[j]]),j++;
ans=max(ans,sum-dp[o]+mx);
while(j<pts.size()&&x[pts[j]]==idx[m][i])ans=max(ans,sum+c[pts[j]]),j++;
printf("%i - ",idx[m][i]);
}
printf("|");
int mx=0;
while(j<pts.size())mx=max(mx,c[pts[j]]),j++;
ans=max(ans,sum-dp.back()+mx);
printf("%i %i %lld\n",l,r,ans);
return ans;*/
}
int main(){
scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&a[i]),rmq[i][0]={a[i],i};//,idx[a[i]].pb(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[(ll)rng.first*N+rng.second].pb(i);
sum+=c[i];
ord[i]=i;
}
sort(ord+1,ord+1+m,[&](int i,int j){return x[i]<x[j];});
for(int i=1;i<=m;i++)Add(x[ord[i]],y[ord[i]]);
printf("%lld\n",sum-DP(0,n+1));
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 Check(int, int, int)':
constellation3.cpp:40:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if(j<bit[i].size()&&bit[i][j]<=r)return 1;
| ~^~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:91:41: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
91 | rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<j-1)][j-1]);
| ~^~
constellation3.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
constellation3.cpp:87:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
87 | for(int i=1;i<=n;i++)scanf("%i",&a[i]),rmq[i][0]={a[i],i};//,idx[a[i]].pb(i);
| ~~~~~^~~~~~~~~~~~
constellation3.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
97 | scanf("%i",&m);
| ~~~~~^~~~~~~~~
constellation3.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
99 | scanf("%i %i %i",&x[i],&y[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5632 KB |
Output is correct |
11 |
Correct |
5 ms |
5376 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
5 ms |
5376 KB |
Output is correct |
15 |
Correct |
5 ms |
5376 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
6 ms |
5504 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5632 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5632 KB |
Output is correct |
11 |
Correct |
5 ms |
5376 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
5 ms |
5376 KB |
Output is correct |
15 |
Correct |
5 ms |
5376 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
6 ms |
5504 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5632 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
9 ms |
5760 KB |
Output is correct |
24 |
Correct |
8 ms |
5760 KB |
Output is correct |
25 |
Correct |
8 ms |
5760 KB |
Output is correct |
26 |
Correct |
8 ms |
5760 KB |
Output is correct |
27 |
Correct |
8 ms |
5760 KB |
Output is correct |
28 |
Correct |
8 ms |
5760 KB |
Output is correct |
29 |
Correct |
8 ms |
5760 KB |
Output is correct |
30 |
Correct |
8 ms |
5760 KB |
Output is correct |
31 |
Correct |
8 ms |
5760 KB |
Output is correct |
32 |
Correct |
193 ms |
23768 KB |
Output is correct |
33 |
Correct |
146 ms |
20196 KB |
Output is correct |
34 |
Correct |
122 ms |
19644 KB |
Output is correct |
35 |
Correct |
99 ms |
15040 KB |
Output is correct |
36 |
Correct |
74 ms |
21864 KB |
Output is correct |
37 |
Correct |
76 ms |
21352 KB |
Output is correct |
38 |
Correct |
6 ms |
5888 KB |
Output is correct |
39 |
Correct |
11 ms |
6400 KB |
Output is correct |
40 |
Correct |
188 ms |
24804 KB |
Output is correct |
41 |
Correct |
12 ms |
6400 KB |
Output is correct |
42 |
Correct |
11 ms |
6272 KB |
Output is correct |
43 |
Correct |
227 ms |
34384 KB |
Output is correct |
44 |
Correct |
11 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5632 KB |
Output is correct |
11 |
Correct |
5 ms |
5376 KB |
Output is correct |
12 |
Correct |
5 ms |
5376 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
5 ms |
5376 KB |
Output is correct |
15 |
Correct |
5 ms |
5376 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
6 ms |
5504 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5632 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
9 ms |
5760 KB |
Output is correct |
24 |
Correct |
8 ms |
5760 KB |
Output is correct |
25 |
Correct |
8 ms |
5760 KB |
Output is correct |
26 |
Correct |
8 ms |
5760 KB |
Output is correct |
27 |
Correct |
8 ms |
5760 KB |
Output is correct |
28 |
Correct |
8 ms |
5760 KB |
Output is correct |
29 |
Correct |
8 ms |
5760 KB |
Output is correct |
30 |
Correct |
8 ms |
5760 KB |
Output is correct |
31 |
Correct |
8 ms |
5760 KB |
Output is correct |
32 |
Correct |
193 ms |
23768 KB |
Output is correct |
33 |
Correct |
146 ms |
20196 KB |
Output is correct |
34 |
Correct |
122 ms |
19644 KB |
Output is correct |
35 |
Correct |
99 ms |
15040 KB |
Output is correct |
36 |
Correct |
74 ms |
21864 KB |
Output is correct |
37 |
Correct |
76 ms |
21352 KB |
Output is correct |
38 |
Correct |
6 ms |
5888 KB |
Output is correct |
39 |
Correct |
11 ms |
6400 KB |
Output is correct |
40 |
Correct |
188 ms |
24804 KB |
Output is correct |
41 |
Correct |
12 ms |
6400 KB |
Output is correct |
42 |
Correct |
11 ms |
6272 KB |
Output is correct |
43 |
Correct |
227 ms |
34384 KB |
Output is correct |
44 |
Correct |
11 ms |
6272 KB |
Output is correct |
45 |
Correct |
1273 ms |
79816 KB |
Output is correct |
46 |
Correct |
1238 ms |
78136 KB |
Output is correct |
47 |
Correct |
1267 ms |
78440 KB |
Output is correct |
48 |
Correct |
1251 ms |
79212 KB |
Output is correct |
49 |
Correct |
1227 ms |
77460 KB |
Output is correct |
50 |
Correct |
1218 ms |
77288 KB |
Output is correct |
51 |
Correct |
1241 ms |
77964 KB |
Output is correct |
52 |
Correct |
1276 ms |
78912 KB |
Output is correct |
53 |
Correct |
1233 ms |
78868 KB |
Output is correct |
54 |
Execution timed out |
1589 ms |
132060 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |