#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
}
const int N = 5e5+5;
int n, q, a[N], ans[N];
// segment tree
int nax[N << 2], spec[N << 2], cand[N << 2], lazy[N << 2];
void apply(int id) {
if (lazy[id] == 0) return;
int k = id << 1;
lazy[k] = max(lazy[k], lazy[id]);
spec[k] = max(spec[k], lazy[id]);
cand[k] = max(cand[k], spec[k] + nax[k]);
lazy[k+1] = max(lazy[k+1], lazy[id]);
spec[k+1] = max(spec[k+1], lazy[id]);
cand[k+1] = max(cand[k+1], spec[k+1] + nax[k+1]);
lazy[id] = 0;
}
void build(int id = 1, int lb = 1, int rb = n) {
if (lb ^ rb) {
int k = id << 1, mb = (lb + rb) >> 1;
build(k, lb, mb);
build(k+1, mb+1, rb);
nax[id] = max(nax[k], nax[k+1]);
}
else {
nax[id] = a[lb];
}
}
void update(int l, int v, int id = 1, int lb = 1, int rb = n) {
// cout << lb << " " << rb << "\n";
if (l <= lb) {
lazy[id] = v;
spec[id] = max(spec[id], v);
cand[id] = max(cand[id], nax[id] + spec[id]);
return;
}
else if (rb < l) {
return;
}
int k = id << 1, mb = (lb + rb) >> 1;
apply(id);
update(l, v, k, lb, mb);
update(l, v, k+1, mb+1, rb);
cand[id] = max(cand[k], cand[k+1]);
}
int question(int l, int r, int id = 1, int lb = 1, int rb = n) {
if (l <= lb && rb <= r) {
return cand[id];
}
else if (rb < l || lb > r) {
return 0;
}
int k = id << 1, mb = (lb + rb) >> 1;
apply(id);
return max(question(l, r, k, lb, mb), question(l, r, k+1, mb+1, rb));
}
// querys
vector<pair<ii, int>> querys;
int main() {
setIO();
cin >> n;
foru(i, 1, n) cin >> a[i];
build();
cin >> q;
foru(i, 1, q) {
int l, r;
cin >> l >> r;
querys.push_back({{l, r}, i});
}
sort(all(querys), greater<pair<ii, int>>());
stack<int> st;
int j = 0;
ford(i, n, 1) {
while (!st.empty()) {
int j = st.top();
if (2*j-i <= n) {
// cout << "upd " << i << " " << j << " at " << 2*j-i << " value " << a[i] + a[j] << "\n";
update(2*j-i, a[i] + a[j]);
}
if (a[j] <= a[i]) st.pop();
else break;
}
st.push(i);
while (j < q && querys[j].first.first == i) {
auto ask = querys[j];
int l = ask.first.first, r = ask.first.second, id = ask.second;
ans[id] = question(l, r);
// cout << "ques " << l << " " << r << " " << ans[id] << "\n";
++j;
}
}
foru(i, 1, q) {
cout << ans[i] << "\n";
}
}
Compilation message
jumps.cpp: In function 'void setIO()':
jumps.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
16264 KB |
Output is correct |
2 |
Correct |
49 ms |
17092 KB |
Output is correct |
3 |
Correct |
52 ms |
16580 KB |
Output is correct |
4 |
Correct |
87 ms |
16196 KB |
Output is correct |
5 |
Correct |
104 ms |
16212 KB |
Output is correct |
6 |
Correct |
97 ms |
16212 KB |
Output is correct |
7 |
Correct |
81 ms |
16212 KB |
Output is correct |
8 |
Correct |
90 ms |
16088 KB |
Output is correct |
9 |
Correct |
83 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |