#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define X first
#define Y second
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;
// #define Sieve
#ifdef Sieve
vector<int> pr;
vector<int> lpf;
void Linear_sieve(int n = Lim) {
pr.assign(1, 2);
lpf.assign(n + 1, 2);
for (int x = 3; x <= n; x += 2) {
if (lpf[x] == 2) pr.push_back(lpf[x] = x);
for (int i = 1; i < pr.size() && pr[i] <= lpf[x] && pr[i] * x <= n; ++i)
lpf[pr[i] * x] = pr[i];
}
}
#endif
// #define Ckn_calc
#ifdef Ckn_calc
const int LIM = 1e6 + 16;
int fact[LIM + 10]; /// factorial: fact[n] = n!
int invs[LIM + 10]; /// inverse modular: invs[n] = n^(-1)
int tcaf[LIM + 10]; /// inverse factorial: tcaf[n] = (n!)^(-1)
void precal_nck(int n = LIM)
{
fact[0] = fact[1] = 1;
invs[0] = invs[1] = 1;
tcaf[0] = tcaf[1] = 1;
for (int i = 2; i <= n; ++i)
{
fact[i] = (1LL * fact[i - 1] * i) % Mod;
invs[i] = Mod - 1LL * (Mod / i) * invs[Mod % i] % Mod;
tcaf[i] = (1LL * tcaf[i - 1] * invs[i]) % Mod;
}
}
ll C(int n, int k) {
if (n < k || k < 0) return 0;
ll res = fact[n];
res *= tcaf[k], res %= Mod;
res *= tcaf[n - k], res %= Mod;
return res;
}
ll P(int n, int k) {
ll res = fact[n];
res *= tcaf[n - k], res %= Mod;
return res;
}
#endif
/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====
const int base = 3;
const int N = 5e5 + 5;
const int K = log2(N) + 2;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;
int n;
int a[N];
vector<pii> qs[N];
vector<pii> upd[N];
pii f[N * 4];
int lazy[N * 4];
#define lx (id << 1)
#define rx (lx | 1)
pii combine(pii l, pii r) {
return make_pair(max(l.first, r.first), max(l.second, r.second));
}
void push(int id, int val) {
maximize(f[id].first, f[id].second + val);
maximize(lazy[id], val);
}
void down(int id) {
push(lx, lazy[id]);
push(rx, lazy[id]);
lazy[id] = 0;
}
void build(int l = 1, int r = n, int id = 1) {
if (l == r) {
return f[id] = make_pair(a[l], a[l]), void();
}
int mid = (l + r) >> 1;
build(l, mid, lx);
build(mid + 1, r, rx);
f[id] = combine(f[lx], f[rx]);
}
void update(int u, int v, int x, int l = 1, int r = n, int id = 1) {
if (r < u || v < l) return;
if (u <= l && r <= v)
return push(id, x);
down(id);
int mid = (l + r) >> 1;
update(u, v, x, l, mid, lx);
update(u, v, x, mid + 1, r, rx);
f[id] = combine(f[lx], f[rx]);
}
int get(int u, int v, int l = 1, int r = n, int id = 1) {
if (r < u || v < l) return 0;
if (u <= l && r <= v)
return f[id].first;
down(id);
int mid = (l + r) >> 1;
return max(get(u, v, l, mid, lx), get(u, v, mid + 1, r, rx));
}
int ans[N];
void solve() {
cin >> n;
foru(i, 1, n) cin >> a[i];
int q; cin >> q;
foru(i, 1, q) {
int l, r;
cin >> l >> r;
qs[l].pb({r, i});
}
build();
vector<int> d;
foru(i, 1, n) {
/*for (auto j: d)
if (i * 2 - j)
upd[j].pb({i * 2 - j, a[i] + a[j]});*/
while (!d.empty()) {
int j = d.back();
if (i * 2 - j)
upd[j].pb({i * 2 - j, a[i] + a[j]});
if (a[i] <= a[j]) break;
d.pop_back();
}
d.push_back(i);
}
ford(i, n, 1) {
for (auto &[x, y]: upd[i]) {
update(x, n, y);
// cout << x << " " << n << " " << y << "\n";
}
for (auto &[r, id]: qs[i]) {
ans[id] = get(i, r);
}
}
foru(i, 1, q) cout << ans[i] << "\n";
}
signed main() {
FastIO;
#define task "test"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
#ifdef Sieve
Linear_sieve();
#endif
#ifdef Ckn_calc
precal_nck();
#endif
int ntest = 1;
// cin >> ntest;
while (ntest--) {
//cout << "Case " << q << ": " << "\n";
solve();
cout << "\n";
}
return 0;
}
/** /\_/\
* (= ._.)
* / >TL \>AC
**/
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:212:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
212 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:213:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
213 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
27108 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
5 ms |
26972 KB |
Output is correct |
7 |
Correct |
5 ms |
27080 KB |
Output is correct |
8 |
Correct |
5 ms |
26968 KB |
Output is correct |
9 |
Correct |
6 ms |
26968 KB |
Output is correct |
10 |
Correct |
5 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
27108 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
5 ms |
26972 KB |
Output is correct |
7 |
Correct |
5 ms |
27080 KB |
Output is correct |
8 |
Correct |
5 ms |
26968 KB |
Output is correct |
9 |
Correct |
6 ms |
26968 KB |
Output is correct |
10 |
Correct |
5 ms |
26972 KB |
Output is correct |
11 |
Correct |
221 ms |
41040 KB |
Output is correct |
12 |
Correct |
224 ms |
40956 KB |
Output is correct |
13 |
Correct |
213 ms |
41044 KB |
Output is correct |
14 |
Correct |
222 ms |
41044 KB |
Output is correct |
15 |
Correct |
222 ms |
41080 KB |
Output is correct |
16 |
Correct |
223 ms |
40276 KB |
Output is correct |
17 |
Correct |
231 ms |
40512 KB |
Output is correct |
18 |
Correct |
223 ms |
40248 KB |
Output is correct |
19 |
Correct |
235 ms |
41148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
41040 KB |
Output is correct |
2 |
Correct |
66 ms |
39760 KB |
Output is correct |
3 |
Correct |
67 ms |
40452 KB |
Output is correct |
4 |
Correct |
128 ms |
40868 KB |
Output is correct |
5 |
Correct |
124 ms |
40884 KB |
Output is correct |
6 |
Correct |
121 ms |
41044 KB |
Output is correct |
7 |
Correct |
188 ms |
40872 KB |
Output is correct |
8 |
Correct |
120 ms |
41040 KB |
Output is correct |
9 |
Correct |
194 ms |
41044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
27108 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
5 ms |
26972 KB |
Output is correct |
6 |
Correct |
5 ms |
26972 KB |
Output is correct |
7 |
Correct |
5 ms |
27080 KB |
Output is correct |
8 |
Correct |
5 ms |
26968 KB |
Output is correct |
9 |
Correct |
6 ms |
26968 KB |
Output is correct |
10 |
Correct |
5 ms |
26972 KB |
Output is correct |
11 |
Correct |
221 ms |
41040 KB |
Output is correct |
12 |
Correct |
224 ms |
40956 KB |
Output is correct |
13 |
Correct |
213 ms |
41044 KB |
Output is correct |
14 |
Correct |
222 ms |
41044 KB |
Output is correct |
15 |
Correct |
222 ms |
41080 KB |
Output is correct |
16 |
Correct |
223 ms |
40276 KB |
Output is correct |
17 |
Correct |
231 ms |
40512 KB |
Output is correct |
18 |
Correct |
223 ms |
40248 KB |
Output is correct |
19 |
Correct |
235 ms |
41148 KB |
Output is correct |
20 |
Correct |
123 ms |
41040 KB |
Output is correct |
21 |
Correct |
66 ms |
39760 KB |
Output is correct |
22 |
Correct |
67 ms |
40452 KB |
Output is correct |
23 |
Correct |
128 ms |
40868 KB |
Output is correct |
24 |
Correct |
124 ms |
40884 KB |
Output is correct |
25 |
Correct |
121 ms |
41044 KB |
Output is correct |
26 |
Correct |
188 ms |
40872 KB |
Output is correct |
27 |
Correct |
120 ms |
41040 KB |
Output is correct |
28 |
Correct |
194 ms |
41044 KB |
Output is correct |
29 |
Correct |
795 ms |
76452 KB |
Output is correct |
30 |
Correct |
672 ms |
73356 KB |
Output is correct |
31 |
Correct |
647 ms |
75224 KB |
Output is correct |
32 |
Correct |
884 ms |
76340 KB |
Output is correct |
33 |
Correct |
842 ms |
76476 KB |
Output is correct |
34 |
Correct |
875 ms |
75672 KB |
Output is correct |
35 |
Correct |
862 ms |
75772 KB |
Output is correct |
36 |
Correct |
814 ms |
75736 KB |
Output is correct |
37 |
Correct |
770 ms |
76296 KB |
Output is correct |
38 |
Correct |
624 ms |
82140 KB |
Output is correct |
39 |
Correct |
630 ms |
82104 KB |
Output is correct |
40 |
Correct |
606 ms |
80348 KB |
Output is correct |
41 |
Correct |
642 ms |
80008 KB |
Output is correct |
42 |
Correct |
657 ms |
80232 KB |
Output is correct |
43 |
Correct |
607 ms |
81204 KB |
Output is correct |
44 |
Correct |
607 ms |
81504 KB |
Output is correct |
45 |
Correct |
681 ms |
81332 KB |
Output is correct |
46 |
Correct |
629 ms |
79924 KB |
Output is correct |
47 |
Correct |
617 ms |
79764 KB |
Output is correct |
48 |
Correct |
632 ms |
79660 KB |
Output is correct |
49 |
Correct |
611 ms |
81016 KB |
Output is correct |
50 |
Correct |
669 ms |
79504 KB |
Output is correct |
51 |
Correct |
650 ms |
79188 KB |
Output is correct |
52 |
Correct |
661 ms |
78680 KB |
Output is correct |
53 |
Correct |
637 ms |
78420 KB |
Output is correct |
54 |
Correct |
645 ms |
78420 KB |
Output is correct |
55 |
Correct |
632 ms |
79100 KB |
Output is correct |