#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5 + 5;
const int MAX_Q = 5e5 + 5;
const int INF = 1e9 + 7;
int A[MAX_N];
int L[MAX_Q], R[MAX_Q], ans[MAX_Q];
vector <int> queries[MAX_N], updates[MAX_N];
struct Node {
int ans, maxL, maxR;
Node() {}
Node(int a, int ml, int mr) : ans(a), maxL(ml), maxR(mr) {}
Node operator+(const Node &o) const {
int ans_, maxL_, maxR_;
ans_ = max({ans, o.ans, maxL + o.maxR});
maxL_ = max(maxL, o.maxL);
maxR_ = max(maxR, o.maxR);
return Node(ans_, maxL_, maxR_);
}
}tree[4 * MAX_N];
void build(int idx, int l, int r) {
if(l == r) {
return void(tree[idx] = Node(-INF, -INF, A[l]));
}
int mid = (l + r) / 2;
build(idx * 2, l, mid);
build(idx * 2 + 1, mid + 1, r);
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}
void update(int idx, int l, int r, int q, int x) {
if(l == r) {
tree[idx].maxL = max(tree[idx].maxL, x);
tree[idx].ans = max(tree[idx].ans, tree[idx].maxL + tree[idx].maxR);
return;
}
int mid = (l + r) / 2;
if(q <= mid) {
update(idx * 2, l, mid, q, x);
}
else {
update(idx * 2 + 1, mid + 1, r, q, x);
}
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}
Node query(int idx, int l, int r, int ql, int qr) {
if(r < ql or qr < l) {
return Node(-INF, -INF, -INF);
}
if(ql <= l and r <= qr) {
return tree[idx];
}
int mid = (l + r) / 2;
return query(idx * 2, l, mid, ql, qr) + query(idx * 2 + 1, mid + 1, r, ql, qr);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> A[i];
}
int Q;
cin >> Q;
for(int i = 1; i <= Q; i++) {
cin >> L[i] >> R[i];
queries[L[i]].push_back(i);
}
stack <int> stk;
for(int i = 1; i <= N; i++) {
while(!stk.empty() and A[stk.top()] <= A[i]) {
updates[stk.top()].push_back(i);
stk.pop();
}
if(!stk.empty()) {
updates[stk.top()].push_back(i);
}
stk.push(i);
}
build(1, 1, N);
for(int i = N; i >= 1; i--) {
for(auto j : updates[i]) {
if(2 * j - i <= N) {
cout << i << ' ' << j << " : " << A[i] + A[j] << endl;
update(1, 1, N, 2 * j - i, A[i] + A[j]);
}
}
for(auto j : queries[i]) {
cout << i << ' ' << j << " : " << L[j] << ' ' << R[j] << endl;
ans[j] = query(1, 1, N, L[j], R[j]).ans;
}
}
for(int i = 1; i <= Q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
685 ms |
48748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |