This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~ Be Name Khoda ~ //
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 5e5 + 10;
const int maxnt = 2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int a[maxn5];
vector <int> good[maxn5];
vector <pair<int, int>> req[maxn5];
ll out[maxn5];
namespace seg{
ll mx[maxnt];
int lazy[maxnt], cons[maxnt];
void build(int l, int r, int v){
if(r - l == 1){
cons[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, v * 2);
build(mid, r, v * 2 + 1);
cons[v] = max(cons[v * 2], cons[v * 2 + 1]);
}
void shift(int v){
if(lazy[v] > lazy[v * 2]){
lazy[v * 2] = lazy[v];
mx[v * 2] = max(mx[v * 2], cons[v * 2] + ll(lazy[v]));
}
if(lazy[v] > lazy[v * 2 + 1]){
lazy[v * 2 + 1] = lazy[v];
mx[v * 2 + 1] = max(mx[v * 2 + 1], cons[v * 2 + 1] + ll(lazy[v]));
}
lazy[v] = 0;
}
void max_mos(int l, int r, int lq, int rq, int val, int v){
if(rq <= l || r <= lq || val <= lazy[v])
return;
if(lq <= l && r <= rq){
lazy[v] = max(lazy[v], val);
mx[v] = max(mx[v], cons[v] + ll(val));
return;
}
int mid = (l + r) >> 1;
shift(v);
max_mos(l, mid, lq, rq, val, v * 2);
max_mos(mid, r, lq, rq, val, v * 2 + 1);
mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
}
ll get_max(int l, int r, int lq, int rq, int v){
if(rq <= l || r <= lq)
return 0;
if(lq <= l && r <= rq)
return mx[v];
int mid = (l + r) >> 1;
shift(v);
return max(get_max(l, mid, lq, rq, v * 2), get_max(mid, r, lq, rq, v * 2 + 1));
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector <int> av;
for(int i = 0; i < n; i++){
cin >> a[i];
while(av.size() && a[av.back()] < a[i]){
good[av.back()].pb(i);
av.pop_back();
}
if(av.size())
good[av.back()].pb(i);
av.pb(i);
}
int q; cin >> q;
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r;
l--; r--;
req[l].pb({r, i});
}
seg::build(0, n, 1);
for(int i = n - 1; i >= 0; i--){
for(auto x : good[i]){
int len = x - i;
seg::max_mos(0, n, x + len, n, a[i] + a[x], 1);
}
for(auto [r, id] : req[i])
out[id] = seg::get_max(0, n, i, r + 1, 1);
}
for(int i = 0; i < q; i++)
cout << out[i] << '\n';
}
# | 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... |