#include <bits/stdc++.h>
#define a first
#define b second
#define ab first
#define value second
#define r first
#define id second
#define int long long
using namespace std;
typedef pair<int,int> ii;
const int inf = 102345678901;
const int N = 1<<19;
int arr[N];
int ans[N];
vector<ii> updates[N];
vector<ii> queries[N];
struct node{ int c, ab, abc; };
node tree[2*N];
node relax(node L, node R){
return { max(L.c, R.c), max(L.ab, R.ab), max(L.ab+R.c, max(L.abc, R.abc)) };
}
void init(){
for(int i = N;i < 2*N;i++) tree[i] = {arr[i-N], -inf, -inf};
for(int i = N-1;i >= 1;i--) tree[i] = relax(tree[i<<1], tree[i<<1|1]);
}
void update(int i, int ab){
i += N;
tree[i].ab = max(tree[i].ab, ab);
tree[i].abc = max(tree[i].ab + tree[i].c, tree[i].abc);
for(i >>= 1;i > 0;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++]);
if(r&1) R = relax(tree[--r], R);
}
return relax(L,R).abc;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 0;i < n;i++) cin >> arr[i];
vector<ii> goodpairs;
stack<int> S;
for(int i = 0;i < n;i++){
while(!S.empty()){
int top = S.top();
goodpairs.push_back(ii(top,i));
if(arr[i] >= arr[top]) S.pop();
else break;
}
S.push(i);
}
for(ii p : goodpairs){
int ab = p.b * 2 - p.a;
if(ab < n) updates[p.a].push_back(ii(ab, arr[p.a] + arr[p.b]));
}
int Q; cin >> Q;
for(int q = 0;q < Q;q++){
int l, r; cin >> l >> r; l--; r--;
queries[l].push_back(ii(r,q));
}
init();
for(int l = n-1;l >= 0;l--){
for(ii u : updates[l]){
update(u.ab, u.value);
}
for(ii q : queries[l]){
int r = q.r;
ans[q.id] = query(l, r+1);
}
}
for(int q = 0;q < Q;q++) cout << ans[q] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49664 KB |
Output is correct |
2 |
Correct |
34 ms |
49656 KB |
Output is correct |
3 |
Correct |
34 ms |
49656 KB |
Output is correct |
4 |
Correct |
34 ms |
49656 KB |
Output is correct |
5 |
Correct |
35 ms |
49656 KB |
Output is correct |
6 |
Correct |
37 ms |
49656 KB |
Output is correct |
7 |
Correct |
35 ms |
49664 KB |
Output is correct |
8 |
Correct |
35 ms |
49656 KB |
Output is correct |
9 |
Correct |
35 ms |
49656 KB |
Output is correct |
10 |
Correct |
34 ms |
49664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49664 KB |
Output is correct |
2 |
Correct |
34 ms |
49656 KB |
Output is correct |
3 |
Correct |
34 ms |
49656 KB |
Output is correct |
4 |
Correct |
34 ms |
49656 KB |
Output is correct |
5 |
Correct |
35 ms |
49656 KB |
Output is correct |
6 |
Correct |
37 ms |
49656 KB |
Output is correct |
7 |
Correct |
35 ms |
49664 KB |
Output is correct |
8 |
Correct |
35 ms |
49656 KB |
Output is correct |
9 |
Correct |
35 ms |
49656 KB |
Output is correct |
10 |
Correct |
34 ms |
49664 KB |
Output is correct |
11 |
Correct |
252 ms |
74424 KB |
Output is correct |
12 |
Correct |
246 ms |
74232 KB |
Output is correct |
13 |
Correct |
234 ms |
74232 KB |
Output is correct |
14 |
Correct |
248 ms |
74360 KB |
Output is correct |
15 |
Correct |
281 ms |
74544 KB |
Output is correct |
16 |
Correct |
254 ms |
73848 KB |
Output is correct |
17 |
Correct |
272 ms |
73808 KB |
Output is correct |
18 |
Correct |
253 ms |
73720 KB |
Output is correct |
19 |
Correct |
245 ms |
74336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
69472 KB |
Output is correct |
2 |
Correct |
127 ms |
62304 KB |
Output is correct |
3 |
Correct |
128 ms |
63916 KB |
Output is correct |
4 |
Correct |
209 ms |
69460 KB |
Output is correct |
5 |
Correct |
214 ms |
69460 KB |
Output is correct |
6 |
Correct |
195 ms |
68800 KB |
Output is correct |
7 |
Correct |
203 ms |
68692 KB |
Output is correct |
8 |
Correct |
195 ms |
68692 KB |
Output is correct |
9 |
Correct |
197 ms |
69076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49664 KB |
Output is correct |
2 |
Correct |
34 ms |
49656 KB |
Output is correct |
3 |
Correct |
34 ms |
49656 KB |
Output is correct |
4 |
Correct |
34 ms |
49656 KB |
Output is correct |
5 |
Correct |
35 ms |
49656 KB |
Output is correct |
6 |
Correct |
37 ms |
49656 KB |
Output is correct |
7 |
Correct |
35 ms |
49664 KB |
Output is correct |
8 |
Correct |
35 ms |
49656 KB |
Output is correct |
9 |
Correct |
35 ms |
49656 KB |
Output is correct |
10 |
Correct |
34 ms |
49664 KB |
Output is correct |
11 |
Correct |
252 ms |
74424 KB |
Output is correct |
12 |
Correct |
246 ms |
74232 KB |
Output is correct |
13 |
Correct |
234 ms |
74232 KB |
Output is correct |
14 |
Correct |
248 ms |
74360 KB |
Output is correct |
15 |
Correct |
281 ms |
74544 KB |
Output is correct |
16 |
Correct |
254 ms |
73848 KB |
Output is correct |
17 |
Correct |
272 ms |
73808 KB |
Output is correct |
18 |
Correct |
253 ms |
73720 KB |
Output is correct |
19 |
Correct |
245 ms |
74336 KB |
Output is correct |
20 |
Correct |
203 ms |
69472 KB |
Output is correct |
21 |
Correct |
127 ms |
62304 KB |
Output is correct |
22 |
Correct |
128 ms |
63916 KB |
Output is correct |
23 |
Correct |
209 ms |
69460 KB |
Output is correct |
24 |
Correct |
214 ms |
69460 KB |
Output is correct |
25 |
Correct |
195 ms |
68800 KB |
Output is correct |
26 |
Correct |
203 ms |
68692 KB |
Output is correct |
27 |
Correct |
195 ms |
68692 KB |
Output is correct |
28 |
Correct |
197 ms |
69076 KB |
Output is correct |
29 |
Correct |
953 ms |
128168 KB |
Output is correct |
30 |
Correct |
770 ms |
109960 KB |
Output is correct |
31 |
Correct |
762 ms |
114204 KB |
Output is correct |
32 |
Correct |
1055 ms |
127900 KB |
Output is correct |
33 |
Correct |
985 ms |
127876 KB |
Output is correct |
34 |
Correct |
1046 ms |
125520 KB |
Output is correct |
35 |
Correct |
973 ms |
125284 KB |
Output is correct |
36 |
Correct |
938 ms |
125456 KB |
Output is correct |
37 |
Correct |
952 ms |
126784 KB |
Output is correct |
38 |
Correct |
684 ms |
130496 KB |
Output is correct |
39 |
Correct |
705 ms |
130624 KB |
Output is correct |
40 |
Correct |
681 ms |
127296 KB |
Output is correct |
41 |
Correct |
655 ms |
126912 KB |
Output is correct |
42 |
Correct |
663 ms |
126736 KB |
Output is correct |
43 |
Correct |
668 ms |
128448 KB |
Output is correct |
44 |
Correct |
707 ms |
131008 KB |
Output is correct |
45 |
Correct |
697 ms |
130760 KB |
Output is correct |
46 |
Correct |
679 ms |
127680 KB |
Output is correct |
47 |
Correct |
716 ms |
127296 KB |
Output is correct |
48 |
Correct |
702 ms |
127372 KB |
Output is correct |
49 |
Correct |
697 ms |
129196 KB |
Output is correct |
50 |
Correct |
784 ms |
131120 KB |
Output is correct |
51 |
Correct |
799 ms |
131136 KB |
Output is correct |
52 |
Correct |
746 ms |
128704 KB |
Output is correct |
53 |
Correct |
796 ms |
128368 KB |
Output is correct |
54 |
Correct |
745 ms |
128192 KB |
Output is correct |
55 |
Correct |
766 ms |
129860 KB |
Output is correct |