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 int long long int
#define pb push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
#define ld long double
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 5e5 + 5;
const int INF = 1e9 + 500;
const int LGN = 20;
array<int, N> lg2;
struct RMQ {
array<vector<int>, LGN> st;
void reset(int s, vector<int> &arr) {
REP(i, LGN) st[i].assign(s + 3, 0);
for(int i = 1; i <= s; i++) {
st[0][i] = arr[i];
}
for(int k = 1; k < LGN; k++) {
for(int i = 1; i <= s; i++) {
if(i + (1 << (k - 1)) > s) break;
st[k][i] = max(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
}
}
int query(int l, int r) {
int k = lg2[r - l + 1];
return max(st[k][l], st[k][r - (1 << k) + 1]);
}
};
int n, q;
vector<int> a;
RMQ st;
vector<int> res;
vector<vector<int> > ans;
void solve() {
cin >> n;
a.resize(n + 1, 0);
ans.assign(n + 1, vector<int>(n + 1, 0));
for(int i = 1; i<=n; i++) {
cin >> a[i];
}
st.reset(n, a);
for(int i = 1; i <= n; i++) {
for(int j = i + 2; j <= n; j++) {
int tm = (i + j) >> 1;
ans[i][j] = st.query(i + 1, tm) + a[i] + a[j];
}
}
for(int len = 3; len <= n; len++) {
for(int i = 1; i <= n; i++) {
int j = i + len - 1;
if(j > n) break;
ans[i][j] = max(ans[i][j], ans[i + 1][j]);
ans[i][j] = max(ans[i][j], ans[i][j - 1]);
}
}
cin >> q;
REP(i, q) {
int l, r;
cin >> l >> r;
cout << ans[l][r] << "\n";
}
}
signed main() {
lg2[1] = 0;
for(int i = 2; i < N; i++) {
lg2[i] = lg2[i >> 1] + 1;
}
fastio();
solve();
}
# | 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... |