#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <ll, ll>;
using pil = pair <ll, ll>;
using pli = pair <ll, ll>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const ll maxN = 5e5 + 1;
ll n;
ll a[maxN];
vector <ll> Q;
vector <pii> p;
ll ans[maxN];
pii st[maxN * 4];
ll lazy[maxN * 4];
struct TQuery{
ll l, r, idx;
};
TQuery q[maxN];
void build(ll id = 1, ll l = 1, ll r = n){
if (l == r){
st[id].fi = st[id].se = a[l];
return;
}
ll mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id].fi = st[id].se = max(st[id * 2].se, st[id * 2 + 1].se);
}
void pull(ll id){
if (lazy[id]){
st[id * 2].fi = max(st[id * 2].fi, st[id * 2].se + lazy[id]);
st[id * 2 + 1].fi = max(st[id * 2 + 1].fi, st[id * 2 + 1].se + lazy[id]);
lazy[id * 2] = max(lazy[id * 2], lazy[id]);
lazy[id * 2 + 1] = max(lazy[id * 2 + 1], lazy[id]);
lazy[id] = 0;
}
}
void update(ll id, ll i, ll j, ll val, ll l = 1, ll r = n){
if (r < i || l > j) return;
if (i <= l && r <= j){
st[id].fi = max(st[id].fi, st[id].se + val);
lazy[id] = max(lazy[id], val);
return;
}
pull(id);
ll mid = (l + r) / 2;
update(id * 2, i, j, val, l, mid);
update(id * 2 + 1, i, j, val, mid + 1, r);
st[id].fi = max(st[id * 2].fi, st[id * 2 + 1].fi);
}
ll get(ll id, ll i, ll j, ll l = 1, ll r = n){
if (r < i || l > j) return 0;
if (i <= l && r <= j){
return st[id].fi;
}
pull(id);
ll mid = (l + r) / 2;
return max(get(id * 2, i, j, l, mid), get(id * 2 + 1, i, j, mid + 1, r));
}
void create(){
for (ll i = n; i >= 1; --i){
while (!Q.empty() && a[i] >= a[Q.back()]){
p.pb({i, Q.back()});
Q.pop_back();
}
if (!Q.empty()) p.pb({i, Q.back()});
Q.pb(i);
}
sort(p.begin(), p.end(), greater<pair<ll, ll>>());
}
void Init(){
cin >> n;
for (ll i = 1; i <= n; ++i) cin >> a[i];
create();
build();
ll qq; cin >> qq;
for (ll i = 1; i <= qq; ++i){
cin >> q[i].l >> q[i].r;
q[i].idx = i;
}
sort(q + 1, q + qq + 1,[](const auto &i, const auto &j){
return i.l > j.l;
});
ll j = 0;
for (ll i = 1; i <= qq; ++i){
while (j < p.size() && p[j].fi >= q[i].l){
update(1, 2 * p[j].se - p[j].fi, n, a[p[j].se] + a[p[j].fi]);
++j;
}
ans[q[i].idx] = get(1, q[i].l, q[i].r);
}
for (ll i = 1; i <= qq; ++i) cout << ans[i] << "\n";
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
jumps.cpp: In function 'void Init()':
jumps.cpp:114:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | while (j < p.size() && p[j].fi >= q[i].l){
| ~~^~~~~~~~~~
jumps.cpp: In function 'int main()':
jumps.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
380 KB |
Output is correct |
11 |
Correct |
275 ms |
21680 KB |
Output is correct |
12 |
Correct |
260 ms |
26340 KB |
Output is correct |
13 |
Correct |
300 ms |
26300 KB |
Output is correct |
14 |
Correct |
255 ms |
26424 KB |
Output is correct |
15 |
Correct |
297 ms |
26364 KB |
Output is correct |
16 |
Correct |
266 ms |
25840 KB |
Output is correct |
17 |
Correct |
280 ms |
25676 KB |
Output is correct |
18 |
Correct |
329 ms |
25688 KB |
Output is correct |
19 |
Correct |
256 ms |
26224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
21224 KB |
Output is correct |
2 |
Correct |
81 ms |
22740 KB |
Output is correct |
3 |
Correct |
75 ms |
19224 KB |
Output is correct |
4 |
Correct |
139 ms |
22260 KB |
Output is correct |
5 |
Correct |
134 ms |
22292 KB |
Output is correct |
6 |
Correct |
118 ms |
21644 KB |
Output is correct |
7 |
Correct |
117 ms |
21536 KB |
Output is correct |
8 |
Correct |
120 ms |
21496 KB |
Output is correct |
9 |
Correct |
123 ms |
21848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
380 KB |
Output is correct |
11 |
Correct |
275 ms |
21680 KB |
Output is correct |
12 |
Correct |
260 ms |
26340 KB |
Output is correct |
13 |
Correct |
300 ms |
26300 KB |
Output is correct |
14 |
Correct |
255 ms |
26424 KB |
Output is correct |
15 |
Correct |
297 ms |
26364 KB |
Output is correct |
16 |
Correct |
266 ms |
25840 KB |
Output is correct |
17 |
Correct |
280 ms |
25676 KB |
Output is correct |
18 |
Correct |
329 ms |
25688 KB |
Output is correct |
19 |
Correct |
256 ms |
26224 KB |
Output is correct |
20 |
Correct |
128 ms |
21224 KB |
Output is correct |
21 |
Correct |
81 ms |
22740 KB |
Output is correct |
22 |
Correct |
75 ms |
19224 KB |
Output is correct |
23 |
Correct |
139 ms |
22260 KB |
Output is correct |
24 |
Correct |
134 ms |
22292 KB |
Output is correct |
25 |
Correct |
118 ms |
21644 KB |
Output is correct |
26 |
Correct |
117 ms |
21536 KB |
Output is correct |
27 |
Correct |
120 ms |
21496 KB |
Output is correct |
28 |
Correct |
123 ms |
21848 KB |
Output is correct |
29 |
Correct |
812 ms |
74212 KB |
Output is correct |
30 |
Correct |
696 ms |
75800 KB |
Output is correct |
31 |
Correct |
700 ms |
67856 KB |
Output is correct |
32 |
Correct |
865 ms |
75716 KB |
Output is correct |
33 |
Correct |
880 ms |
75944 KB |
Output is correct |
34 |
Correct |
859 ms |
73968 KB |
Output is correct |
35 |
Correct |
786 ms |
73628 KB |
Output is correct |
36 |
Correct |
799 ms |
73580 KB |
Output is correct |
37 |
Correct |
884 ms |
74904 KB |
Output is correct |
38 |
Correct |
612 ms |
75736 KB |
Output is correct |
39 |
Correct |
660 ms |
75752 KB |
Output is correct |
40 |
Correct |
673 ms |
72736 KB |
Output is correct |
41 |
Correct |
596 ms |
72280 KB |
Output is correct |
42 |
Correct |
609 ms |
72232 KB |
Output is correct |
43 |
Correct |
649 ms |
74032 KB |
Output is correct |
44 |
Correct |
636 ms |
75912 KB |
Output is correct |
45 |
Correct |
634 ms |
75888 KB |
Output is correct |
46 |
Correct |
636 ms |
72996 KB |
Output is correct |
47 |
Correct |
651 ms |
72532 KB |
Output is correct |
48 |
Correct |
642 ms |
72648 KB |
Output is correct |
49 |
Correct |
673 ms |
74556 KB |
Output is correct |
50 |
Correct |
757 ms |
75708 KB |
Output is correct |
51 |
Correct |
709 ms |
74608 KB |
Output is correct |
52 |
Correct |
689 ms |
73724 KB |
Output is correct |
53 |
Correct |
707 ms |
73444 KB |
Output is correct |
54 |
Correct |
693 ms |
73304 KB |
Output is correct |
55 |
Correct |
691 ms |
74864 KB |
Output is correct |