#include <bits/stdc++.h>
#define int long long
using namespace std;
template <typename T>
string to_string (T x) {
string S = "[";
bool first = true;
for (const auto v : x) {
if (!first) S += ", ";
first = false;
S += to_string(v);
}
S += "]";
return S;
}
string to_string (string S) { return S; }
template <typename A, typename B>
string to_string (pair<A, B> p) {
return "(" + to_string(p.first )+ ", " + to_string(p.second) + ")";
}
void dbg_out () { cout << endl; }
template <typename Head, typename... Tail>
void dbg_out (Head H, Tail... T) {
cout << to_string(H) << " ";
dbg_out(T...);
}
#ifdef DEBUG
# define dbg(...) { cout << "(" << #__VA_ARGS__ << "): "; dbg_out(__VA_ARGS__); }
#else
# define dbg(...)
#endif
using idata = vector<int>;
using igrid = vector<idata>;
template<typename T>
using grid = vector<vector<T>>;
using di = pair<int, int>; using vd = vector<di>;
using ti = pair<di, int> ; using vt = vector<ti>;
igrid opt;
struct Monoid {
int p = 0, q = 0, ppq = 0;
Monoid () = default;
Monoid (int p, int q, int ppq) : p(p), q(q), ppq(ppq) {}
static Monoid merge (Monoid &left, Monoid &right) {
return Monoid( max(left.p, right.p), max(left.q, right.q), max(left.p + right.q, max(left.ppq, right.ppq)) );
}
};
struct SegTree {
vector<Monoid> tree;
int start, height, size;
SegTree (int _size) {
size = _size;
height = ceil(log2(size));
start = 1 << height;
tree.resize(start << 1);
}
void update (int i) {
i += start;
i >>= 1;
while (i != 0) {
tree[i] = Monoid::merge(tree[2 * i], tree[2 * i + 1]);
i >>= 1;
}
}
void modify_p (int i, int p) {
tree[i + start].p = max(tree[i + start].p, p);
tree[i + start].ppq = max(tree[i + start].ppq, tree[i + start].p + tree[i + start].q);
update(i);
}
void modify_q (int i, int q) {
tree[i + start].q = max(tree[i + start].q, q);
tree[i + start].ppq = max(tree[i + start].ppq, tree[i + start].p + tree[i + start].q);
update(i);
}
int query (int l, int r) {
l += start; r += start;
Monoid res;
idata qL, qR;
while (l < r) {
if ((l & 1) == 1) qL.push_back(l);
if ((r & 1) == 0) qR.push_back(r);
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
if (l == r) qL.push_back(l);
reverse(qR.begin(), qR.end());
for (int dR : qR) qL.push_back(dR);
for (int h : qL) res = Monoid::merge(res, tree[h]);
return res.ppq;
}
};
signed main () {
int N;
cin >> N;
idata A(N);
for (int i = 0; i < N; i ++)
cin >> A[i];
opt.resize(N);
deque<int> local;
for (int i = 0; i < N; i ++) {
while (local.size() != 0 && A[local.front()] < A[i]) {
int curr = local.front(); local.pop_front();
opt[i].push_back(curr);
}
if (local.size() != 0) opt[i].push_back(local.front());
local.push_front(i);
}
vd opt_q;
for (int i = 0; i < N; i ++) {
for (int a : opt[i]) {
opt_q.push_back({ a, i });
}
}
sort(opt_q.begin(), opt_q.end());
reverse(opt_q.begin(), opt_q.end());
int Q;
cin >> Q;
idata answer(Q);
vt queries;
for (int q = 0; q < Q; q ++) {
int l, r;
cin >> l >> r;
l --; r --;
queries.push_back({ { l, r }, q });
}
sort(queries.begin(), queries.end());
reverse(queries.begin(), queries.end());
int opt_q_id = 0;
int query_id = 0;
SegTree tree(N);
for (int i = 0; i < N; i ++)
tree.modify_q(i, A[i]);
for (int i = N - 1; i >= 0; i --) {
while (opt_q_id != opt_q.size() && opt_q[opt_q_id].first == i) {
int optA = opt_q[opt_q_id].first;
int optB = opt_q[opt_q_id].second;
opt_q_id ++;
int optC = 2 * optB - optA;
if (optC > N) continue ;
tree.modify_p(optC, A[optA] + A[optB]);
}
while (query_id != queries.size() && queries[query_id].first.first == i) {
int l = queries[query_id].first.first;
int r = queries[query_id].first.second;
int i = queries[query_id].second;
query_id ++;
answer[i] = tree.query(l, r);
}
}
for (int u : answer) cout << u << "\n";
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:167:25: 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]
167 | while (opt_q_id != opt_q.size() && opt_q[opt_q_id].first == i) {
| ~~~~~~~~~^~~~~~~~~~~~~~~
jumps.cpp:178:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
178 | while (query_id != queries.size() && queries[query_id].first.first == i) {
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
406 ms |
21836 KB |
Output is correct |
12 |
Correct |
405 ms |
23056 KB |
Output is correct |
13 |
Correct |
412 ms |
22924 KB |
Output is correct |
14 |
Correct |
421 ms |
23132 KB |
Output is correct |
15 |
Correct |
402 ms |
23124 KB |
Output is correct |
16 |
Correct |
418 ms |
22428 KB |
Output is correct |
17 |
Correct |
424 ms |
22360 KB |
Output is correct |
18 |
Correct |
407 ms |
22348 KB |
Output is correct |
19 |
Correct |
429 ms |
22892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
32700 KB |
Output is correct |
2 |
Correct |
104 ms |
28348 KB |
Output is correct |
3 |
Correct |
114 ms |
30012 KB |
Output is correct |
4 |
Correct |
139 ms |
32736 KB |
Output is correct |
5 |
Correct |
140 ms |
32676 KB |
Output is correct |
6 |
Correct |
129 ms |
32728 KB |
Output is correct |
7 |
Correct |
125 ms |
32684 KB |
Output is correct |
8 |
Correct |
126 ms |
32644 KB |
Output is correct |
9 |
Correct |
134 ms |
32760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
406 ms |
21836 KB |
Output is correct |
12 |
Correct |
405 ms |
23056 KB |
Output is correct |
13 |
Correct |
412 ms |
22924 KB |
Output is correct |
14 |
Correct |
421 ms |
23132 KB |
Output is correct |
15 |
Correct |
402 ms |
23124 KB |
Output is correct |
16 |
Correct |
418 ms |
22428 KB |
Output is correct |
17 |
Correct |
424 ms |
22360 KB |
Output is correct |
18 |
Correct |
407 ms |
22348 KB |
Output is correct |
19 |
Correct |
429 ms |
22892 KB |
Output is correct |
20 |
Correct |
141 ms |
32700 KB |
Output is correct |
21 |
Correct |
104 ms |
28348 KB |
Output is correct |
22 |
Correct |
114 ms |
30012 KB |
Output is correct |
23 |
Correct |
139 ms |
32736 KB |
Output is correct |
24 |
Correct |
140 ms |
32676 KB |
Output is correct |
25 |
Correct |
129 ms |
32728 KB |
Output is correct |
26 |
Correct |
125 ms |
32684 KB |
Output is correct |
27 |
Correct |
126 ms |
32644 KB |
Output is correct |
28 |
Correct |
134 ms |
32760 KB |
Output is correct |
29 |
Correct |
932 ms |
115852 KB |
Output is correct |
30 |
Correct |
826 ms |
98700 KB |
Output is correct |
31 |
Correct |
836 ms |
102784 KB |
Output is correct |
32 |
Correct |
932 ms |
115940 KB |
Output is correct |
33 |
Correct |
931 ms |
115952 KB |
Output is correct |
34 |
Correct |
901 ms |
115224 KB |
Output is correct |
35 |
Correct |
909 ms |
115088 KB |
Output is correct |
36 |
Correct |
903 ms |
115080 KB |
Output is correct |
37 |
Correct |
911 ms |
115720 KB |
Output is correct |
38 |
Correct |
813 ms |
115932 KB |
Output is correct |
39 |
Correct |
810 ms |
115904 KB |
Output is correct |
40 |
Correct |
755 ms |
114232 KB |
Output is correct |
41 |
Correct |
735 ms |
113848 KB |
Output is correct |
42 |
Correct |
739 ms |
113932 KB |
Output is correct |
43 |
Correct |
781 ms |
114844 KB |
Output is correct |
44 |
Correct |
847 ms |
115952 KB |
Output is correct |
45 |
Correct |
809 ms |
115848 KB |
Output is correct |
46 |
Correct |
782 ms |
114320 KB |
Output is correct |
47 |
Correct |
794 ms |
114192 KB |
Output is correct |
48 |
Correct |
780 ms |
114208 KB |
Output is correct |
49 |
Correct |
803 ms |
115344 KB |
Output is correct |
50 |
Correct |
833 ms |
115812 KB |
Output is correct |
51 |
Correct |
860 ms |
115784 KB |
Output is correct |
52 |
Correct |
791 ms |
114972 KB |
Output is correct |
53 |
Correct |
803 ms |
114968 KB |
Output is correct |
54 |
Correct |
816 ms |
114960 KB |
Output is correct |
55 |
Correct |
839 ms |
115596 KB |
Output is correct |