#include<bits/stdc++.h>
using namespace std;
typedef pair<int , int> ipair;
const int N = 5e5 + 5;
int n , q , a[N] , ans[N];
vector<ipair> Q[N] , e;
void R(){
stack<int> s;
for(int i = 0; i < n; ++i){
while(!s.empty() && a[i] >= a[s.top()]) e.emplace_back(s.top() , i) , s.pop();
if(!s.empty()) e.emplace_back(s.top() , i);
s.push(i);
}
}
struct Node{
int mx , f , a;
}T[1<<18];
Node operator+(const Node &A , const Node &B){
Node ret = A;
ret.mx = max({ret.mx , ret.f + B.a , B.mx});
ret.a = max(ret.a , B.a);
ret.f = max(ret.f , B.f);
return ret;
}
void B(int l = 0 , int r= n - 1 , int pos = 0){
if(l == r){
T[pos].a = a[l]; return;
}
int mid = (l + r)>>1;
B(l , mid , pos * 2 + 1); B(mid + 1 ,r , pos * 2 + 2);
T[pos].a = max(T[pos* 2+ 1].a , T[pos * 2 + 2].a);
}
void upd(int i , int x , int l = 0 , int r = n - 1 , int pos = 0){
if(l == r){
T[pos].mx = max(T[pos].mx , x + T[pos].a);
T[pos].f = max(T[pos].f , x);
return;
}
int mid = (l + r)>>1;
if(i < mid) upd(i ,x , l ,mid, pos * 2 + 1);
else upd(i ,x , mid +1 , r, pos * 2 + 2);
T[pos] = T[pos * 2 + 1] + T[pos * 2 + 2];
}
Node get(int L ,int R , int l = 0 , int r= n - 1 , int pos = 0){
if(r < L || R < l)return {0 , 0, 0};
if(L <= l && r <= R) return T[pos];
int mid = (l + r)>>1;
return get(L , R , l , mid , pos * 2 + 1) + get(L , R ,mid + 1 , r , pos * 2 + 2);
}
int main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i =0; i < n; ++i)cin >> a[i];
cin >> q;
for(int i = 0; i < q; ++i){
int l , r; cin >> l >> r; --l; --r;
Q[l].emplace_back(r , i);
}
R();
B();
sort(e.rbegin() , e.rend());
//for(auto x : e)cout << x.first << " " << x.second << endl;
int P = -1;
for(int l = n - 1; ~l; --l){
while(P + 1 < e.size() && e[P + 1].first >= l){
P++;
int A = e[P].first , B = e[P].second;
int Z = 2 * B - A;
if(Z < n)upd(Z , a[A] + a[B]);
}
for(auto X : Q[l]){
int i = X.second , r = X.first;
ans[i] = get(l , r).mx;
}
}
for(int i= 0; i < q; ++i)cout << ans[i] << " ";
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | while(P + 1 < e.size() && e[P + 1].first >= l){
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
67 ms |
38752 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |