// ~ Be Name Khoda ~ //
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 5e5 + 10;
const int maxnt = 2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int a[maxn5];
vector <int> good[maxn5];
vector <pair<int, int>> req[maxn5];
ll out[maxn5];
namespace seg{
ll mx[maxnt];
int lazy[maxnt], cons[maxnt];
void build(int l, int r, int v){
if(r - l == 1){
cons[v] = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, v * 2);
build(mid, r, v * 2 + 1);
cons[v] = max(cons[v * 2], cons[v * 2 + 1]);
}
void shift(int v){
if(lazy[v] > lazy[v * 2]){
lazy[v * 2] = lazy[v];
mx[v * 2] = max(mx[v * 2], cons[v * 2] + ll(lazy[v]));
}
if(lazy[v] > lazy[v * 2 + 1]){
lazy[v * 2 + 1] = lazy[v];
mx[v * 2 + 1] = max(mx[v * 2 + 1], cons[v * 2 + 1] + ll(lazy[v]));
}
lazy[v] = 0;
}
void max_mos(int l, int r, int lq, int rq, int val, int v){
if(rq <= l || r <= lq || val <= lazy[v])
return;
if(lq <= l && r <= rq){
lazy[v] = max(lazy[v], val);
mx[v] = max(mx[v], cons[v] + ll(val));
return;
}
int mid = (l + r) >> 1;
shift(v);
max_mos(l, mid, lq, rq, val, v * 2);
max_mos(mid, r, lq, rq, val, v * 2 + 1);
mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
}
ll get_max(int l, int r, int lq, int rq, int v){
if(rq <= l || r <= lq)
return 0;
if(lq <= l && r <= rq)
return mx[v];
int mid = (l + r) >> 1;
shift(v);
return max(get_max(l, mid, lq, rq, v * 2), get_max(mid, r, lq, rq, v * 2 + 1));
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector <int> av;
for(int i = 0; i < n; i++){
cin >> a[i];
while(av.size() && a[av.back()] < a[i]){
good[av.back()].pb(i);
av.pop_back();
}
if(av.size())
good[av.back()].pb(i);
av.pb(i);
}
int q; cin >> q;
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r;
l--; r--;
req[l].pb({r, i});
}
seg::build(0, n, 1);
for(int i = n - 1; i >= 0; i--){
for(auto x : good[i]){
int len = x - i;
seg::max_mos(0, n, x + len, n, a[i] + a[x], 1);
}
for(auto [r, id] : req[i])
out[id] = seg::get_max(0, n, i, r + 1, 1);
}
for(int i = 0; i < q; i++)
cout << out[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23812 KB |
Output is correct |
4 |
Correct |
14 ms |
23816 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
10 ms |
23800 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23852 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23812 KB |
Output is correct |
4 |
Correct |
14 ms |
23816 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
10 ms |
23800 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23852 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
258 ms |
44824 KB |
Output is correct |
12 |
Correct |
295 ms |
44672 KB |
Output is correct |
13 |
Correct |
242 ms |
44792 KB |
Output is correct |
14 |
Correct |
256 ms |
44788 KB |
Output is correct |
15 |
Correct |
296 ms |
44772 KB |
Output is correct |
16 |
Correct |
255 ms |
44192 KB |
Output is correct |
17 |
Correct |
239 ms |
44092 KB |
Output is correct |
18 |
Correct |
252 ms |
44036 KB |
Output is correct |
19 |
Correct |
235 ms |
44692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
40964 KB |
Output is correct |
2 |
Correct |
76 ms |
40776 KB |
Output is correct |
3 |
Correct |
73 ms |
41520 KB |
Output is correct |
4 |
Correct |
122 ms |
41000 KB |
Output is correct |
5 |
Correct |
149 ms |
40960 KB |
Output is correct |
6 |
Correct |
122 ms |
40436 KB |
Output is correct |
7 |
Correct |
117 ms |
40152 KB |
Output is correct |
8 |
Correct |
117 ms |
40224 KB |
Output is correct |
9 |
Correct |
118 ms |
40520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23812 KB |
Output is correct |
4 |
Correct |
14 ms |
23816 KB |
Output is correct |
5 |
Correct |
10 ms |
23764 KB |
Output is correct |
6 |
Correct |
10 ms |
23800 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23852 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
258 ms |
44824 KB |
Output is correct |
12 |
Correct |
295 ms |
44672 KB |
Output is correct |
13 |
Correct |
242 ms |
44792 KB |
Output is correct |
14 |
Correct |
256 ms |
44788 KB |
Output is correct |
15 |
Correct |
296 ms |
44772 KB |
Output is correct |
16 |
Correct |
255 ms |
44192 KB |
Output is correct |
17 |
Correct |
239 ms |
44092 KB |
Output is correct |
18 |
Correct |
252 ms |
44036 KB |
Output is correct |
19 |
Correct |
235 ms |
44692 KB |
Output is correct |
20 |
Correct |
144 ms |
40964 KB |
Output is correct |
21 |
Correct |
76 ms |
40776 KB |
Output is correct |
22 |
Correct |
73 ms |
41520 KB |
Output is correct |
23 |
Correct |
122 ms |
41000 KB |
Output is correct |
24 |
Correct |
149 ms |
40960 KB |
Output is correct |
25 |
Correct |
122 ms |
40436 KB |
Output is correct |
26 |
Correct |
117 ms |
40152 KB |
Output is correct |
27 |
Correct |
117 ms |
40224 KB |
Output is correct |
28 |
Correct |
118 ms |
40520 KB |
Output is correct |
29 |
Correct |
839 ms |
88120 KB |
Output is correct |
30 |
Correct |
680 ms |
87644 KB |
Output is correct |
31 |
Correct |
678 ms |
89492 KB |
Output is correct |
32 |
Correct |
836 ms |
88096 KB |
Output is correct |
33 |
Correct |
848 ms |
88188 KB |
Output is correct |
34 |
Correct |
841 ms |
85800 KB |
Output is correct |
35 |
Correct |
833 ms |
85616 KB |
Output is correct |
36 |
Correct |
805 ms |
85532 KB |
Output is correct |
37 |
Correct |
810 ms |
86984 KB |
Output is correct |
38 |
Correct |
552 ms |
93788 KB |
Output is correct |
39 |
Correct |
551 ms |
93780 KB |
Output is correct |
40 |
Correct |
556 ms |
90544 KB |
Output is correct |
41 |
Correct |
535 ms |
90060 KB |
Output is correct |
42 |
Correct |
554 ms |
89940 KB |
Output is correct |
43 |
Correct |
555 ms |
91728 KB |
Output is correct |
44 |
Correct |
611 ms |
93136 KB |
Output is correct |
45 |
Correct |
580 ms |
93224 KB |
Output is correct |
46 |
Correct |
582 ms |
90188 KB |
Output is correct |
47 |
Correct |
578 ms |
89688 KB |
Output is correct |
48 |
Correct |
599 ms |
89692 KB |
Output is correct |
49 |
Correct |
578 ms |
91796 KB |
Output is correct |
50 |
Correct |
651 ms |
91344 KB |
Output is correct |
51 |
Correct |
724 ms |
91368 KB |
Output is correct |
52 |
Correct |
652 ms |
88892 KB |
Output is correct |
53 |
Correct |
686 ms |
88552 KB |
Output is correct |
54 |
Correct |
685 ms |
88508 KB |
Output is correct |
55 |
Correct |
664 ms |
90296 KB |
Output is correct |