#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 5e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll n,q;
ll a[N], nxt[N], res[N];
vector<ll>candi[N];
vector<pll>t[N];
struct segment_tree{
ll n;
vector<ll>st, lz, mx, val;
void init(ll _n){
n = _n;
st.clear(); st.resize(4 * n + 10, 0);
lz.clear(); lz.resize(4 * n + 10, 0);
mx.clear(); mx.resize(4 * n + 10);
val.clear(); val.resize(4 * n + 10);
build(1,1,n);
}
void build(ll id, ll l, ll r){
st[id] = -inf;
val[id] = -inf;
if(l == r){
mx[id] = a[l];
return;
}
ll mid = (l + r) / 2;
build(2 * id, l, mid); build(2 * id + 1, mid + 1, r);
mx[id] = max(mx[2 * id], mx[2 * id + 1]);
}
void down(ll id, ll l, ll r){
st[id] = max(lz[id], st[id]);
val[id] = max(val[id], mx[id] + st[id]);
if(l != r) lz[2 * id] = max(lz[2 * id], lz[id]), lz[2 * id + 1] = max(lz[2 * id + 1], lz[id]);
lz[id] = 0;
}
void update(ll id, ll l, ll r, ll left, ll right, ll v){
down(id, l, r);
if(l > right || r < left) return;
if(left <= l && r <= right){
lz[id] = max(lz[id], v);
down(id, l, r);
return;
}
ll mid = (l + r) / 2;
update(2 * id, l, mid, left, right, v);
update(2 * id + 1, mid + 1, r, left, right, v);
val[id] = max(val[2 * id], val[2 * id + 1]);
}
ll get(ll id, ll l, ll r, ll left, ll right){
down(id, l, r);
if(l > right || r < left) return -inf;
if(left <= l && r <= right) return val[id];
ll mid = (l + r) / 2;
return max(get(2 * id, l, mid, left, right), get(2 * id + 1, mid + 1, r, left, right));
}
void update(ll l, ll r, ll val){update(1,1,n,l,r,val);}
ll get(ll l, ll r){return get(1,1,n,l,r);}
} st;
void to_thic_cau(){
cin >> n;
for(int i = 1; i <= n;i++) cin >> a[i];
a[n+1] = a[0] = inf;
stack<ll>s;
s.push(n + 1);
for(int i = n; i >= 1;i--){
while(a[s.top()] < a[i]) s.pop();
if(s.top() <= n) candi[i].pb(s.top());
s.push(i);
}
while(s.size()) s.pop();
s.push(0);
for(int i = 1; i <= n;i++){
while(a[s.top()] < a[i]) s.pop();
if(s.top() >= 1) candi[s.top()].pb(i);
s.push(i);
}
cin >> q;
for(int i = 1; i <= q;i++){
ll l,r; cin >> l >> r;
t[l].pb({r, i});
}
st.init(n);
for(int i = n; i >= 1;i--){
for(auto x : candi[i]){
ll len = x - i;
if(x + len <= n) st.update(x + len, n, a[i] + a[x]);
}
for(auto x : t[i]) res[x.S] = st.get(i, x.F);
}
for(int i = 1; i <= q;i++) cout << res[i] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23988 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23816 KB |
Output is correct |
8 |
Correct |
18 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23988 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23816 KB |
Output is correct |
8 |
Correct |
18 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23864 KB |
Output is correct |
11 |
Correct |
260 ms |
51836 KB |
Output is correct |
12 |
Correct |
287 ms |
51728 KB |
Output is correct |
13 |
Correct |
297 ms |
51740 KB |
Output is correct |
14 |
Correct |
306 ms |
51792 KB |
Output is correct |
15 |
Correct |
257 ms |
51796 KB |
Output is correct |
16 |
Correct |
242 ms |
51028 KB |
Output is correct |
17 |
Correct |
248 ms |
51260 KB |
Output is correct |
18 |
Correct |
247 ms |
51028 KB |
Output is correct |
19 |
Correct |
274 ms |
51728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
60940 KB |
Output is correct |
2 |
Correct |
111 ms |
59984 KB |
Output is correct |
3 |
Correct |
121 ms |
60124 KB |
Output is correct |
4 |
Correct |
184 ms |
61152 KB |
Output is correct |
5 |
Correct |
173 ms |
61348 KB |
Output is correct |
6 |
Correct |
172 ms |
60460 KB |
Output is correct |
7 |
Correct |
190 ms |
60240 KB |
Output is correct |
8 |
Correct |
186 ms |
60240 KB |
Output is correct |
9 |
Correct |
172 ms |
60712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23988 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
12 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23816 KB |
Output is correct |
8 |
Correct |
18 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23864 KB |
Output is correct |
11 |
Correct |
260 ms |
51836 KB |
Output is correct |
12 |
Correct |
287 ms |
51728 KB |
Output is correct |
13 |
Correct |
297 ms |
51740 KB |
Output is correct |
14 |
Correct |
306 ms |
51792 KB |
Output is correct |
15 |
Correct |
257 ms |
51796 KB |
Output is correct |
16 |
Correct |
242 ms |
51028 KB |
Output is correct |
17 |
Correct |
248 ms |
51260 KB |
Output is correct |
18 |
Correct |
247 ms |
51028 KB |
Output is correct |
19 |
Correct |
274 ms |
51728 KB |
Output is correct |
20 |
Correct |
187 ms |
60940 KB |
Output is correct |
21 |
Correct |
111 ms |
59984 KB |
Output is correct |
22 |
Correct |
121 ms |
60124 KB |
Output is correct |
23 |
Correct |
184 ms |
61152 KB |
Output is correct |
24 |
Correct |
173 ms |
61348 KB |
Output is correct |
25 |
Correct |
172 ms |
60460 KB |
Output is correct |
26 |
Correct |
190 ms |
60240 KB |
Output is correct |
27 |
Correct |
186 ms |
60240 KB |
Output is correct |
28 |
Correct |
172 ms |
60712 KB |
Output is correct |
29 |
Correct |
1127 ms |
142380 KB |
Output is correct |
30 |
Correct |
783 ms |
139092 KB |
Output is correct |
31 |
Correct |
774 ms |
142948 KB |
Output is correct |
32 |
Correct |
991 ms |
142204 KB |
Output is correct |
33 |
Correct |
1063 ms |
142208 KB |
Output is correct |
34 |
Correct |
1057 ms |
140036 KB |
Output is correct |
35 |
Correct |
1039 ms |
139696 KB |
Output is correct |
36 |
Correct |
1014 ms |
139600 KB |
Output is correct |
37 |
Correct |
968 ms |
140996 KB |
Output is correct |
38 |
Correct |
728 ms |
144944 KB |
Output is correct |
39 |
Correct |
745 ms |
144912 KB |
Output is correct |
40 |
Correct |
761 ms |
141504 KB |
Output is correct |
41 |
Correct |
710 ms |
140988 KB |
Output is correct |
42 |
Correct |
774 ms |
141140 KB |
Output is correct |
43 |
Correct |
774 ms |
142652 KB |
Output is correct |
44 |
Correct |
871 ms |
145236 KB |
Output is correct |
45 |
Correct |
902 ms |
145124 KB |
Output is correct |
46 |
Correct |
818 ms |
141904 KB |
Output is correct |
47 |
Correct |
852 ms |
141644 KB |
Output is correct |
48 |
Correct |
812 ms |
141652 KB |
Output is correct |
49 |
Correct |
959 ms |
143520 KB |
Output is correct |
50 |
Correct |
940 ms |
145624 KB |
Output is correct |
51 |
Correct |
965 ms |
145548 KB |
Output is correct |
52 |
Correct |
914 ms |
143188 KB |
Output is correct |
53 |
Correct |
877 ms |
142928 KB |
Output is correct |
54 |
Correct |
861 ms |
142932 KB |
Output is correct |
55 |
Correct |
847 ms |
144524 KB |
Output is correct |