#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("avx2")
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define all(m) (m).begin(), (m).end()
#define rall(m) (m).rbegin(), (m).rend()
#define vec vector
#define sz(a) (int) (a).size()
#define mpp make_pair
#define mtt make_tuple
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef tuple <int, int, int> tui;
template <typename T>
using prq = priority_queue <T>;
template <typename T>
using pgq = priority_queue <T, vec <T>, greater <T>>;
template <typename T> bool umin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> bool umax(T &a, T b) { return a < b ? a = b, 1 : 0; }
const int N = 5e5 + 42;
struct node{
ll mx, ans, p;
node(ll mx = 0): mx(mx), ans(0), p(0) {}
node operator+(node o){
node res;
res.mx = max(mx, o.mx);
res.ans = max(ans, o.ans);
return res;
}
} t[4 * N];
void build(int tl, int tr, int v, vec <ll> &a){
if (tl + 1 == tr) return void(t[v] = a[tl]);
int tm = tl + tr >> 1;
build(tl, tm, v * 2 + 1, a);
build(tm, tr, v * 2 + 2, a);
t[v] = t[v * 2 + 1] + t[v * 2 + 2];
}
void apply(int v, ll x){
umax(t[v].ans, t[v].mx + x);
umax(t[v].p, x);
}
void push(int v){
if (!t[v].p) return;
for (int i: {v * 2 + 1, v * 2 + 2}){
apply(i, t[v].p);
}
t[v].p = 0;
}
void upd(int l, int r, ll x, int tl, int tr, int v){
if (l >= r) return;
if (tl == l && tr == r) return void(apply(v, x));
push(v);
int tm = tl + tr >> 1;
upd(l, min(tm, r), x, tl, tm, v * 2 + 1);
upd(max(l, tm), r, x, tm, tr, v * 2 + 2);
t[v] = t[v * 2 + 1] + t[v * 2 + 2];
}
ll get(int l, int r, int tl, int tr, int v){
if (l >= r) return 0LL;
if (tl == l && tr == r) return t[v].ans;
push(v);
int tm = tl + tr >> 1;
return max(get(l, min(tm, r), tl, tm, v * 2 + 1),
get(max(l, tm), r, tm, tr, v * 2 + 2));
}
inline int solve(){
int n; cin >> n;
vec <ll> a(n);
for (auto &i: a) cin >> i;
int q; cin >> q;
vec <vec <pii>> qq(n);
for (int i = 0; i < q; ++i){
int l, r;
cin >> l >> r, --l;
qq[l].push_back({r, i});
}
vec <vec <int>> qu(n);
vec <int> st;
for (int i = 1; i < n; ++i){
while(!st.empty() && a[st.back()] < a[i]){
st.pop_back();
}
if (!st.empty()){
qu[st.back()].push_back(i);
}
st.push_back(i);
}
st.clear();
for (int i = n - 1; ~i; --i){
while(!st.empty() && a[st.back()] < a[i]){
st.pop_back();
}
if (!st.empty()){
qu[i].push_back(st.back());
}
st.push_back(i);
}
build(0, n, 0, a);
vec <ll> ans(q);
for (int l = n - 1; ~l; --l){
for (auto &r: qu[l]){
upd(2 * r - l, n, a[l] + a[r], 0, n, 0);
}
for (auto &[r, id]: qq[l]){
ans[id] = get(0, r, 0, n, 0);
}
}
for (auto &i: ans){
cout << i << "\n";
}
return 0;
}
inline void precalc(){}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tst = 1; //cin >> tst;
precalc();
while(tst--) solve();
return 0;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int, std::vector<long long int>&)':
jumps.cpp:50:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int tm = tl + tr >> 1;
| ~~~^~~~
jumps.cpp: In function 'void upd(int, int, ll, int, int, int)':
jumps.cpp:73:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int tm = tl + tr >> 1;
| ~~~^~~~
jumps.cpp: In function 'll get(int, int, int, int, int)':
jumps.cpp:83:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int tm = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
47308 KB |
Output is correct |
2 |
Correct |
18 ms |
47188 KB |
Output is correct |
3 |
Correct |
18 ms |
47188 KB |
Output is correct |
4 |
Incorrect |
21 ms |
47224 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
47308 KB |
Output is correct |
2 |
Correct |
18 ms |
47188 KB |
Output is correct |
3 |
Correct |
18 ms |
47188 KB |
Output is correct |
4 |
Incorrect |
21 ms |
47224 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
64724 KB |
Output is correct |
2 |
Correct |
82 ms |
65232 KB |
Output is correct |
3 |
Incorrect |
82 ms |
65216 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
47308 KB |
Output is correct |
2 |
Correct |
18 ms |
47188 KB |
Output is correct |
3 |
Correct |
18 ms |
47188 KB |
Output is correct |
4 |
Incorrect |
21 ms |
47224 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |