#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
template <class Info, class Tag>
class LazySegtree {
private:
const int n;
vector<Info> t;
vector<Tag> lz;
void build(int v, int l, int r, const vector<Info> &a) {
if (l == r) {
t[v] = a[l];
} else {
int m = (l + r) >> 1;
build(2 * v, l, m, a);
build(2 * v + 1, m + 1, r, a);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
void apply(int v, int l, int r, const Tag &x) {
t[v].apply(x, l, r);
lz[v].apply(x);
}
void pushdown(int v, int l, int r) {
int m = (l + r) >> 1;
apply(2 * v, l, m, lz[v]);
apply(2 * v + 1, m + 1, r, lz[v]);
lz[v] = Tag();
}
void upd(int v, int l, int r, int ql, int qr, const Tag &x) {
if (qr < l || ql > r) { return; }
if (ql <= l && r <= qr) {
apply(v, l, r, x);
} else {
pushdown(v, l, r);
int m = (l + r) >> 1;
upd(2 * v, l, m, ql, qr, x);
upd(2 * v + 1, m + 1, r, ql, qr, x);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
Info qry(int v, int l, int r, int ql, int qr) {
if (qr < l || ql > r) return Info();
if (l >= ql && r <= qr) return t[v];
pushdown(v, l, r);
int m = (l + r) >> 1;
return qry(2 * v, l, m, ql, qr) + qry(2 * v + 1, m + 1, r, ql, qr);
}
public:
LazySegtree() {}
LazySegtree(int n) : n(n) {
t.assign(4 << __lg(n), Info());
lz.assign(4 << __lg(n), Tag());
}
LazySegtree(const vector<Info> &a) : n(a.size()) {
t.assign(4 << __lg(n), Info());
lz.assign(4 << __lg(n), Tag());
build(1, 0, n - 1, a);
}
/** updates [ql, qr] with the arbitrary update chosen */
void upd(int ql, int qr, const Tag &x) {
upd(1, 0, n - 1, ql, qr, x);
}
/** @return result of range query on [ql, qr] */
Info qry(int ql, int qr) {
return qry(1, 0, n - 1, ql, qr);
}
};
struct Tag {
ll add = 0;
void apply(const Tag &t) {
add = max(add, t.add);
}
};
struct Info {
ll val = 0, best = 0;
void apply(const Tag &t, int l, int r) {
best = max(best, val + t.add);
}
};
/** @return result of joining nodes a and b together */
Info operator+(const Info &a, const Info &b) {
ll nb = max(a.best, b.best);
return {max(a.val, b.val), nb};
}
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int &i : a) { cin >> i; }
vector<vector<pii>> tests(n);
int q; cin >> q;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
tests[l].push_back({r, i});
}
vector<Info> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = {a[i], a[i]};
}
LazySegtree<Info, Tag> st(arr);
vector<ll> res(q);
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
int nxt = j + (j - i);
if (nxt >= n) { break; }
st.upd(nxt, n - 1, {a[i] + a[j]});
}
for (const auto [r, id] : tests[i]) {
const auto [val, best] = st.qry(i, r);
res[id] = best;
}
}
for (int i = 0; i < q; i++) {
cout << res[i] << "\n";
}
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int t = 1; // cin >> t;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1136 ms |
21484 KB |
Output is correct |
12 |
Correct |
1164 ms |
21576 KB |
Output is correct |
13 |
Correct |
1101 ms |
21520 KB |
Output is correct |
14 |
Correct |
1058 ms |
21504 KB |
Output is correct |
15 |
Correct |
1165 ms |
21524 KB |
Output is correct |
16 |
Correct |
1095 ms |
21012 KB |
Output is correct |
17 |
Correct |
1059 ms |
20808 KB |
Output is correct |
18 |
Correct |
1159 ms |
20700 KB |
Output is correct |
19 |
Correct |
1123 ms |
21448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4046 ms |
23120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1136 ms |
21484 KB |
Output is correct |
12 |
Correct |
1164 ms |
21576 KB |
Output is correct |
13 |
Correct |
1101 ms |
21520 KB |
Output is correct |
14 |
Correct |
1058 ms |
21504 KB |
Output is correct |
15 |
Correct |
1165 ms |
21524 KB |
Output is correct |
16 |
Correct |
1095 ms |
21012 KB |
Output is correct |
17 |
Correct |
1059 ms |
20808 KB |
Output is correct |
18 |
Correct |
1159 ms |
20700 KB |
Output is correct |
19 |
Correct |
1123 ms |
21448 KB |
Output is correct |
20 |
Execution timed out |
4046 ms |
23120 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |