#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define fi first
#define se second
#define BIT(x, i) (((x) >> (i)) & 1)
using namespace std;
/** END OF TEMPLATE **/
const int mxN = 2e5 + 5;
int n, d;
struct fenwick{
vector<int> bit;
int sz;
fenwick(int t) : sz(t), bit(t+1, 0) {}
void update(int id, int val) {
for(; id <= sz; id += id&-id)
bit[id] = max(bit[id], val);
}
int get(int id) {
int res = 0;
for(; id > 0; id -= id&-id)
res = max(res, bit[id]);
return res;
}
};
int a[mxN], b[mxN];
int main()
{
FastIO;
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
cin >> n >> d;
vector<int> t;
FOR(i, 1, n) {
cin >> a[i];
b[i] = a[i] + d;
t.PB(a[i]); t.PB(b[i]);
}
sort(ALL(t)); t.erase(unique(ALL(t)), t.end());
FOR(i, 1, n) {
a[i] = lower_bound(ALL(t), a[i]) - t.begin() + 1;
b[i] = lower_bound(ALL(t), b[i]) - t.begin() + 1;
}
fenwick T1(t.size()+1);
fenwick T2(t.size()+2);
int res = 0;
FOR(i, 1, n) {
int r1 = T1.get(a[i]-1) + 1;
int r2 = max(T1.get(b[i]-1), T2.get(a[i]-1)) + 1;
T1.update(a[i], r1); T2.update(a[i], r2);
res = max(res, max(r1, r2));
// cerr << r1 << ' ' << r2 << '\n';
}
cout << res;
return 0;
}
/*
*/
Compilation message
glo.cpp: In constructor 'fenwick::fenwick(int)':
glo.cpp:19:9: warning: 'fenwick::sz' will be initialized after [-Wreorder]
19 | int sz;
| ^~
glo.cpp:18:17: warning: 'std::vector<int> fenwick::bit' [-Wreorder]
18 | vector<int> bit;
| ^~~
glo.cpp:20:5: warning: when initialized here [-Wreorder]
20 | fenwick(int t) : sz(t), bit(t+1, 0) {}
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
99 ms |
6008 KB |
Output is correct |
2 |
Correct |
97 ms |
6924 KB |
Output is correct |
3 |
Correct |
95 ms |
6916 KB |
Output is correct |
4 |
Correct |
103 ms |
6920 KB |
Output is correct |
5 |
Correct |
46 ms |
5392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
2444 KB |
Output is correct |
2 |
Correct |
23 ms |
2516 KB |
Output is correct |
3 |
Correct |
23 ms |
2440 KB |
Output is correct |
4 |
Correct |
12 ms |
1820 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
1752 KB |
Output is correct |
7 |
Correct |
20 ms |
2420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
4524 KB |
Output is correct |
2 |
Correct |
51 ms |
4568 KB |
Output is correct |
3 |
Correct |
124 ms |
8584 KB |
Output is correct |
4 |
Correct |
57 ms |
6152 KB |
Output is correct |
5 |
Correct |
39 ms |
4184 KB |
Output is correct |
6 |
Correct |
53 ms |
7584 KB |
Output is correct |
7 |
Correct |
57 ms |
8260 KB |
Output is correct |
8 |
Correct |
41 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
99 ms |
6008 KB |
Output is correct |
28 |
Correct |
97 ms |
6924 KB |
Output is correct |
29 |
Correct |
95 ms |
6916 KB |
Output is correct |
30 |
Correct |
103 ms |
6920 KB |
Output is correct |
31 |
Correct |
46 ms |
5392 KB |
Output is correct |
32 |
Correct |
23 ms |
2444 KB |
Output is correct |
33 |
Correct |
23 ms |
2516 KB |
Output is correct |
34 |
Correct |
23 ms |
2440 KB |
Output is correct |
35 |
Correct |
12 ms |
1820 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
11 ms |
1752 KB |
Output is correct |
38 |
Correct |
20 ms |
2420 KB |
Output is correct |
39 |
Correct |
51 ms |
4524 KB |
Output is correct |
40 |
Correct |
51 ms |
4568 KB |
Output is correct |
41 |
Correct |
124 ms |
8584 KB |
Output is correct |
42 |
Correct |
57 ms |
6152 KB |
Output is correct |
43 |
Correct |
39 ms |
4184 KB |
Output is correct |
44 |
Correct |
53 ms |
7584 KB |
Output is correct |
45 |
Correct |
57 ms |
8260 KB |
Output is correct |
46 |
Correct |
41 ms |
4568 KB |
Output is correct |
47 |
Correct |
51 ms |
4564 KB |
Output is correct |
48 |
Correct |
55 ms |
4532 KB |
Output is correct |
49 |
Correct |
105 ms |
8584 KB |
Output is correct |
50 |
Correct |
50 ms |
6160 KB |
Output is correct |
51 |
Correct |
41 ms |
4712 KB |
Output is correct |
52 |
Correct |
55 ms |
6256 KB |
Output is correct |
53 |
Correct |
55 ms |
7916 KB |
Output is correct |
54 |
Correct |
64 ms |
8688 KB |
Output is correct |
55 |
Correct |
87 ms |
8160 KB |
Output is correct |