#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
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 |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
49244 KB |
Output is correct |
2 |
Correct |
149 ms |
47332 KB |
Output is correct |
3 |
Correct |
152 ms |
48880 KB |
Output is correct |
4 |
Correct |
221 ms |
49032 KB |
Output is correct |
5 |
Correct |
207 ms |
49188 KB |
Output is correct |
6 |
Correct |
201 ms |
48476 KB |
Output is correct |
7 |
Correct |
200 ms |
48344 KB |
Output is correct |
8 |
Correct |
199 ms |
48320 KB |
Output is correct |
9 |
Correct |
203 ms |
48704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |