#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,r);
}
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(20,i);j++)
{
pii cur = pq.top();
pq.pop();
qu.push(cur);
int beg = max(2*cur.second - i,1);
dp = max(dp,v[i]+v[cur.second]+max(tree.ans(beg,cur.second),tree.ans(cur.second+2,(cur.second+i)/2 +1)));
//cout<<"VAL: "<<i<<' '<<j<<' '<<beg<<' '<<cur.second<<' '<<tree.ans(beg,cur.second)<<' '<<tree.ans(cur.second+2,(cur.second+i)/2 +1)<<' '<<(cur.second+i)/2<<endl;
}
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:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
jumps.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2492 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2492 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2218 ms |
8096 KB |
Output is correct |
2 |
Correct |
1715 ms |
7920 KB |
Output is correct |
3 |
Correct |
1899 ms |
7964 KB |
Output is correct |
4 |
Incorrect |
2211 ms |
8000 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2492 KB |
Output is correct |
2 |
Incorrect |
5 ms |
2368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |