#include <bits/stdc++.h>
using namespace std;
constexpr int SZ = 1 << 19;
struct Node{
int a, b, v;
Node(){ a = b = v = 0; }
Node(int a, int b, int v) : a(a), b(b), v(v) {}
friend Node operator + (const Node &l, const Node &r){
return { max(l.a, r.a), max(l.b, r.b), max({ l.v, r.v, l.a + r.b }) };
}
} T[SZ << 1];
int N, Q, A[SZ], R[SZ];
vector<pair<int,int>> Upd[SZ], Qry[SZ];
void Init(){
for(int i=0; i<SZ; i++) T[i|SZ].b = T[i|SZ].v = A[i];
for(int i=SZ-1; i; i--) T[i] = T[i<<1] + T[i<<1|1];
}
void Update(int x, int v){
x |= SZ;
T[x].a = max(T[x].a, v);
T[x].v = T[x].a + T[x].b;
while(x >>= 1) T[x] = T[x<<1] + T[x<<1|1];
}
int Query(int l, int r){
Node lv, rv; l |= SZ; r |= SZ;
while(l <= r){
if(l & 1) lv = lv + T[l++];
if(~r & 1) rv = T[r--] + rv;
l >>= 1; r >>= 1;
}
return (lv + rv).v;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N;
for(int i=1; i<=N; i++) cin >> A[i];
cin >> Q;
for(int i=1,l,r; i<=Q; i++) cin >> l >> r, Qry[l].emplace_back(r, i);
Init();
stack<int> S;
for(int i=1; i<=N; i++){
while(!S.empty()){
int j = S.top();
Upd[j].emplace_back(i, A[i] + A[j]);
if(A[i] <= A[j]) break;
S.pop();
}
S.push(i);
}
for(int i=N; i>=1; i--){
for(auto [idx,val] : Upd[i]) if(2*idx - i <= N) Update(2*idx - i, val);
for(auto [end,idx] : Qry[i]) R[idx] = Query(i, end);
}
for(int i=1; i<=Q; i++) cout << R[i] << "\n";
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:59:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for(auto [idx,val] : Upd[i]) if(2*idx - i <= N) Update(2*idx - i, val);
| ^
jumps.cpp:60:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for(auto [end,idx] : Qry[i]) R[idx] = Query(i, end);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
37196 KB |
Output is correct |
2 |
Correct |
25 ms |
37264 KB |
Output is correct |
3 |
Correct |
21 ms |
37180 KB |
Output is correct |
4 |
Correct |
20 ms |
37160 KB |
Output is correct |
5 |
Correct |
22 ms |
37264 KB |
Output is correct |
6 |
Correct |
20 ms |
37248 KB |
Output is correct |
7 |
Correct |
21 ms |
37196 KB |
Output is correct |
8 |
Correct |
23 ms |
37256 KB |
Output is correct |
9 |
Correct |
21 ms |
37196 KB |
Output is correct |
10 |
Correct |
22 ms |
37324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
37196 KB |
Output is correct |
2 |
Correct |
25 ms |
37264 KB |
Output is correct |
3 |
Correct |
21 ms |
37180 KB |
Output is correct |
4 |
Correct |
20 ms |
37160 KB |
Output is correct |
5 |
Correct |
22 ms |
37264 KB |
Output is correct |
6 |
Correct |
20 ms |
37248 KB |
Output is correct |
7 |
Correct |
21 ms |
37196 KB |
Output is correct |
8 |
Correct |
23 ms |
37256 KB |
Output is correct |
9 |
Correct |
21 ms |
37196 KB |
Output is correct |
10 |
Correct |
22 ms |
37324 KB |
Output is correct |
11 |
Correct |
184 ms |
55876 KB |
Output is correct |
12 |
Correct |
175 ms |
55704 KB |
Output is correct |
13 |
Correct |
186 ms |
55944 KB |
Output is correct |
14 |
Correct |
184 ms |
55876 KB |
Output is correct |
15 |
Correct |
188 ms |
55904 KB |
Output is correct |
16 |
Correct |
188 ms |
55236 KB |
Output is correct |
17 |
Correct |
184 ms |
55176 KB |
Output is correct |
18 |
Correct |
189 ms |
55136 KB |
Output is correct |
19 |
Correct |
183 ms |
55688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
47172 KB |
Output is correct |
2 |
Correct |
66 ms |
46028 KB |
Output is correct |
3 |
Correct |
64 ms |
46736 KB |
Output is correct |
4 |
Correct |
99 ms |
47132 KB |
Output is correct |
5 |
Correct |
99 ms |
47244 KB |
Output is correct |
6 |
Correct |
102 ms |
46604 KB |
Output is correct |
7 |
Correct |
96 ms |
46480 KB |
Output is correct |
8 |
Correct |
97 ms |
46492 KB |
Output is correct |
9 |
Correct |
99 ms |
46816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
37196 KB |
Output is correct |
2 |
Correct |
25 ms |
37264 KB |
Output is correct |
3 |
Correct |
21 ms |
37180 KB |
Output is correct |
4 |
Correct |
20 ms |
37160 KB |
Output is correct |
5 |
Correct |
22 ms |
37264 KB |
Output is correct |
6 |
Correct |
20 ms |
37248 KB |
Output is correct |
7 |
Correct |
21 ms |
37196 KB |
Output is correct |
8 |
Correct |
23 ms |
37256 KB |
Output is correct |
9 |
Correct |
21 ms |
37196 KB |
Output is correct |
10 |
Correct |
22 ms |
37324 KB |
Output is correct |
11 |
Correct |
184 ms |
55876 KB |
Output is correct |
12 |
Correct |
175 ms |
55704 KB |
Output is correct |
13 |
Correct |
186 ms |
55944 KB |
Output is correct |
14 |
Correct |
184 ms |
55876 KB |
Output is correct |
15 |
Correct |
188 ms |
55904 KB |
Output is correct |
16 |
Correct |
188 ms |
55236 KB |
Output is correct |
17 |
Correct |
184 ms |
55176 KB |
Output is correct |
18 |
Correct |
189 ms |
55136 KB |
Output is correct |
19 |
Correct |
183 ms |
55688 KB |
Output is correct |
20 |
Correct |
100 ms |
47172 KB |
Output is correct |
21 |
Correct |
66 ms |
46028 KB |
Output is correct |
22 |
Correct |
64 ms |
46736 KB |
Output is correct |
23 |
Correct |
99 ms |
47132 KB |
Output is correct |
24 |
Correct |
99 ms |
47244 KB |
Output is correct |
25 |
Correct |
102 ms |
46604 KB |
Output is correct |
26 |
Correct |
96 ms |
46480 KB |
Output is correct |
27 |
Correct |
97 ms |
46492 KB |
Output is correct |
28 |
Correct |
99 ms |
46816 KB |
Output is correct |
29 |
Correct |
589 ms |
85660 KB |
Output is correct |
30 |
Correct |
476 ms |
82564 KB |
Output is correct |
31 |
Correct |
453 ms |
84604 KB |
Output is correct |
32 |
Correct |
581 ms |
85720 KB |
Output is correct |
33 |
Correct |
591 ms |
85704 KB |
Output is correct |
34 |
Correct |
572 ms |
83452 KB |
Output is correct |
35 |
Correct |
572 ms |
83132 KB |
Output is correct |
36 |
Correct |
573 ms |
83072 KB |
Output is correct |
37 |
Correct |
577 ms |
84576 KB |
Output is correct |
38 |
Correct |
390 ms |
91332 KB |
Output is correct |
39 |
Correct |
404 ms |
91416 KB |
Output is correct |
40 |
Correct |
382 ms |
88004 KB |
Output is correct |
41 |
Correct |
380 ms |
87596 KB |
Output is correct |
42 |
Correct |
383 ms |
87504 KB |
Output is correct |
43 |
Correct |
383 ms |
89284 KB |
Output is correct |
44 |
Correct |
422 ms |
90740 KB |
Output is correct |
45 |
Correct |
410 ms |
90708 KB |
Output is correct |
46 |
Correct |
404 ms |
87620 KB |
Output is correct |
47 |
Correct |
403 ms |
87256 KB |
Output is correct |
48 |
Correct |
397 ms |
87192 KB |
Output is correct |
49 |
Correct |
403 ms |
89284 KB |
Output is correct |
50 |
Correct |
465 ms |
88804 KB |
Output is correct |
51 |
Correct |
464 ms |
88688 KB |
Output is correct |
52 |
Correct |
450 ms |
86208 KB |
Output is correct |
53 |
Correct |
457 ms |
86176 KB |
Output is correct |
54 |
Correct |
445 ms |
86032 KB |
Output is correct |
55 |
Correct |
456 ms |
87548 KB |
Output is correct |