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 int long long
struct node
{
int s, e, m, v;
node *l, *r;
node(int _s, int _e) : s(_s), e(_e), m((_s + _e) / 2), v(0)
{
if (s != e)
l = new node(s, m),
r = new node(m + 1, e);
}
void upd(int n, int x)
{
if (s == e)
{
v = x;
return;
}
if (n <= m)
l->upd(n, x);
else
r->upd(n, x);
v = max(l->v, r->v);
}
int qry(int a, int b)
{
if (b < s || a > e)
return -1;
if (a <= s && b >= e)
return v;
return max(l->qry(a, b), r->qry(a, b));
}
} *root;
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, x, Q, l, r;
cin >> N;
vector<int> stiff;
root = new node(0, N - 1);
for (int i = 0; i < N; i++)
{
cin >> x;
root->upd(i, x);
stiff.push_back(x);
}
vector<vector<int>> precomp(N, vector<int>(N, -1)), prefix(N, vector<int>(N, -1)), ans(N, vector<int>(N, -1));
for (int i = 0; i < N; i++)
{
for (int j = i + 2; j < N; j++)
{
precomp[i][j] = stiff[i] + stiff[j] + root->qry(i + 1, (i + j) / 2);
}
}
for (int i = N - 1; i >= 0; i--)
{
for (int j = i + 2; j < N; j++)
{
prefix[i][j] = max(prefix[i + 1][j], precomp[i][j]);
}
}
for (int i = 0; i < N; i++)
{
for (int j = i + 2; j < N; j++)
{
ans[i][j] = max(ans[i][j - 1], prefix[i][j]);
}
}
cin >> Q;
for (int i = 0; i < Q; i++)
{
cin >> l >> r;
l--, r--;
cout << ans[l][r] << '\n';
}
}
# | 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... |