#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __prine_one(const char *&s, const T &x)
{
while (*s == ' ') ++s;
const char *p = s;
int bal = 0;
while (*s)
{
if (*s == '(') ++bal;
else if (*s == ')') --bal;
else if (*s == ',' && bal == 0) break;
++s;
}
cerr.write(p, s - p) << " = " << x;
if (*s == ',')
{
cerr << " , ";
++s;
}
}
template<typename... Args>
void debug(const char *s, Args... args)
{
cerr << "[ ";
int dummy[] = {0, (__prine_one(s, args), 0)...};
(void)dummy;
cerr << " ]\n\n";
}
// --------------------------------------------------------------------------------------------
const int maxn = 5e5 + 3;
int n, q;
ll a[maxn];
// --------------------------------------------------------------------------------------------
namespace sub2
{
bool approve()
{
return n <= 5000;
}
ll f[maxn][20];
int lg[maxn], pre[5005][5005];
ll get_max(int l, int r)
{
if (l > r) return 1e9;
int k = lg[r - l + 1];
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
void solve()
{
FOR(i, 1, n)
f[i][0] = a[i];
FOR(j, 1, 19)
FOR(i, 1, n - (1 << j) + 1)
f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
FOR(i, 2, n)
lg[i] = lg[i >> 1] + 1;
FOR(i, 1, n - 2)
FOR(j, i + 2, n)
pre[i][j] = a[i] + a[j] + get_max(i + 1, i + (j - i) / 2);
FORD(l, n, 1)
FOR(r, 1, n)
{
pre[l][r] = max(pre[l][r], pre[l + 1][r]);
pre[l][r] = max(pre[l][r], pre[l][r - 1]);
}
while (q--)
{
int l, r; cin >> l >> r;
if (l > r)
return void(cout << 0 << '\n');
cout << pre[l][r] << '\n';
}
}
}
namespace Greedy
{
typedef pair< pair<ll, ll>, pair<ll, ll> > PAIR;
ll f[maxn][20];
int lg[maxn];
ll get_max(int l, int r)
{
if (l > r) return 1e9;
int k = lg[r - l + 1];
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
bool approve()
{
return true;
}
PAIR seg[maxn << 2];
bool cmp(const pll &x, const pll &y)
{
if (x.fi == y.fi) return x.se < y.se;
return x.fi > y.fi;
}
PAIR combine(const PAIR left, const PAIR right)
{
vector<pll> vec;
vec.push_back(left.fi);
vec.push_back(left.se);
vec.push_back(right.fi);
vec.push_back(right.se);
sort(all(vec), greater<pll>());
sort(vec.begin() + 1, vec.end(), cmp);
return {{vec[0].fi, vec[0].se}, {vec[1].fi, vec[1].se}};
}
void build(int id, int l, int r)
{
if (l == r)
{
seg[id] = {{a[l], l}, {0ll, 0ll}};
return;
}
int mid = l + r >> 1;
build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r);
seg[id] = combine(seg[id << 1], seg[id << 1 | 1]);
}
PAIR get(int id, int l, int r, int u, int v)
{
if (l > v || r < u) return {{0, 0}, {0, 0}};
if (l >= u && r <= v) return seg[id];
int mid = l + r >> 1;
return combine(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
void solve()
{
build(1, 1, n);
FOR(i, 1, n)
f[i][0] = a[i];
FOR(j, 1, 19)
FOR(i, 1, n - (1 << j) + 1)
f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
FOR(i, 2, n)
lg[i] = lg[i >> 1] + 1;
while (q--)
{
int l, r; cin >> l >> r;
if (l > r)
return void(cout << 0 << '\n');
PAIR cur = get(1, 1, n, l, r);
int x = cur.fi.se, y = cur.se.se;
if (x > y)
swap(x, y);
ll res = 0;
if (y + (y - x) <= r)
res = max(res, a[x] + a[y] + get_max(y + (y - x), r));
if (x > l)
res = max(res, a[x] + a[y] + get_max( max( l, x - (y - x) ) , x - 1 ) );
if (y - x > 1)
res = max(res, a[x] + a[y] + get_max(x + 1, x + (y - x) / 2));
cout << res << '\n';
}
}
}
void solve()
{
cin >> n;
FOR(i, 1, n)
cin >> a[i];
cin >> q;
if (sub2 :: approve()) return sub2 :: solve(), void();
Greedy :: solve();
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:219:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
219 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:220:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
220 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |