#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(5e5)+7;
int n , q , a[maxN] , res[maxN];
vector<ii> query[maxN];
struct node{
int lazy , base , val;
node(){
lazy = base = val = 0;
}
} st[4 * maxN];
#define lef(id) id * 2
#define rig(id) id * 2 + 1
void build(int id , int l , int r){
if (l == r){
st[id].base = a[l];
return;
}
int mid = (l + r) / 2;
build(lef(id) , l , mid);
build(rig(id) , mid + 1 , r);
st[id].base = max(st[lef(id)].base , st[rig(id)].base);
}
void modify(int id , int x){
st[id].lazy = max(st[id].lazy , x);
st[id].val = max(st[id].val , st[id].base + x);
}
void down(int id , int l , int r){
int &x = st[id].lazy;
if (l == r) return;
modify(lef(id) , x);
modify(rig(id) , x);
}
void update(int id , int l , int r , int u , int v , int x){
if (v < l || r < u) return;
if (u <= l && r <= v) return modify(id , x);
down(id , l , r);
int mid = (l + r) / 2;
update(lef(id) , l , mid , u , v , x);
update(rig(id) , mid + 1 , r , u , v , x);
st[id].val = max(st[lef(id)].val , st[rig(id)].val);
}
int get(int id , int l , int r , int u , int v){
if (v < l || r < u) return 0;
if (u <= l && r <= v) return st[id].val;
down(id , l , r);
int mid = (l + r) / 2;
return max(get(lef(id) , l , mid , u , v) , get(rig(id) , mid + 1 , r , u , v));
}
void update(int x , int y){
int z = 2 * y - x;
update(1 , 1 , n , z , n , a[x] + a[y]);
}
void solve(){
cin >> n;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
build(1 , 1 , n);
cin >> q;
for (int i = 1 ; i <= q ; i++){
int l , r;
cin >> l >> r;
query[l].push_back({r , i});
}
deque<int> dq;
for (int i = n ; i >= 1 ; i--){
while (dq.empty() == 0 && a[i] >= a[dq.back()]){
update(i , dq.back());
dq.pop_back();
}
if (dq.empty() == 0) update(i , dq.back());
for (auto it : query[i]){
res[it.se] = get(1 , 1 , n , i , it.fi);
}
dq.push_back(i);
}
for (int i = 1 ; i <= q ; i++) cout << res[i] << "\n";
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35664 KB |
Output is correct |
2 |
Correct |
22 ms |
35560 KB |
Output is correct |
3 |
Correct |
22 ms |
35664 KB |
Output is correct |
4 |
Correct |
22 ms |
35664 KB |
Output is correct |
5 |
Correct |
24 ms |
35632 KB |
Output is correct |
6 |
Correct |
22 ms |
35656 KB |
Output is correct |
7 |
Correct |
24 ms |
35664 KB |
Output is correct |
8 |
Correct |
22 ms |
35664 KB |
Output is correct |
9 |
Correct |
24 ms |
35664 KB |
Output is correct |
10 |
Correct |
23 ms |
35664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35664 KB |
Output is correct |
2 |
Correct |
22 ms |
35560 KB |
Output is correct |
3 |
Correct |
22 ms |
35664 KB |
Output is correct |
4 |
Correct |
22 ms |
35664 KB |
Output is correct |
5 |
Correct |
24 ms |
35632 KB |
Output is correct |
6 |
Correct |
22 ms |
35656 KB |
Output is correct |
7 |
Correct |
24 ms |
35664 KB |
Output is correct |
8 |
Correct |
22 ms |
35664 KB |
Output is correct |
9 |
Correct |
24 ms |
35664 KB |
Output is correct |
10 |
Correct |
23 ms |
35664 KB |
Output is correct |
11 |
Correct |
230 ms |
49480 KB |
Output is correct |
12 |
Correct |
228 ms |
54092 KB |
Output is correct |
13 |
Correct |
219 ms |
54120 KB |
Output is correct |
14 |
Correct |
224 ms |
54172 KB |
Output is correct |
15 |
Correct |
218 ms |
54124 KB |
Output is correct |
16 |
Correct |
220 ms |
53580 KB |
Output is correct |
17 |
Correct |
221 ms |
53572 KB |
Output is correct |
18 |
Correct |
242 ms |
53580 KB |
Output is correct |
19 |
Correct |
251 ms |
54192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
36424 KB |
Output is correct |
2 |
Correct |
75 ms |
37192 KB |
Output is correct |
3 |
Correct |
77 ms |
36468 KB |
Output is correct |
4 |
Correct |
106 ms |
36432 KB |
Output is correct |
5 |
Correct |
107 ms |
36428 KB |
Output is correct |
6 |
Correct |
113 ms |
37192 KB |
Output is correct |
7 |
Correct |
104 ms |
36228 KB |
Output is correct |
8 |
Correct |
105 ms |
36260 KB |
Output is correct |
9 |
Correct |
110 ms |
37460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35664 KB |
Output is correct |
2 |
Correct |
22 ms |
35560 KB |
Output is correct |
3 |
Correct |
22 ms |
35664 KB |
Output is correct |
4 |
Correct |
22 ms |
35664 KB |
Output is correct |
5 |
Correct |
24 ms |
35632 KB |
Output is correct |
6 |
Correct |
22 ms |
35656 KB |
Output is correct |
7 |
Correct |
24 ms |
35664 KB |
Output is correct |
8 |
Correct |
22 ms |
35664 KB |
Output is correct |
9 |
Correct |
24 ms |
35664 KB |
Output is correct |
10 |
Correct |
23 ms |
35664 KB |
Output is correct |
11 |
Correct |
230 ms |
49480 KB |
Output is correct |
12 |
Correct |
228 ms |
54092 KB |
Output is correct |
13 |
Correct |
219 ms |
54120 KB |
Output is correct |
14 |
Correct |
224 ms |
54172 KB |
Output is correct |
15 |
Correct |
218 ms |
54124 KB |
Output is correct |
16 |
Correct |
220 ms |
53580 KB |
Output is correct |
17 |
Correct |
221 ms |
53572 KB |
Output is correct |
18 |
Correct |
242 ms |
53580 KB |
Output is correct |
19 |
Correct |
251 ms |
54192 KB |
Output is correct |
20 |
Correct |
114 ms |
36424 KB |
Output is correct |
21 |
Correct |
75 ms |
37192 KB |
Output is correct |
22 |
Correct |
77 ms |
36468 KB |
Output is correct |
23 |
Correct |
106 ms |
36432 KB |
Output is correct |
24 |
Correct |
107 ms |
36428 KB |
Output is correct |
25 |
Correct |
113 ms |
37192 KB |
Output is correct |
26 |
Correct |
104 ms |
36228 KB |
Output is correct |
27 |
Correct |
105 ms |
36260 KB |
Output is correct |
28 |
Correct |
110 ms |
37460 KB |
Output is correct |
29 |
Correct |
735 ms |
65360 KB |
Output is correct |
30 |
Correct |
616 ms |
67352 KB |
Output is correct |
31 |
Correct |
633 ms |
65356 KB |
Output is correct |
32 |
Correct |
735 ms |
65356 KB |
Output is correct |
33 |
Correct |
686 ms |
65356 KB |
Output is correct |
34 |
Correct |
660 ms |
63052 KB |
Output is correct |
35 |
Correct |
686 ms |
62796 KB |
Output is correct |
36 |
Correct |
724 ms |
62696 KB |
Output is correct |
37 |
Correct |
674 ms |
64292 KB |
Output is correct |
38 |
Correct |
532 ms |
70948 KB |
Output is correct |
39 |
Correct |
521 ms |
70988 KB |
Output is correct |
40 |
Correct |
560 ms |
67660 KB |
Output is correct |
41 |
Correct |
499 ms |
67148 KB |
Output is correct |
42 |
Correct |
511 ms |
67164 KB |
Output is correct |
43 |
Correct |
499 ms |
68940 KB |
Output is correct |
44 |
Correct |
540 ms |
70380 KB |
Output is correct |
45 |
Correct |
567 ms |
70492 KB |
Output is correct |
46 |
Correct |
555 ms |
67460 KB |
Output is correct |
47 |
Correct |
553 ms |
66792 KB |
Output is correct |
48 |
Correct |
526 ms |
66804 KB |
Output is correct |
49 |
Correct |
545 ms |
68940 KB |
Output is correct |
50 |
Correct |
607 ms |
68720 KB |
Output is correct |
51 |
Correct |
581 ms |
68520 KB |
Output is correct |
52 |
Correct |
577 ms |
66124 KB |
Output is correct |
53 |
Correct |
565 ms |
65844 KB |
Output is correct |
54 |
Correct |
578 ms |
65868 KB |
Output is correct |
55 |
Correct |
589 ms |
67404 KB |
Output is correct |