#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int inf = 1000000003;
struct node{
int c;
int ab;
int abc;
};
const int N = 1 << 19;
static node tree[2*N+5];
static int arr[N+5];
node relax(node x, node y){
node z;
z.c = max(x.c, y.c);
z.ab = max(x.ab, y.ab);
z.abc = max(x.ab + y.c, max(x.abc,y.abc));
return z;
}
void init(){
for(int i = N;i < 2*N;i++){
tree[i] = {arr[i-N],-inf,-inf};
}
for(int i = N-1;i > 0;i--){
tree[i] = relax(tree[i<<1],tree[i<<1|1]);
}
}
void update(int i, int v){
i += N;
tree[i].ab = max(tree[i].ab,v);
tree[i].abc = max(tree[i].abc,tree[i].ab+tree[i].c);
while(i > 1){
i >>= 1;
tree[i] = relax(tree[i<<1],tree[i<<1|1]);
}
}
int query(int l, int r){
node L = {-inf,-inf,-inf};
node R = {-inf,-inf,-inf};
for(l += N, r += N;l < r;l >>= 1, r >>= 1){
if(l&1){
L = relax(L,tree[l]);
l++;
}
if(r&1){
r--;
R = relax(tree[r],R);
}
}
return relax(L,R).abc;
}
int main(){
freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0;i < n;i++) cin >> arr[i];
vector<int> critical[n];
vector<ii> queries[n];
stack<int> s;
for(int b = 0;b < n;b++){
while(!s.empty() && arr[b] >= arr[s.top()]){
int a = s.top();
int c = 2 * b - a;
if(c < n) critical[a].push_back(c);
s.pop();
}
if(!s.empty()){
int a = s.top();
int c = 2 * b - a;
if(c < n) critical[a].push_back(c);
}
s.push(b);
}
int q; cin >> q;
int answer[q];
for(int i = 0;i < q;i++){
int l, r; cin >> l >> r;
queries[l-1].push_back(ii(r-1,i));
}
init();
for(int l = n-1;l >= 0;l--){
int a = l;
for(int ab : critical[a]){
int b = (a + ab) / 2;
update(ab, arr[a] + arr[b]);
}
for(ii Q : queries[l]){
int r = Q.first;
answer[Q.second] = query(l, r+1);
}
}
for(int i = 0;i < q;i++) cout << answer[i] << "\n";
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:61:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("i.txt","r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |