#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first
#define vs second
const int mod = 1000000007;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
struct Seg{
vector<int> mx,ans;
vector<int> lazy;
int siz;
void init(int n){
siz = 1;
while(siz < n)siz*=2;
mx.assign(siz * 2 , 0);
ans.assign(siz * 2 , 0);
lazy.assign(siz * 2 , -1);
}
void push(int x){
lazy[2 * x + 1] = max(lazy[2 * x + 1] , lazy[x]);
lazy[2 * x + 2] = max(lazy[2 * x + 2] , lazy[x]);
ans[2 * x + 1] = max(ans[2 * x + 1] , lazy[2 * x + 1] + mx[2 * x + 1]);
ans[2 * x + 2] = max(ans[2 * x + 2] , lazy[2 * x + 2] + mx[2 * x + 2]);
lazy[x] = -1;
}
void build(vector<int>& a , int x , int lx , int rx){
if(rx - lx == 1){
if(lx < a.size()){
mx[x] = ans[x] = a[lx];
}
return;
}
int m = (lx + rx)/2;
build(a , 2 * x + 1 ,lx , m);
build(a , 2 * x + 2 , m , rx);
mx[x] = max(mx[2 * x + 1] , mx[2 * x + 2]);
ans[x] = max(ans[2 *x + 1] , ans[2 * x + 2]);
}
void build(vector<int>& a){
build(a , 0 , 0 , siz);
}
void set(int l , int r, int val, int x , int lx, int rx){
if(r <= lx || rx <= l)return;
if(lx >= l && rx <= r){
lazy[x] = max(lazy[x], val);
ans[x] = max(ans[x] , mx[x] + val);
return;
}
push(x);
int m = (lx + rx)/2;
set(l , r , val , 2 * x + 1, lx , m);
set(l , r , val , 2 * x + 2 , m , rx);
ans[x] = max(ans[2 * x + 1] , ans[2 * x + 2]);
}
void set(int l , int r , int val){
set(l , r , val , 0 , 0 , siz);
}
int range_op(int l , int r , int x , int lx, int rx){
if(rx <= l || r <= lx)return -1;
if(lx >= l && rx <= r){
return ans[x];
}
push(x);
int m = (lx + rx)/2;
return max(range_op(l , r , 2 * x + 1 , lx , m) , range_op(l , r , 2 * x + 2 , m , rx));
}
int range_op(int l ,int r){
return range_op(l , r , 0 , 0 ,siz);
}
};
void solve(){
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
Seg st;
st.init(n + 1);
st.build(a);
stack<int> stk;
vector<int> right[n + 1];
for(int i = 0; i < n; i++){
while(stk.size() && a[stk.top()] < a[i]){
stk.pop();
}
if(stk.size()){
//cout << stk.top() << ' ' << i << '\n';
right[stk.top()].pb(i);
}
stk.push(i);
}
while(stk.size())stk.pop();
for(int i = n - 1; i >= 0; i--){
while(stk.size() && a[stk.top()] < a[i]){
stk.pop();
}
if(stk.size()){
//cout << stk.top() << ' ' << i << '\n';
right[i].pb(stk.top());
}
stk.push(i);
}
int q;
cin >> q;
vector<pair<int,int>> queries[n + 1];
int ans[q];
for(int i = 0; i < q; i++){
int l , r;
cin >> l >> r;
l--;
r--;
queries[l].pb(mp(r , i));
}
for(int i = n - 1; i >= 0; i--){
for(int j : right[i]){
int r = j + (j - i);
if(r < n){
st.set(r , n , a[i] + a[j]);
}
}
for(auto j : queries[i]){
ans[j.vs] = st.range_op(i , j.vf + 1);
}
}
for(int i = 0; i < q; i++){
cout << ans[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;
//cin >> t;
//while(t--)
solve();
}
Compilation message
jumps.cpp: In member function 'void Seg::build(std::vector<int>&, int, int, int)':
jumps.cpp:37:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if(lx < a.size()){
| ~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
215 ms |
19536 KB |
Output is correct |
12 |
Correct |
197 ms |
19492 KB |
Output is correct |
13 |
Correct |
200 ms |
19600 KB |
Output is correct |
14 |
Correct |
211 ms |
19540 KB |
Output is correct |
15 |
Correct |
197 ms |
19540 KB |
Output is correct |
16 |
Correct |
208 ms |
19016 KB |
Output is correct |
17 |
Correct |
206 ms |
18984 KB |
Output is correct |
18 |
Correct |
203 ms |
18896 KB |
Output is correct |
19 |
Correct |
201 ms |
19496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
24916 KB |
Output is correct |
2 |
Correct |
115 ms |
25628 KB |
Output is correct |
3 |
Correct |
105 ms |
25620 KB |
Output is correct |
4 |
Correct |
186 ms |
24912 KB |
Output is correct |
5 |
Correct |
178 ms |
24924 KB |
Output is correct |
6 |
Correct |
186 ms |
24140 KB |
Output is correct |
7 |
Correct |
179 ms |
24144 KB |
Output is correct |
8 |
Correct |
176 ms |
24148 KB |
Output is correct |
9 |
Correct |
191 ms |
24660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
215 ms |
19536 KB |
Output is correct |
12 |
Correct |
197 ms |
19492 KB |
Output is correct |
13 |
Correct |
200 ms |
19600 KB |
Output is correct |
14 |
Correct |
211 ms |
19540 KB |
Output is correct |
15 |
Correct |
197 ms |
19540 KB |
Output is correct |
16 |
Correct |
208 ms |
19016 KB |
Output is correct |
17 |
Correct |
206 ms |
18984 KB |
Output is correct |
18 |
Correct |
203 ms |
18896 KB |
Output is correct |
19 |
Correct |
201 ms |
19496 KB |
Output is correct |
20 |
Correct |
188 ms |
24916 KB |
Output is correct |
21 |
Correct |
115 ms |
25628 KB |
Output is correct |
22 |
Correct |
105 ms |
25620 KB |
Output is correct |
23 |
Correct |
186 ms |
24912 KB |
Output is correct |
24 |
Correct |
178 ms |
24924 KB |
Output is correct |
25 |
Correct |
186 ms |
24140 KB |
Output is correct |
26 |
Correct |
179 ms |
24144 KB |
Output is correct |
27 |
Correct |
176 ms |
24148 KB |
Output is correct |
28 |
Correct |
191 ms |
24660 KB |
Output is correct |
29 |
Correct |
919 ms |
82256 KB |
Output is correct |
30 |
Correct |
666 ms |
83536 KB |
Output is correct |
31 |
Correct |
690 ms |
81508 KB |
Output is correct |
32 |
Correct |
869 ms |
82260 KB |
Output is correct |
33 |
Correct |
907 ms |
82256 KB |
Output is correct |
34 |
Correct |
866 ms |
79996 KB |
Output is correct |
35 |
Correct |
878 ms |
79488 KB |
Output is correct |
36 |
Correct |
1004 ms |
79696 KB |
Output is correct |
37 |
Correct |
906 ms |
80936 KB |
Output is correct |
38 |
Correct |
735 ms |
87940 KB |
Output is correct |
39 |
Correct |
784 ms |
87892 KB |
Output is correct |
40 |
Correct |
703 ms |
84628 KB |
Output is correct |
41 |
Correct |
680 ms |
84048 KB |
Output is correct |
42 |
Correct |
746 ms |
84048 KB |
Output is correct |
43 |
Correct |
714 ms |
85844 KB |
Output is correct |
44 |
Correct |
728 ms |
87264 KB |
Output is correct |
45 |
Correct |
767 ms |
87264 KB |
Output is correct |
46 |
Correct |
715 ms |
84160 KB |
Output is correct |
47 |
Correct |
728 ms |
83596 KB |
Output is correct |
48 |
Correct |
710 ms |
83596 KB |
Output is correct |
49 |
Correct |
737 ms |
86016 KB |
Output is correct |
50 |
Correct |
785 ms |
85332 KB |
Output is correct |
51 |
Correct |
909 ms |
85332 KB |
Output is correct |
52 |
Correct |
784 ms |
82876 KB |
Output is correct |
53 |
Correct |
822 ms |
82512 KB |
Output is correct |
54 |
Correct |
829 ms |
82512 KB |
Output is correct |
55 |
Correct |
793 ms |
84304 KB |
Output is correct |