#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct seg{
vector<ll> a, b, laz;
int n;
seg(int _n = 0){
n = _n;
a.resize(n * 4 + 3, 0);
b.resize(n * 4 + 3, 0);
laz.resize(n * 4 + 3, 0);
}
void build(int val[], int i, int l, int r){
if(l == r){
a[i] = val[l];
b[i] = val[l];
return;
}
int m = (l + r) >> 1;
build(val, i * 2, l, m);
build(val, i * 2 + 1, m + 1, r);
a[i] = max(a[i * 2], a[i * 2 + 1]);
b[i] = max(b[i * 2], b[i * 2 + 1]);
}
void push(int i){
maximize(laz[i * 2], laz[i]);
maximize(laz[i * 2 + 1], laz[i]);
maximize(b[i * 2], a[i * 2] + laz[i]);
maximize(b[i * 2 + 1], a[i * 2 + 1] + laz[i]);
}
void upd(int i, int l, int r, int u, int v, ll c){
if(r < u || v < l) return;
if(u <= l && r <= v){
maximize(laz[i], c);
maximize(b[i], a[i] + c);
return;
}
push(i);
int m = (l + r) >> 1;
upd(i * 2, l, m, u, v, c);
upd(i * 2 + 1, m + 1, r, u, v, c);
b[i] = max(b[i * 2], b[i * 2 + 1]);
}
ll get(int i, int l, int r, int u, int v){
if(r < u || v < l) return 0;
if(u <= l && r <= v) return b[i];
push(i);
int m = (l + r) >> 1;
return max(get(i * 2, l, m, u, v), get(i * 2 + 1, m + 1, r, u, v));
}
};
const int MAX = 5e5 + 10;
int N, Q;
int a[MAX];
ll ans[MAX];
vector<int> change[MAX];
vector<pair<int, int>> ask[MAX];
void solve(){
cin >> N;
for(int i=1; i<=N; i++){
cin >> a[i];
}
seg it(N);
it.build(a, 1, 1, N);
stack<int> st;
for(int i=N; i>=1; i--){
while(sz(st)){
change[i].push_back(st.top());
if(a[st.top()] > a[i]) break;
st.pop();
}
st.push(i);
}
cin >> Q;
for(int i=1; i<=Q; i++){
int l, r;
cin >> l >> r;
ask[l].push_back({r, i});
}
for(int i=N; i>=1; i--){
for(int x: change[i]){
if(x * 2 - i <= N){
it.upd(1, 1, N, x * 2 - i, N, a[i] + a[x]);
}
}
for(auto o: ask[i]){
ans[o.se] = it.get(1, 1, N, i, o.fi);
}
}
for(int i=1; i<=Q; i++){
cout << ans[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("task.inp", "r", stdin);
// freopen("task.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23900 KB |
Output is correct |
3 |
Correct |
13 ms |
24152 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
15 ms |
23948 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23900 KB |
Output is correct |
3 |
Correct |
13 ms |
24152 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
15 ms |
23948 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23732 KB |
Output is correct |
11 |
Correct |
192 ms |
45220 KB |
Output is correct |
12 |
Correct |
214 ms |
45004 KB |
Output is correct |
13 |
Correct |
196 ms |
44932 KB |
Output is correct |
14 |
Correct |
186 ms |
45136 KB |
Output is correct |
15 |
Correct |
198 ms |
45060 KB |
Output is correct |
16 |
Correct |
183 ms |
44568 KB |
Output is correct |
17 |
Correct |
190 ms |
44348 KB |
Output is correct |
18 |
Correct |
181 ms |
44372 KB |
Output is correct |
19 |
Correct |
202 ms |
44968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
51580 KB |
Output is correct |
2 |
Correct |
87 ms |
52304 KB |
Output is correct |
3 |
Correct |
77 ms |
51520 KB |
Output is correct |
4 |
Correct |
124 ms |
51692 KB |
Output is correct |
5 |
Correct |
121 ms |
51748 KB |
Output is correct |
6 |
Correct |
123 ms |
51028 KB |
Output is correct |
7 |
Correct |
147 ms |
50984 KB |
Output is correct |
8 |
Correct |
120 ms |
50912 KB |
Output is correct |
9 |
Correct |
139 ms |
51280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23900 KB |
Output is correct |
2 |
Correct |
16 ms |
23900 KB |
Output is correct |
3 |
Correct |
13 ms |
24152 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23904 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
15 ms |
23948 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23732 KB |
Output is correct |
11 |
Correct |
192 ms |
45220 KB |
Output is correct |
12 |
Correct |
214 ms |
45004 KB |
Output is correct |
13 |
Correct |
196 ms |
44932 KB |
Output is correct |
14 |
Correct |
186 ms |
45136 KB |
Output is correct |
15 |
Correct |
198 ms |
45060 KB |
Output is correct |
16 |
Correct |
183 ms |
44568 KB |
Output is correct |
17 |
Correct |
190 ms |
44348 KB |
Output is correct |
18 |
Correct |
181 ms |
44372 KB |
Output is correct |
19 |
Correct |
202 ms |
44968 KB |
Output is correct |
20 |
Correct |
134 ms |
51580 KB |
Output is correct |
21 |
Correct |
87 ms |
52304 KB |
Output is correct |
22 |
Correct |
77 ms |
51520 KB |
Output is correct |
23 |
Correct |
124 ms |
51692 KB |
Output is correct |
24 |
Correct |
121 ms |
51748 KB |
Output is correct |
25 |
Correct |
123 ms |
51028 KB |
Output is correct |
26 |
Correct |
147 ms |
50984 KB |
Output is correct |
27 |
Correct |
120 ms |
50912 KB |
Output is correct |
28 |
Correct |
139 ms |
51280 KB |
Output is correct |
29 |
Correct |
852 ms |
118868 KB |
Output is correct |
30 |
Correct |
607 ms |
120304 KB |
Output is correct |
31 |
Correct |
632 ms |
118260 KB |
Output is correct |
32 |
Correct |
720 ms |
118856 KB |
Output is correct |
33 |
Correct |
816 ms |
118848 KB |
Output is correct |
34 |
Correct |
735 ms |
116564 KB |
Output is correct |
35 |
Correct |
692 ms |
116308 KB |
Output is correct |
36 |
Correct |
688 ms |
116084 KB |
Output is correct |
37 |
Correct |
725 ms |
117704 KB |
Output is correct |
38 |
Correct |
608 ms |
124788 KB |
Output is correct |
39 |
Correct |
482 ms |
124500 KB |
Output is correct |
40 |
Correct |
572 ms |
121172 KB |
Output is correct |
41 |
Correct |
511 ms |
120596 KB |
Output is correct |
42 |
Correct |
521 ms |
120760 KB |
Output is correct |
43 |
Correct |
531 ms |
122372 KB |
Output is correct |
44 |
Correct |
558 ms |
123732 KB |
Output is correct |
45 |
Correct |
564 ms |
123988 KB |
Output is correct |
46 |
Correct |
559 ms |
120724 KB |
Output is correct |
47 |
Correct |
546 ms |
120364 KB |
Output is correct |
48 |
Correct |
523 ms |
120404 KB |
Output is correct |
49 |
Correct |
533 ms |
122448 KB |
Output is correct |
50 |
Correct |
650 ms |
122016 KB |
Output is correct |
51 |
Correct |
600 ms |
121936 KB |
Output is correct |
52 |
Correct |
667 ms |
119380 KB |
Output is correct |
53 |
Correct |
644 ms |
119244 KB |
Output is correct |
54 |
Correct |
622 ms |
119260 KB |
Output is correct |
55 |
Correct |
689 ms |
120792 KB |
Output is correct |