#include<bits/stdc++.h>
#define int long long int
#define No_TOI_I_am_Garbage ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int n, a[500050], q;
vector<pair<int, int> > seg;
struct thing {
int left, right, v1, v2, lz;
} arr[500050 << 2];
void build(int left, int right, int m)
{
arr[m].left = left; arr[m].right = right;
arr[m].lz = 0;
if(left == right)
{
arr[m].v1 = arr[m].v2 = a[left];
return;
}
int mid = (left + right) >> 1;
build(left, mid, m * 2);
build(mid + 1, right, m * 2 + 1);
arr[m].v2 = max(arr[m * 2].v2, arr[m * 2 + 1].v2);
arr[m].v1 = arr[m].v2;
}
void down(int m)
{
arr[m * 2].v1 = max(arr[m * 2].v1, arr[m * 2].v2 + arr[m].lz);
arr[m * 2 + 1].v1 = max(arr[m * 2 + 1].v1, arr[m * 2 + 1].v2 + arr[m].lz);
arr[m * 2].lz = max(arr[m].lz, arr[m * 2].lz);
arr[m * 2 + 1].lz = max(arr[m].lz, arr[m * 2 + 1].lz);
}
void modify(int left, int right, int x, int m)
{
if(arr[m].left > right || arr[m].right < left) return;
if(left <= arr[m].left && arr[m].right <= right)
{
arr[m].lz = max(arr[m].lz, x);
arr[m].v1 = max(arr[m].v1, x + arr[m].v2);
return;
}
down(m);
modify(left, right, x, m * 2);
modify(left, right, x, m * 2 + 1);
arr[m].v1 = max(arr[m * 2].v1, arr[m * 2 + 1].v1);
arr[m].v2 = max(arr[m * 2].v2, arr[m * 2 + 1].v2);
return;
}
int search(int left, int right, int m)
{
if(arr[m].left > right || arr[m].right < left) return 0;
if(left <= arr[m].left && arr[m].right <= right)
return arr[m].v1;
down(m);
return max(search(left, right, m * 2), search(left, right, m * 2 + 1));
}
struct he {
int first, second, seq, ans;
} que[500050];
int32_t main()
{
No_TOI_I_am_Garbage
cin >> n;
for(int i=1; i <= n; i ++)
cin >> a[i];
deque<int> Q; Q.push_back(1);
for(int i=2; i <= n; i ++)
{
while(!Q.empty() && a[Q.back()] <= a[i])
{
int top = Q.back(); Q.pop_back();
seg.push_back(make_pair(top, i));
}
if(!Q.empty()) seg.push_back(make_pair(Q.back(), i));
Q.push_back(i);
}
sort(seg.begin(), seg.end(), [](pair<int, int> x, pair<int, int> y) {
return x.first > y.first;
});
cin >> q;
for(int i=0; i < q; i ++)
{
cin >> que[i].first >> que[i].second;
que[i].seq = i;
}
sort(que, que + q, [](he x, he y) {
return x.first > y.first;
});
build(1, n, 1);
int ind = 0;
for(int i=0; i < q; i ++)
{
while(ind < seg.size() && seg[ind].first >= que[i].first)
{
modify(seg[ind].second * 2 - seg[ind].first, n, a[seg[ind].first] + a[seg[ind].second], 1);
// cout << seg[ind].second * 2 - seg[ind].first << ' ' << a[seg[ind].first] + a[seg[ind].second] << ' ' << seg[ind].first << ' ' << seg[ind].second << '\n';
ind ++;
}
// cout << '\n';
que[i].ans = search(que[i].first, que[i].second, 1);
}
sort(que, que + q, [](he x, he y) {
return x.seq < y.seq;
});
for(int i=0; i < q; i ++)
cout << que[i].ans << '\n';
return 0;
}
Compilation message
jumps.cpp: In function 'int32_t main()':
jumps.cpp:92:13: 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]
92 | while(ind < seg.size() && seg[ind].first >= que[i].first)
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
416 ms |
26864 KB |
Output is correct |
12 |
Correct |
411 ms |
26624 KB |
Output is correct |
13 |
Correct |
418 ms |
26604 KB |
Output is correct |
14 |
Correct |
416 ms |
26736 KB |
Output is correct |
15 |
Correct |
412 ms |
26736 KB |
Output is correct |
16 |
Correct |
415 ms |
26096 KB |
Output is correct |
17 |
Correct |
417 ms |
26080 KB |
Output is correct |
18 |
Correct |
430 ms |
26096 KB |
Output is correct |
19 |
Correct |
417 ms |
26608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
30544 KB |
Output is correct |
2 |
Correct |
122 ms |
27488 KB |
Output is correct |
3 |
Correct |
125 ms |
28964 KB |
Output is correct |
4 |
Correct |
228 ms |
30544 KB |
Output is correct |
5 |
Correct |
216 ms |
30544 KB |
Output is correct |
6 |
Correct |
211 ms |
30040 KB |
Output is correct |
7 |
Correct |
215 ms |
29788 KB |
Output is correct |
8 |
Correct |
213 ms |
29784 KB |
Output is correct |
9 |
Correct |
216 ms |
30160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
416 ms |
26864 KB |
Output is correct |
12 |
Correct |
411 ms |
26624 KB |
Output is correct |
13 |
Correct |
418 ms |
26604 KB |
Output is correct |
14 |
Correct |
416 ms |
26736 KB |
Output is correct |
15 |
Correct |
412 ms |
26736 KB |
Output is correct |
16 |
Correct |
415 ms |
26096 KB |
Output is correct |
17 |
Correct |
417 ms |
26080 KB |
Output is correct |
18 |
Correct |
430 ms |
26096 KB |
Output is correct |
19 |
Correct |
417 ms |
26608 KB |
Output is correct |
20 |
Correct |
222 ms |
30544 KB |
Output is correct |
21 |
Correct |
122 ms |
27488 KB |
Output is correct |
22 |
Correct |
125 ms |
28964 KB |
Output is correct |
23 |
Correct |
228 ms |
30544 KB |
Output is correct |
24 |
Correct |
216 ms |
30544 KB |
Output is correct |
25 |
Correct |
211 ms |
30040 KB |
Output is correct |
26 |
Correct |
215 ms |
29788 KB |
Output is correct |
27 |
Correct |
213 ms |
29784 KB |
Output is correct |
28 |
Correct |
216 ms |
30160 KB |
Output is correct |
29 |
Correct |
1350 ms |
92796 KB |
Output is correct |
30 |
Correct |
1044 ms |
84840 KB |
Output is correct |
31 |
Correct |
1042 ms |
88960 KB |
Output is correct |
32 |
Correct |
1323 ms |
92960 KB |
Output is correct |
33 |
Correct |
1297 ms |
92732 KB |
Output is correct |
34 |
Correct |
1316 ms |
90532 KB |
Output is correct |
35 |
Correct |
1301 ms |
90216 KB |
Output is correct |
36 |
Correct |
1301 ms |
90172 KB |
Output is correct |
37 |
Correct |
1306 ms |
91712 KB |
Output is correct |
38 |
Correct |
1039 ms |
92988 KB |
Output is correct |
39 |
Correct |
1025 ms |
92988 KB |
Output is correct |
40 |
Correct |
1022 ms |
89404 KB |
Output is correct |
41 |
Correct |
1030 ms |
88980 KB |
Output is correct |
42 |
Correct |
1018 ms |
89048 KB |
Output is correct |
43 |
Correct |
1030 ms |
90836 KB |
Output is correct |
44 |
Correct |
1065 ms |
92860 KB |
Output is correct |
45 |
Correct |
1056 ms |
92860 KB |
Output is correct |
46 |
Correct |
1043 ms |
89788 KB |
Output is correct |
47 |
Correct |
1047 ms |
89404 KB |
Output is correct |
48 |
Correct |
1034 ms |
89452 KB |
Output is correct |
49 |
Correct |
1071 ms |
91452 KB |
Output is correct |
50 |
Correct |
1175 ms |
92988 KB |
Output is correct |
51 |
Correct |
1126 ms |
92988 KB |
Output is correct |
52 |
Correct |
1124 ms |
90428 KB |
Output is correct |
53 |
Correct |
1121 ms |
90196 KB |
Output is correct |
54 |
Correct |
1110 ms |
90172 KB |
Output is correct |
55 |
Correct |
1121 ms |
91836 KB |
Output is correct |