#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double ld;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 5e5 + 10;
int n, q, a[maxn];
pll seg[maxn << 2];
ll lazy[maxn << 2], ans[maxn];
vector<pii> Q[maxn], A[maxn];
pii operator +(pii a, pii b){
return MP(max(a.F, b.F), max(a.S, b.S));
}
void build(int v, int l, int r){
if (l + 1 == r){
seg[v] = {a[l], a[l]};
return;
}
int mid = (l + r) >> 1;
build(v << 1, l, mid);
build(v << 1 | 1, mid, r);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
void shift(int v, int l, int r);
void change(int v, int l, int r, int ql, int qr, ll val){
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr){
seg[v].F = max(seg[v].F, seg[v].S + val);
lazy[v] = max(lazy[v], val);
return;
}
shift(v, l, r);
int mid = (l + r) >> 1;
change(v << 1, l, mid, ql, qr, val);
change(v << 1 | 1, mid, r, ql, qr, val);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
ll get(int v, int l, int r, int ql, int qr){
if (qr <= l || r <= ql) return 0;
if (ql <= l && r <= qr) return seg[v].F;
shift(v, l, r);
int mid = (l + r) >> 1;
return max(get(v << 1, l, mid, ql, qr)
, get(v << 1 | 1, mid, r, ql, qr));
}
void shift(int v, int l, int r){
if (lazy[v] == 0) return;
int mid = (l + r) >> 1;
change(v << 1, l, mid, l, mid, lazy[v]);
change(v << 1 | 1, mid, r, mid, r, lazy[v]);
lazy[v] = 0;
}
int main(){
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
build(1, 1, n+1);
cin >> q;
for (int i = 1; i <= q; i++){
int l, r; cin >> l >> r;
Q[l].push_back({r, i});
}
vector<int> v;
for (int i = 1; i <= n; i++){
while(!v.empty() && a[v.back()] < a[i]) v.pop_back();
if (!v.empty()){
A[v.back()].push_back({i + (i - v.back()), a[v.back()] + a[i]});
}
v.push_back(i);
}
v.clear();
for (int i = n; i; i--){
while(!v.empty() && a[v.back()] < a[i]) v.pop_back();
if (!v.empty()){
A[i].push_back({v.back() + (v.back() - i), a[v.back()] + a[i]});
}
v.push_back(i);
}
for (int i = n; i; i--){
for (auto [x, y]: A[i]){
change(1, 1, n+1, x, n+1, y);
}
for (auto [r, idx]: Q[i]){
ans[idx] = get(1, 1, n+1, i, r+1);
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23768 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23768 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
361 ms |
40020 KB |
Output is correct |
12 |
Correct |
422 ms |
39948 KB |
Output is correct |
13 |
Correct |
386 ms |
40048 KB |
Output is correct |
14 |
Correct |
373 ms |
40064 KB |
Output is correct |
15 |
Correct |
381 ms |
39996 KB |
Output is correct |
16 |
Correct |
428 ms |
39416 KB |
Output is correct |
17 |
Correct |
361 ms |
39400 KB |
Output is correct |
18 |
Correct |
367 ms |
39304 KB |
Output is correct |
19 |
Correct |
382 ms |
40028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
44268 KB |
Output is correct |
2 |
Correct |
119 ms |
43920 KB |
Output is correct |
3 |
Correct |
120 ms |
45636 KB |
Output is correct |
4 |
Correct |
181 ms |
46124 KB |
Output is correct |
5 |
Correct |
178 ms |
46096 KB |
Output is correct |
6 |
Correct |
159 ms |
45416 KB |
Output is correct |
7 |
Correct |
169 ms |
45288 KB |
Output is correct |
8 |
Correct |
153 ms |
45312 KB |
Output is correct |
9 |
Correct |
161 ms |
45616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23760 KB |
Output is correct |
7 |
Correct |
12 ms |
23768 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23784 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
361 ms |
40020 KB |
Output is correct |
12 |
Correct |
422 ms |
39948 KB |
Output is correct |
13 |
Correct |
386 ms |
40048 KB |
Output is correct |
14 |
Correct |
373 ms |
40064 KB |
Output is correct |
15 |
Correct |
381 ms |
39996 KB |
Output is correct |
16 |
Correct |
428 ms |
39416 KB |
Output is correct |
17 |
Correct |
361 ms |
39400 KB |
Output is correct |
18 |
Correct |
367 ms |
39304 KB |
Output is correct |
19 |
Correct |
382 ms |
40028 KB |
Output is correct |
20 |
Correct |
173 ms |
44268 KB |
Output is correct |
21 |
Correct |
119 ms |
43920 KB |
Output is correct |
22 |
Correct |
120 ms |
45636 KB |
Output is correct |
23 |
Correct |
181 ms |
46124 KB |
Output is correct |
24 |
Correct |
178 ms |
46096 KB |
Output is correct |
25 |
Correct |
159 ms |
45416 KB |
Output is correct |
26 |
Correct |
169 ms |
45288 KB |
Output is correct |
27 |
Correct |
153 ms |
45312 KB |
Output is correct |
28 |
Correct |
161 ms |
45616 KB |
Output is correct |
29 |
Correct |
1200 ms |
98852 KB |
Output is correct |
30 |
Correct |
1063 ms |
97848 KB |
Output is correct |
31 |
Correct |
1020 ms |
97764 KB |
Output is correct |
32 |
Correct |
1165 ms |
98912 KB |
Output is correct |
33 |
Correct |
1188 ms |
98912 KB |
Output is correct |
34 |
Correct |
1198 ms |
95752 KB |
Output is correct |
35 |
Correct |
1212 ms |
87248 KB |
Output is correct |
36 |
Correct |
1153 ms |
87244 KB |
Output is correct |
37 |
Correct |
1231 ms |
87824 KB |
Output is correct |
38 |
Correct |
983 ms |
93588 KB |
Output is correct |
39 |
Correct |
931 ms |
93536 KB |
Output is correct |
40 |
Correct |
857 ms |
91832 KB |
Output is correct |
41 |
Correct |
877 ms |
91624 KB |
Output is correct |
42 |
Correct |
833 ms |
91608 KB |
Output is correct |
43 |
Correct |
961 ms |
92496 KB |
Output is correct |
44 |
Correct |
963 ms |
93032 KB |
Output is correct |
45 |
Correct |
911 ms |
92852 KB |
Output is correct |
46 |
Correct |
864 ms |
91384 KB |
Output is correct |
47 |
Correct |
874 ms |
91260 KB |
Output is correct |
48 |
Correct |
945 ms |
91212 KB |
Output is correct |
49 |
Correct |
898 ms |
92436 KB |
Output is correct |
50 |
Correct |
1008 ms |
90724 KB |
Output is correct |
51 |
Correct |
1016 ms |
90832 KB |
Output is correct |
52 |
Correct |
933 ms |
89948 KB |
Output is correct |
53 |
Correct |
941 ms |
89876 KB |
Output is correct |
54 |
Correct |
937 ms |
89916 KB |
Output is correct |
55 |
Correct |
933 ms |
90744 KB |
Output is correct |