#include<bits/stdc++.h>
using namespace std;
const int N = (int)5e5+7;
const int M = (int)2e5+7;
const int inf = (int)1e9+7;
int n, q, a[N], b[N], ans[N];
pair<int, int> suf[N];
int L[N], R[N];
vector<pair<int, int> > Q[N];
vector<int> adj[N];
int add[4*N], t[4*N], mx[4*N];
void build(int v, int tl, int tr) {
if(tl == tr) {
t[v] = a[tl];
return;
}
int mid = (tl+tr)/2;
build(v*2, tl, mid);
build(v*2+1, mid+1, tr);
t[v] = max(t[v*2], t[v*2+1]);
mx[v] = t[v];
}
void push(int v, int tl, int tr) {
if(tl != tr) {
add[v*2] = max(add[v*2], add[v]);
add[v*2+1] = max(add[v*2+1], add[v]);
}
mx[v] = max(mx[v], t[v]+add[v]);
}
void upd(int v, int tl, int tr, int l, int r, int x) {
push(v, tl, tr);
if(tl>r || l>tr) {
return;
}
if(tl >= l && tr<= r) {
add[v] = max(add[v], x);
push(v, tl, tr);
return;
}
int mid = (tl+tr)>>1;
upd(v*2, tl, mid, l, r, x);
upd(v*2+1, mid+1, tr, l, r, x);
mx[v] = max(mx[v*2], mx[v*2+1]);
}
void upd(int l, int r, int x) {
if(l>r) {
return;
}
upd(1, 1, n, l, r, x);
}
int get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if(tl >= l && tr <= r) {
return mx[v];
}
if(tl>r || l > tr) {
return 0;
}
int mid = (tl+tr)/2;
return max(get(v*2, tl, mid, l, r), get(v*2+1, mid+1, tr, l, r));
}
int get(int l, int r) {
if(l>r) {
return 0;
}
return get(1, 1, n, l, r);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
build(1, 1, n);
cin >> q;
stack<int> st;
for(int i = 1; i <= n; ++i) {
L[i] = i;
while(!st.empty() && a[st.top()]<a[i]) {
L[i]=L[st.top()];
st.pop();
}
st.push(i);
}
while(!st.empty()) {
continue;
}
for(int i = n; i >= 1; --i) {
R[i] = i;
while(!st.empty() && a[st.top()]<a[i]) {
R[i] = R[st.top()];
st.pop();
}
st.push(i);
}
for(int i = 1; i <= n; ++i) {
if(R[i]+1<=n) {
adj[i].push_back(R[i]+1);
}
if(L[i]-1>=1) {
adj[L[i]-1].push_back(i);
}
}
for(int i = 1; i <= q; ++i) {
int l, r;
cin >> l >> r;
Q[l].push_back({r, i});
}
for(int l = n; l >= 1; --l) {
{
for(int j : adj[l]) {
int i = l;
upd(2*j-i, n, a[i]+a[j]);
}
}
for(auto [r, i] : Q[l]) {
ans[i] = max(ans[i], get(l, r));
}
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
8 ms |
31164 KB |
Output is correct |
3 |
Correct |
13 ms |
31224 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
8 ms |
31068 KB |
Output is correct |
6 |
Correct |
7 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31256 KB |
Output is correct |
9 |
Correct |
8 ms |
31068 KB |
Output is correct |
10 |
Correct |
8 ms |
31192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
8 ms |
31164 KB |
Output is correct |
3 |
Correct |
13 ms |
31224 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
8 ms |
31068 KB |
Output is correct |
6 |
Correct |
7 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31256 KB |
Output is correct |
9 |
Correct |
8 ms |
31068 KB |
Output is correct |
10 |
Correct |
8 ms |
31192 KB |
Output is correct |
11 |
Correct |
262 ms |
45340 KB |
Output is correct |
12 |
Correct |
252 ms |
45292 KB |
Output is correct |
13 |
Correct |
267 ms |
45324 KB |
Output is correct |
14 |
Correct |
256 ms |
45316 KB |
Output is correct |
15 |
Correct |
278 ms |
45300 KB |
Output is correct |
16 |
Correct |
256 ms |
44764 KB |
Output is correct |
17 |
Correct |
250 ms |
44704 KB |
Output is correct |
18 |
Correct |
250 ms |
44628 KB |
Output is correct |
19 |
Correct |
249 ms |
45252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
45956 KB |
Output is correct |
2 |
Correct |
80 ms |
46676 KB |
Output is correct |
3 |
Correct |
217 ms |
46048 KB |
Output is correct |
4 |
Correct |
140 ms |
45912 KB |
Output is correct |
5 |
Correct |
170 ms |
46028 KB |
Output is correct |
6 |
Correct |
136 ms |
45864 KB |
Output is correct |
7 |
Correct |
143 ms |
45904 KB |
Output is correct |
8 |
Correct |
137 ms |
46008 KB |
Output is correct |
9 |
Correct |
160 ms |
46100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
8 ms |
31164 KB |
Output is correct |
3 |
Correct |
13 ms |
31224 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
8 ms |
31068 KB |
Output is correct |
6 |
Correct |
7 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
31068 KB |
Output is correct |
8 |
Correct |
6 ms |
31256 KB |
Output is correct |
9 |
Correct |
8 ms |
31068 KB |
Output is correct |
10 |
Correct |
8 ms |
31192 KB |
Output is correct |
11 |
Correct |
262 ms |
45340 KB |
Output is correct |
12 |
Correct |
252 ms |
45292 KB |
Output is correct |
13 |
Correct |
267 ms |
45324 KB |
Output is correct |
14 |
Correct |
256 ms |
45316 KB |
Output is correct |
15 |
Correct |
278 ms |
45300 KB |
Output is correct |
16 |
Correct |
256 ms |
44764 KB |
Output is correct |
17 |
Correct |
250 ms |
44704 KB |
Output is correct |
18 |
Correct |
250 ms |
44628 KB |
Output is correct |
19 |
Correct |
249 ms |
45252 KB |
Output is correct |
20 |
Correct |
194 ms |
45956 KB |
Output is correct |
21 |
Correct |
80 ms |
46676 KB |
Output is correct |
22 |
Correct |
217 ms |
46048 KB |
Output is correct |
23 |
Correct |
140 ms |
45912 KB |
Output is correct |
24 |
Correct |
170 ms |
46028 KB |
Output is correct |
25 |
Correct |
136 ms |
45864 KB |
Output is correct |
26 |
Correct |
143 ms |
45904 KB |
Output is correct |
27 |
Correct |
137 ms |
46008 KB |
Output is correct |
28 |
Correct |
160 ms |
46100 KB |
Output is correct |
29 |
Correct |
890 ms |
79440 KB |
Output is correct |
30 |
Correct |
790 ms |
80980 KB |
Output is correct |
31 |
Correct |
888 ms |
79064 KB |
Output is correct |
32 |
Correct |
900 ms |
79456 KB |
Output is correct |
33 |
Correct |
962 ms |
79764 KB |
Output is correct |
34 |
Correct |
860 ms |
79024 KB |
Output is correct |
35 |
Correct |
822 ms |
78980 KB |
Output is correct |
36 |
Correct |
811 ms |
78848 KB |
Output is correct |
37 |
Correct |
820 ms |
79440 KB |
Output is correct |
38 |
Correct |
637 ms |
85328 KB |
Output is correct |
39 |
Correct |
657 ms |
85140 KB |
Output is correct |
40 |
Correct |
634 ms |
83600 KB |
Output is correct |
41 |
Correct |
652 ms |
83300 KB |
Output is correct |
42 |
Correct |
653 ms |
83452 KB |
Output is correct |
43 |
Correct |
637 ms |
84360 KB |
Output is correct |
44 |
Correct |
672 ms |
84616 KB |
Output is correct |
45 |
Correct |
675 ms |
84652 KB |
Output is correct |
46 |
Correct |
690 ms |
83128 KB |
Output is correct |
47 |
Correct |
657 ms |
83028 KB |
Output is correct |
48 |
Correct |
731 ms |
83024 KB |
Output is correct |
49 |
Correct |
692 ms |
84304 KB |
Output is correct |
50 |
Correct |
752 ms |
82768 KB |
Output is correct |
51 |
Correct |
823 ms |
82708 KB |
Output is correct |
52 |
Correct |
718 ms |
81856 KB |
Output is correct |
53 |
Correct |
760 ms |
81748 KB |
Output is correct |
54 |
Correct |
719 ms |
81668 KB |
Output is correct |
55 |
Correct |
725 ms |
82500 KB |
Output is correct |