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>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 500005;
int n;
int a[MX];
pii mx[MX][19];
void nhap(){
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
}
pii get(int l, int r){
int k = __lg(r-l+1);
return max(mx[l][k], mx[r-MASK(k)+1][k]);
}
void solve(){
for(int i=1; i<=n; i++) mx[i][0] = {a[i], i};
for(int j=1; j<=18; j++){
for(int i=1; i+MASK(j)-1<=n; i++){
mx[i][j] = max(mx[i][j-1], mx[i+MASK(j-1)][j-1]);
}
}
int q;
cin >> q;
while(q--){
int l, r;
cin >> l >> r;
vector<int> pos;
priority_queue<pair<pii, pii>> Q;
Q.push({get(l, r), {l, r}});
while(Q.size() && pos.size()<30){
pii f1, f2;
tie(f1, f2) = Q.top();
Q.pop();
pos.push_back(f1.se);
if(f2.fi != f1.se) Q.push({get(f2.fi, f1.se - 1), {f2.fi, f1.se - 1}});
if(f2.se != f1.se) Q.push({get(f1.se + 1, f2.se), {f1.se + 1, f2.se}});
}
int ans = 0;
sort(pos.begin(), pos.end());
for(int t1=0; t1<(int)pos.size(); t1++){
int i = pos[t1];
for(int t2=t1+1; t2<(int)pos.size(); t2++){
int j = pos[t2];
if(i != l){
ans = max(ans, get(max(l, 2*i - j), i-1).fi + a[i] + a[j]);
}
if(i+1!=j){
ans = max(ans, get(i+1, (i+j)>>1).fi + a[i] + a[j]);
}
if(r-j >= j-i){
ans = max(ans, get(2*j - i, r).fi + a[i] + a[j]);
}
}
}
cout << ans << endl;
}
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# | 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... |