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;
struct node{
int s, e, m, val;
node *l, *r;
node (int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1, val = 0;
if (s != e){
l = new node(s, m);
r = new node(m + 1, e);
}
}
void update(int p, int v){
val = max(val, v);
if (s == e) return;
else if (p <= m) l->update(p, v);
else r->update(p, v);
}
int query(int x, int y){
if (x == s && y == e) return val;
else if (y <= m) return l->query(x, y);
else if (x > m) return r->query(x, y);
else return max(l->query(x, m), r->query(m + 1, y));
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nums; cin >> nums;
vector<int> vec(nums + 1);
for (int i = 1; i <= nums; i++) cin >> vec[i];
int queries; cin >> queries;
vector<tuple<int, int, int>> qvec(queries);
for (int i = 0; i < queries; i++){
int l, r; cin >> l >> r;
qvec[i] = {r, l, i};
}
sort(qvec.begin(), qvec.end());
vector<int> ans(queries);
node *root = new node(1, nums), *rootmx = new node(1, nums);
for (int i = 1; i <= nums; i++) rootmx->update(i, vec[i]);
int curr = 2;
for (auto [r, l, qind] : qvec){
while (curr < r){
curr++;
for (int i = curr - 2; i >= 1; i--) root->update(i, vec[i] + rootmx->query(i + 1, (i + curr) >> 1) + vec[curr]);
}
ans[qind] = root->query(l, r);
}
for (int x : ans) cout << x << '\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... |