#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, --r;
qq[r].push_back({l, 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[i].push_back(st.back());
}
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[st.back()].push_back(i);
}
st.push_back(i);
}
build(0, n, 0, a);
vec <ll> ans(q);
for (int r = 0; r < n; ++r){
for (auto &l: qu[r]){
//// cout << "upd " << l + 1 << " " << r + 1 << " -> ";
int k = max(0, 2 * l - r);
// cout << "[" << k + 1 << " " << l + 1 << ") " << a[l] + a[r] << "\n";
upd(k, l, a[l] + a[r], 0, n, 0);
}
for (auto &[l, id]: qq[r]){
ans[id] = get(l, n, 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 |
19 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
19 ms |
47188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
19 ms |
47188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
66760 KB |
Output is correct |
2 |
Correct |
80 ms |
66992 KB |
Output is correct |
3 |
Correct |
81 ms |
66980 KB |
Output is correct |
4 |
Correct |
147 ms |
66860 KB |
Output is correct |
5 |
Correct |
152 ms |
66820 KB |
Output is correct |
6 |
Correct |
169 ms |
66120 KB |
Output is correct |
7 |
Incorrect |
139 ms |
66100 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
19 ms |
47188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |