#include<bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
//Let's do sub case 2 first
struct segmentTree
{
vector<int> val;
segmentTree(int n)
{
for(int i=0;i<2*n;i++)
val.push_back(0);
}
void update(int idx, int v)
{
idx = idx + (val.size())/2 - 1;
val[idx] = v;
idx /= 2;
while(idx != 0)
{
val[idx] = max(val[2*idx],val[2*idx+1]);
idx /= 2;
}
}
int ans(int l, int r)
{
return ans(1,1,(val.size())/2,l+1,r+1);
}
int ans(int idx, int tl, int tr, int l, int r)
{
if(l > tr || r < tl)
return 0;
if(l <= tl && r >= tr)
return val[idx];
int can1 = ans(2*idx,tl,(tl+tr)/2,l,r);
int can2 = ans(2*idx+1,(tl+tr)/2 +1,tr,l,r);
//cout<<"ACCESS: "<<idx<<' '<<tl<<' '<<tr<<' '<<l<<' '<<r<<' '<<max(can1,can2)<<endl;
return max(can1,can2);
}
};
const int N = 262144;
segmentTree tree = segmentTree(N);
int dp = 0;
int v[N];
int main()
{
int n;
scanf("%d",&n);
if(n > N)
return 0;
for(int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
tree.update(i+1,a);
v[i] = a;
}
vector<pii> vv; //{val,pos}
for(int i=0;i<n;i++)
{
for(pii cur: vv)
{
int beg = max(2*cur.second - i,0);
dp = max(dp,v[i]+v[cur.second]+max(tree.ans(beg,cur.second-1),tree.ans(cur.second+1,(cur.second+i)/2)));
}
vv.push_back({v[i],i});
sort(vv.begin(),vv.end(),greater<pii>());
if(vv.size() > 30)
vv.pop_back();
}
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp);
}
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
jumps.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d",&a);
| ~~~~~^~~~~~~~~
jumps.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
jumps.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2368 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2408 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2368 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2408 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2289 ms |
3192 KB |
Output is correct |
2 |
Correct |
1744 ms |
3124 KB |
Output is correct |
3 |
Correct |
1998 ms |
3124 KB |
Output is correct |
4 |
Correct |
2351 ms |
3124 KB |
Output is correct |
5 |
Correct |
2350 ms |
3120 KB |
Output is correct |
6 |
Correct |
2149 ms |
3120 KB |
Output is correct |
7 |
Correct |
2156 ms |
3120 KB |
Output is correct |
8 |
Correct |
2193 ms |
3120 KB |
Output is correct |
9 |
Correct |
2269 ms |
3120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2368 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2408 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |