#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template<class Info,
class Merge = std::plus<Info>>
struct SegmentTree {
const int n;
const Merge merge;
std::vector<Info> info;
SegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)) {}
SegmentTree(std::vector<Info> init) : SegmentTree(init.size()) {
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = merge(info[2 * p], info[2 * p + 1]);
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y));
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
};
struct Info {
int x;
Info(int x = 0) : x(x) {}
};
Info operator+(const Info &a, const Info &b) {
return Info(max(a.x, b.x));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, X;
cin >> n >> X;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> f = a;
sort(f.begin(), f.end());
f.erase(unique(f.begin(), f.end()), f.end());
vector<int> b(n);
for (int i = 0; i < n; i++) {
b[i] = lower_bound(f.begin(), f.end(), a[i]) - f.begin();
}
vector<int> dp(n);
SegmentTree<Info> s(n);
vector<int> changes;
for (int i = n - 1; i >= 0; i--) {
int x = s.rangeQuery(b[i] + 1, n).x;
dp[i] = x + 1;
changes.emplace_back(s.rangeQuery(b[i], b[i] + 1).x);
s.modify(b[i], Info(dp[i]));
}
SegmentTree<Info> t(n);
int ans = 1;
for (int i = 0; i < n; i++) {
s.modify(b[i], Info(changes.back()));
changes.pop_back();
int x = t.rangeQuery(0, b[i]).x;
int p = upper_bound(f.begin(), f.end(), f[b[i]] - X) - f.begin();
ans = max(ans, x + 1 + s.rangeQuery(p, n).x);
t.modify(b[i], Info(x + 1));
}
cout << ans << "\n";
return 0;
}
# |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
8496 KB |
Output is correct |
2 |
Correct |
368 ms |
10416 KB |
Output is correct |
3 |
Correct |
376 ms |
10360 KB |
Output is correct |
4 |
Correct |
337 ms |
10468 KB |
Output is correct |
5 |
Correct |
156 ms |
9584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
2468 KB |
Output is correct |
2 |
Correct |
73 ms |
2932 KB |
Output is correct |
3 |
Correct |
72 ms |
2948 KB |
Output is correct |
4 |
Correct |
33 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
39 ms |
2684 KB |
Output is correct |
7 |
Correct |
55 ms |
2884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
4464 KB |
Output is correct |
2 |
Correct |
126 ms |
5356 KB |
Output is correct |
3 |
Correct |
260 ms |
10324 KB |
Output is correct |
4 |
Correct |
137 ms |
9548 KB |
Output is correct |
5 |
Correct |
73 ms |
4992 KB |
Output is correct |
6 |
Correct |
170 ms |
9388 KB |
Output is correct |
7 |
Correct |
145 ms |
10036 KB |
Output is correct |
8 |
Correct |
118 ms |
5452 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
317 ms |
8496 KB |
Output is correct |
28 |
Correct |
368 ms |
10416 KB |
Output is correct |
29 |
Correct |
376 ms |
10360 KB |
Output is correct |
30 |
Correct |
337 ms |
10468 KB |
Output is correct |
31 |
Correct |
156 ms |
9584 KB |
Output is correct |
32 |
Correct |
64 ms |
2468 KB |
Output is correct |
33 |
Correct |
73 ms |
2932 KB |
Output is correct |
34 |
Correct |
72 ms |
2948 KB |
Output is correct |
35 |
Correct |
33 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
39 ms |
2684 KB |
Output is correct |
38 |
Correct |
55 ms |
2884 KB |
Output is correct |
39 |
Correct |
138 ms |
4464 KB |
Output is correct |
40 |
Correct |
126 ms |
5356 KB |
Output is correct |
41 |
Correct |
260 ms |
10324 KB |
Output is correct |
42 |
Correct |
137 ms |
9548 KB |
Output is correct |
43 |
Correct |
73 ms |
4992 KB |
Output is correct |
44 |
Correct |
170 ms |
9388 KB |
Output is correct |
45 |
Correct |
145 ms |
10036 KB |
Output is correct |
46 |
Correct |
118 ms |
5452 KB |
Output is correct |
47 |
Correct |
145 ms |
5380 KB |
Output is correct |
48 |
Correct |
139 ms |
5440 KB |
Output is correct |
49 |
Correct |
390 ms |
10408 KB |
Output is correct |
50 |
Correct |
143 ms |
9652 KB |
Output is correct |
51 |
Correct |
120 ms |
8364 KB |
Output is correct |
52 |
Correct |
190 ms |
9788 KB |
Output is correct |
53 |
Correct |
184 ms |
9732 KB |
Output is correct |
54 |
Correct |
147 ms |
10516 KB |
Output is correct |
55 |
Correct |
248 ms |
10448 KB |
Output is correct |