This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define ii pair<ll,ll>
#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif
const ll maxn=5005;
const ll inf=1e15;
ll arr[maxn];
struct node{
ll s,e,m,val;
node *l,*r;
node (ll _s, ll _e){
s=_s,e=_e,m=(s+e)>>1,val=0;
if (s!=e) l=new node(s,m),r=new node(m+1,e);
}
void upd(ll x, ll v){
if (s==e){
val=max(v,val);
return;
}
if (x>m) r->upd(x,v);
else l->upd(x,v);
val=max(l->val,r->val);
}
ll query(ll x, ll y){
if (x<=s && e<=y) return val;
if (x>m) return r->query(x,y);
if (y<=m) return l->query(x,y);
return max(l->query(x,y),r->query(x,y));
}
}*root,*rmq;
ll n;
vector<tuple<ll,ll,ll>> v,q;
ll ans[maxn];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
root=new node(1,n);
for (int i=1;i<=n;i++) cin>>arr[i],root->upd(i,arr[i]);
for (int i=1;i<=n;i++){
for (int j=i+2;j<=n;j++){
v.emplace_back(i,j,arr[i]+arr[j]+
root->query(i+1,(i+j)>>1));
//cerr<<i<<' '<<j<<' '<<arr[i]+arr[j]+root->query(i+1,(i+j)>>1)<<endl;
}
}
sort(v.rbegin(),v.rend());
rmq=new node(1,n);
ll qu,l,r;
cin>>qu;
for (int i=1;i<=qu;i++){
cin>>l>>r;
q.emplace_back(l,r,i);
}
sort(q.rbegin(),q.rend());
ll ptr=0;
for (auto [l,r,i]:q){
while (ptr!=v.size() && l<=get<0>(v[ptr])){
rmq->upd(get<1>(v[ptr]),get<2>(v[ptr]));
ptr++;
}
ans[i]=rmq->query(l,r);
}
for (int i=1;i<=qu;i++){
cout<<ans[i]<<endl;
}
return 0;
}
Compilation message (stderr)
jumps.cpp:12:14: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+15' to '2147483647' [-Woverflow]
12 | const ll inf=1e15;
| ^~~~
jumps.cpp: In function 'int main()':
jumps.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | while (ptr!=v.size() && l<=get<0>(v[ptr])){
| ~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |