This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5+10;
int n;
ll a[N];
deque<pair<int,int>>q;
vector<pair<int,int>> rel;
ll ab[N<<2], c[N<<2], sum[N<<2];
void updAB(int node, int L, int R, int pos, ll x) {
if(L == R) {
if(pos != L) return;
ab[node] = x;
sum[node] = ab[node] + c[node];
return;
} int mid = (L + R) >> 1, lc = node << 1, rc = lc | 1;
if(pos <= mid) updAB(lc, L, mid, pos, x);
else updAB(rc, mid + 1, R, pos, x);
sum[node] = max({sum[lc], sum[rc], ab[lc] + c[rc]});
ab[node] = max(ab[lc], ab[rc]);
c[node] = max(c[lc], c[rc]);
}
void updC(int node, int L, int R, int pos, ll x) {
if(L == R) {
if(pos != L) return;
c[node] = x;
sum[node] = ab[node] + c[node];
return;
} int mid = (L + R) >> 1, lc = node << 1, rc = lc | 1;
if(pos <= mid) updC(lc, L, mid, pos, x);
else updC(rc, mid + 1, R, pos, x);
sum[node] = max({sum[lc], sum[rc], ab[lc] + c[rc]});
ab[node] = max(ab[lc], ab[rc]);
c[node] = max(c[lc], c[rc]);
}
pair<ll,pair<ll,ll>> get(int node, int L, int R, int l, int r) {
if(r < L || R < l) return {0,{0,0}};
if(l <= L && R <= r) return {sum[node],{ab[node],c[node]}};
int mid = (L + R) >> 1, lc = node << 1, rc = lc | 1;
auto le = get(lc, L, mid, l, r);
auto ri = get(rc, mid + 1, R, l, r); pair<ll,pair<ll,ll>> ret;
ret.first = max({le.first, ri.first, le.second.first + ri.second.second});
ret.second.first = max(le.second.first, ri.second.first);
ret.second.second = max(le.second.second, ri.second.second);
return ret;
}
vector<pair<int,int>> off[N];
vector<int> candi[N];
ll ans[N];
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
q.push_back({INT_MAX, -1});
for(int i = 1; i <= n; i++) {
while(q.back().first < a[i]) {
rel.push_back({q.back().second, i});
q.pop_back();
}
if(q.back().second + 1) rel.push_back({q.back().second, i});
q.push_back({a[i], i});
}
for(auto it : rel) candi[it.first].push_back(it.second);
int m; scanf("%d", &m);
for(int i = 0; i < m; i++) {
int l, r; scanf("%d %d", &l, &r);
off[l].push_back({r, i});
}
for(int i = n; i > 0; i--) {
updC(1, 1, n, i, a[i]);
for(auto it : candi[i]) updAB(1, 1, n, 2 * it - i, a[i] + a[it]);
for(auto qu : off[i]) {
auto got = get(1, 1, n, i, qu.first);
ans[qu.second] = got.first;
}
}
for(int i = 0; i < m; i++)
printf("%lld\n", ans[i]);
return 0;
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
jumps.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a[i]);
~~~~~^~~~~~~~~~~~~~~
jumps.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int m; scanf("%d", &m);
~~~~~^~~~~~~~~~
jumps.cpp:78:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int l, r; scanf("%d %d", &l, &r);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |