#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;
}
priority_queue<pii> pq; //{val,pos}
for(int i=0;i<n;i++)
{
queue<pii> qu;
for(int j=0;j<min(30,i);j++)
{
pii cur = pq.top();
pq.pop();
qu.push(cur);
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)));
}
while(!qu.empty())
{
pii cur = qu.front();
qu.pop();
pq.push(cur);
}
pq.push({v[i],i});
//cout<<"POS: "<<i<<' '<<dp<<endl;
}
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:87:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
jumps.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d%d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2368 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2368 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 |
2368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3338 ms |
6340 KB |
Output is correct |
2 |
Correct |
2482 ms |
6256 KB |
Output is correct |
3 |
Correct |
2828 ms |
6248 KB |
Output is correct |
4 |
Correct |
3228 ms |
6240 KB |
Output is correct |
5 |
Correct |
3252 ms |
8096 KB |
Output is correct |
6 |
Correct |
3126 ms |
7332 KB |
Output is correct |
7 |
Correct |
3188 ms |
7240 KB |
Output is correct |
8 |
Correct |
3145 ms |
7216 KB |
Output is correct |
9 |
Correct |
3107 ms |
7612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2368 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |