#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, a[maxN], Q;
int res[maxN];
struct TQuery
{
int l, r, id;
} q[maxN];
void ReadInput()
{
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
cin >> Q;
for(int i=1; i<=Q; i++)
{
int l, r;
cin >> l >> r;
q[i] = {l, r, i};
}
}
pair<int, int> st[4 * maxN];
int lazy[4 * maxN];
void down(int id)
{
int t = lazy[id];
lazy[id] = 0;
st[id * 2].fi = max(st[id * 2].fi, st[id * 2].se + t);
st[id * 2 + 1].fi = max(st[id * 2 + 1].fi, st[id * 2 + 1].se + t);
lazy[id * 2] = max(lazy[id * 2], t);
lazy[id * 2 + 1] = max(lazy[id * 2 + 1], t);
}
void build(int id, int l, int r)
{
if(l == r)
{
st[id].se = a[l];
return;
}
int 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 update(int id, int l, int r, int u, int v, int val)
{
if(l > v || r < u) return;
if(l >= u && r <= v)
{
st[id].fi = max(st[id].fi, st[id].se + val);
lazy[id] = max(lazy[id], val);
return;
}
down(id);
int mid = (l + r) / 2;
update(id * 2, l, mid, u, v, val);
update(id * 2 + 1, mid + 1, r, u, v, val);
st[id].fi = max(st[id * 2].fi, st[id * 2 + 1].fi);
}
int get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return 0;
if(l >= u && r <= v) return st[id].fi;
down(id);
int mid = (l + r) / 2;
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
void Solve()
{
sort(q + 1, q + Q + 1, [](TQuery p, TQuery q)
{
return p.l > q.l;
});
build(1, 1, n);
vector<pair<int, int>> vc;
stack<int> s;
for(int i=n; i>=1; i--)
{
while(!s.empty() && a[i] >= a[s.top()])
{
vc.pb({i, s.top()});
s.pop();
}
if(!s.empty()) vc.pb({i, s.top()});
s.push(i);
}
sort(vc.begin(), vc.end(), greater<pair<int, int>>());
int p = 1;
for(int i=1; i<=Q; i++)
{
while(p < vc.size() && vc[p].fi >= q[i].l)
{
update(1, 1, n, 2 * vc[p].se - vc[p].fi, n, a[vc[p].fi] + a[vc[p].se]);
// cout << get(1, 1, n, q[i].l, q[i].r) << '\n';
p++;
}
res[q[i].id] = get(1, 1, n, q[i].l, q[i].r);
}
for(int i=1; i<=Q; i++) cout << res[i] << '\n';
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
Compilation message
jumps.cpp: In function 'void Solve()':
jumps.cpp:102:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while(p < vc.size() && vc[p].fi >= q[i].l)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
366 ms |
25736 KB |
Output is correct |
12 |
Correct |
298 ms |
25608 KB |
Output is correct |
13 |
Correct |
270 ms |
25688 KB |
Output is correct |
14 |
Correct |
287 ms |
25816 KB |
Output is correct |
15 |
Correct |
287 ms |
25800 KB |
Output is correct |
16 |
Correct |
281 ms |
25084 KB |
Output is correct |
17 |
Correct |
304 ms |
25044 KB |
Output is correct |
18 |
Correct |
289 ms |
25044 KB |
Output is correct |
19 |
Correct |
309 ms |
25688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
22320 KB |
Output is correct |
2 |
Correct |
77 ms |
20740 KB |
Output is correct |
3 |
Correct |
83 ms |
19220 KB |
Output is correct |
4 |
Correct |
132 ms |
22368 KB |
Output is correct |
5 |
Correct |
135 ms |
22364 KB |
Output is correct |
6 |
Correct |
140 ms |
21676 KB |
Output is correct |
7 |
Correct |
130 ms |
21592 KB |
Output is correct |
8 |
Correct |
132 ms |
21512 KB |
Output is correct |
9 |
Correct |
142 ms |
21832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
366 ms |
25736 KB |
Output is correct |
12 |
Correct |
298 ms |
25608 KB |
Output is correct |
13 |
Correct |
270 ms |
25688 KB |
Output is correct |
14 |
Correct |
287 ms |
25816 KB |
Output is correct |
15 |
Correct |
287 ms |
25800 KB |
Output is correct |
16 |
Correct |
281 ms |
25084 KB |
Output is correct |
17 |
Correct |
304 ms |
25044 KB |
Output is correct |
18 |
Correct |
289 ms |
25044 KB |
Output is correct |
19 |
Correct |
309 ms |
25688 KB |
Output is correct |
20 |
Correct |
186 ms |
22320 KB |
Output is correct |
21 |
Correct |
77 ms |
20740 KB |
Output is correct |
22 |
Correct |
83 ms |
19220 KB |
Output is correct |
23 |
Correct |
132 ms |
22368 KB |
Output is correct |
24 |
Correct |
135 ms |
22364 KB |
Output is correct |
25 |
Correct |
140 ms |
21676 KB |
Output is correct |
26 |
Correct |
130 ms |
21592 KB |
Output is correct |
27 |
Correct |
132 ms |
21512 KB |
Output is correct |
28 |
Correct |
142 ms |
21832 KB |
Output is correct |
29 |
Correct |
859 ms |
69400 KB |
Output is correct |
30 |
Correct |
762 ms |
65464 KB |
Output is correct |
31 |
Correct |
720 ms |
61608 KB |
Output is correct |
32 |
Correct |
891 ms |
69416 KB |
Output is correct |
33 |
Correct |
845 ms |
69428 KB |
Output is correct |
34 |
Correct |
900 ms |
68820 KB |
Output is correct |
35 |
Correct |
886 ms |
68688 KB |
Output is correct |
36 |
Correct |
842 ms |
68704 KB |
Output is correct |
37 |
Correct |
832 ms |
69292 KB |
Output is correct |
38 |
Correct |
657 ms |
69492 KB |
Output is correct |
39 |
Correct |
671 ms |
69488 KB |
Output is correct |
40 |
Correct |
636 ms |
67736 KB |
Output is correct |
41 |
Correct |
635 ms |
67496 KB |
Output is correct |
42 |
Correct |
752 ms |
67472 KB |
Output is correct |
43 |
Correct |
661 ms |
68392 KB |
Output is correct |
44 |
Correct |
681 ms |
69428 KB |
Output is correct |
45 |
Correct |
786 ms |
69444 KB |
Output is correct |
46 |
Correct |
657 ms |
67860 KB |
Output is correct |
47 |
Correct |
669 ms |
67680 KB |
Output is correct |
48 |
Correct |
674 ms |
67804 KB |
Output is correct |
49 |
Correct |
674 ms |
69024 KB |
Output is correct |
50 |
Correct |
737 ms |
69432 KB |
Output is correct |
51 |
Correct |
712 ms |
69444 KB |
Output is correct |
52 |
Correct |
763 ms |
68620 KB |
Output is correct |
53 |
Correct |
735 ms |
68544 KB |
Output is correct |
54 |
Correct |
787 ms |
68532 KB |
Output is correct |
55 |
Correct |
840 ms |
69308 KB |
Output is correct |