#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int N = 5e5 + 1;
int n;
struct SegTree{
struct Info{
int opt, mx[2];
Info(){
opt = mx[0] = mx[1] = 0;
};
};
Info T[N * 4];
Info merge(Info L, Info R){
Info rt;
for ( auto j: {0, 1} ){
rt.mx[j] = max(L.mx[j], R.mx[j]);
}
rt.opt = max({L.opt, R.opt, L.mx[0] + R.mx[1]});
return rt;
}
void upd(int v, int l, int r, int p, int x, int j){
if ( l == r ){
chmax(T[v].mx[j], x);
chmax(T[v].opt, T[v].mx[0] + T[v].mx[1]);
return;
}
int md = (l + r) >> 1;
if ( p <= md ){
upd(v * 2, l, md, p, x, j);
} else{
upd(v * 2 + 1, md + 1, r, p, x, j);
}
T[v] = merge(T[v * 2], T[v * 2 + 1]);
}
void upd(int p, int x, int j = 0){
upd(1, 0, n - 1, p, x, j);
}
Info get(int v, int l, int r, int tl, int tr){
Info ret;
if ( l > tr or r < tl ){
return ret;
}
if ( tl <= l && tr >= r ){
return T[v];
}
int md = (l + r) >> 1;
return merge(get(v * 2, l, md, tl, tr), get(v * 2 + 1, md + 1, r, tl, tr));
}
int query(int l, int r){
return get(1, 0, n - 1, l, r).opt;
}
} seg;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
vector <int> a(n);
for ( auto &u: a ){
cin >> u;
}
vector <vector<int>> upd(n);
stack <int> stk;
for ( int i = 0; i < n; i++ ){
while ( !stk.empty() && a[stk.top()] < a[i] ){
stk.pop();
}
if ( !stk.empty() ){
upd[stk.top()].pb(i);
}
stk.push(i);
}
while ( stk.size() ) stk.pop();
for ( int i = n - 1; i >= 0; i-- ){
while ( !stk.empty() && a[stk.top()] < a[i] ){
stk.pop();
}
if ( !stk.empty() ){
upd[i].pb(stk.top());
}
stk.push(i);
}
int q; cin >> q;
vector <vector<ar<int,2>>> qu(n);
for ( int i = 0; i < q; i++ ){
int l, r; cin >> l >> r;
--l, --r;
qu[l].pb({i, r});
}
for ( int i = 0; i < n; i++ ){
seg.upd(i, a[i], 1);
}
vector <int> ans(q);
for ( int i = n - 1; i >= 0; i-- ){
for ( auto &j: upd[i] ){
if ( j * 2 - i < n ){
seg.upd(j * 2 - i, a[i] + a[j]);
}
}
for ( auto &[j, r]: qu[i] ){
ans[j] = seg.query(i, r);
}
}
for ( auto &u: ans ){
cout << u << ln;
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
47192 KB |
Output is correct |
2 |
Correct |
17 ms |
47240 KB |
Output is correct |
3 |
Correct |
16 ms |
47196 KB |
Output is correct |
4 |
Correct |
22 ms |
47220 KB |
Output is correct |
5 |
Correct |
17 ms |
47192 KB |
Output is correct |
6 |
Correct |
18 ms |
47196 KB |
Output is correct |
7 |
Correct |
18 ms |
47192 KB |
Output is correct |
8 |
Correct |
17 ms |
47196 KB |
Output is correct |
9 |
Correct |
17 ms |
47196 KB |
Output is correct |
10 |
Correct |
16 ms |
47264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
47192 KB |
Output is correct |
2 |
Correct |
17 ms |
47240 KB |
Output is correct |
3 |
Correct |
16 ms |
47196 KB |
Output is correct |
4 |
Correct |
22 ms |
47220 KB |
Output is correct |
5 |
Correct |
17 ms |
47192 KB |
Output is correct |
6 |
Correct |
18 ms |
47196 KB |
Output is correct |
7 |
Correct |
18 ms |
47192 KB |
Output is correct |
8 |
Correct |
17 ms |
47196 KB |
Output is correct |
9 |
Correct |
17 ms |
47196 KB |
Output is correct |
10 |
Correct |
16 ms |
47264 KB |
Output is correct |
11 |
Correct |
192 ms |
74792 KB |
Output is correct |
12 |
Correct |
180 ms |
74832 KB |
Output is correct |
13 |
Correct |
177 ms |
74804 KB |
Output is correct |
14 |
Correct |
158 ms |
74836 KB |
Output is correct |
15 |
Correct |
176 ms |
74836 KB |
Output is correct |
16 |
Correct |
165 ms |
74196 KB |
Output is correct |
17 |
Correct |
177 ms |
74324 KB |
Output is correct |
18 |
Correct |
176 ms |
74068 KB |
Output is correct |
19 |
Correct |
165 ms |
74808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
67668 KB |
Output is correct |
2 |
Correct |
73 ms |
67872 KB |
Output is correct |
3 |
Correct |
86 ms |
67916 KB |
Output is correct |
4 |
Correct |
99 ms |
67520 KB |
Output is correct |
5 |
Correct |
108 ms |
67540 KB |
Output is correct |
6 |
Correct |
106 ms |
66896 KB |
Output is correct |
7 |
Correct |
100 ms |
66760 KB |
Output is correct |
8 |
Correct |
108 ms |
66632 KB |
Output is correct |
9 |
Correct |
111 ms |
67152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
47192 KB |
Output is correct |
2 |
Correct |
17 ms |
47240 KB |
Output is correct |
3 |
Correct |
16 ms |
47196 KB |
Output is correct |
4 |
Correct |
22 ms |
47220 KB |
Output is correct |
5 |
Correct |
17 ms |
47192 KB |
Output is correct |
6 |
Correct |
18 ms |
47196 KB |
Output is correct |
7 |
Correct |
18 ms |
47192 KB |
Output is correct |
8 |
Correct |
17 ms |
47196 KB |
Output is correct |
9 |
Correct |
17 ms |
47196 KB |
Output is correct |
10 |
Correct |
16 ms |
47264 KB |
Output is correct |
11 |
Correct |
192 ms |
74792 KB |
Output is correct |
12 |
Correct |
180 ms |
74832 KB |
Output is correct |
13 |
Correct |
177 ms |
74804 KB |
Output is correct |
14 |
Correct |
158 ms |
74836 KB |
Output is correct |
15 |
Correct |
176 ms |
74836 KB |
Output is correct |
16 |
Correct |
165 ms |
74196 KB |
Output is correct |
17 |
Correct |
177 ms |
74324 KB |
Output is correct |
18 |
Correct |
176 ms |
74068 KB |
Output is correct |
19 |
Correct |
165 ms |
74808 KB |
Output is correct |
20 |
Correct |
105 ms |
67668 KB |
Output is correct |
21 |
Correct |
73 ms |
67872 KB |
Output is correct |
22 |
Correct |
86 ms |
67916 KB |
Output is correct |
23 |
Correct |
99 ms |
67520 KB |
Output is correct |
24 |
Correct |
108 ms |
67540 KB |
Output is correct |
25 |
Correct |
106 ms |
66896 KB |
Output is correct |
26 |
Correct |
100 ms |
66760 KB |
Output is correct |
27 |
Correct |
108 ms |
66632 KB |
Output is correct |
28 |
Correct |
111 ms |
67152 KB |
Output is correct |
29 |
Correct |
658 ms |
126340 KB |
Output is correct |
30 |
Correct |
535 ms |
127344 KB |
Output is correct |
31 |
Correct |
525 ms |
123396 KB |
Output is correct |
32 |
Correct |
622 ms |
126548 KB |
Output is correct |
33 |
Correct |
631 ms |
126280 KB |
Output is correct |
34 |
Correct |
590 ms |
123988 KB |
Output is correct |
35 |
Correct |
605 ms |
123700 KB |
Output is correct |
36 |
Correct |
578 ms |
123760 KB |
Output is correct |
37 |
Correct |
612 ms |
125256 KB |
Output is correct |
38 |
Correct |
481 ms |
129072 KB |
Output is correct |
39 |
Correct |
457 ms |
129104 KB |
Output is correct |
40 |
Correct |
438 ms |
125700 KB |
Output is correct |
41 |
Correct |
487 ms |
125268 KB |
Output is correct |
42 |
Correct |
460 ms |
125124 KB |
Output is correct |
43 |
Correct |
426 ms |
126972 KB |
Output is correct |
44 |
Correct |
479 ms |
129264 KB |
Output is correct |
45 |
Correct |
466 ms |
129364 KB |
Output is correct |
46 |
Correct |
460 ms |
126144 KB |
Output is correct |
47 |
Correct |
434 ms |
125696 KB |
Output is correct |
48 |
Correct |
489 ms |
125780 KB |
Output is correct |
49 |
Correct |
457 ms |
127640 KB |
Output is correct |
50 |
Correct |
515 ms |
129508 KB |
Output is correct |
51 |
Correct |
542 ms |
129432 KB |
Output is correct |
52 |
Correct |
499 ms |
127204 KB |
Output is correct |
53 |
Correct |
501 ms |
126724 KB |
Output is correct |
54 |
Correct |
552 ms |
126704 KB |
Output is correct |
55 |
Correct |
511 ms |
128492 KB |
Output is correct |