This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, a[MAXN], q;
vector<ii> qr[MAXN];
ll st[MAXN << 2], mx[MAXN << 2], lazy[MAXN << 2];
void down(int id, int l, int r){
if(!lazy[id] || l == r) return;
for(int i : {id * 2, id * 2 + 1}){
maximize(mx[i], st[i] + lazy[id]);
maximize(lazy[i], lazy[id]);
}
lazy[id] = 0;
}
void build(int id, int l, int r){
if(l == r){
st[id] = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void update(int id, int l, int r, int u, int v, int val){
if(v < l || r < u) return;
down(id, l, r);
if(u <= l && r <= v){
maximize(mx[id], val + st[id]);
maximize(lazy[id], val);
down(id, l, r);
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
mx[id] = max(mx[id << 1], mx[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v){
if (v < l || r < u) return 0;
down(id, l, r);
if (u <= l && r <= v) return mx[id];
int mid = l + r >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
ll res[MAXN];
void Solve() {
cin >> n;
For (i, 1, n) cin >> a[i];
build(1, 1, n);
cin >> q;
For (i, 1, q){
int l, r; cin >> l >> r;
qr[l].push_back({r, i});
}
vector<int> st1, st2;
Ford(i, n, 1){
while(st1.size() && a[st1.back()] <= a[i]){
int j = st1.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
st1.pop_back();
}
while(st2.size() && a[st2.back()] >= a[i]){
int j = st2.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
st2.pop_back();
}
st1.push_back(i);
st2.push_back(i);
for(auto [r, id] : qr[i]) res[id] = get(1, 1, n, i + 2, r);
}
For(i, 1, q) cout << res[i] << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("JOI19_jumps.inp", "r")) {
freopen("JOI19_jumps.inp", "r", stdin);
freopen("JOI19_jumps.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message (stderr)
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:71:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get(int, int, int, int, int)':
jumps.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int32_t main()':
jumps.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen("JOI19_jumps.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen("JOI19_jumps.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... |