#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
struct chash {
const uint64_t C = uint64_t(6283185307179586476) + 71;
const uint32_t RANDOM = chrono::steady_clock::now().time_since_epoch().count();
size_t operator()(uint64_t x) const {
return __builtin_bswap64((x ^ RANDOM) * C);
}
};
template <class K, class V> using ht = gp_hash_table
<K, V, /*hash<K> for undefined chash*/ chash,
equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>,
hash_standard_resize_policy<hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>, true>>;
template <class T> class segtree {
private:
const T DEFAULT = 0;
vector<T> tree;
int n;
public:
segtree(int n) : n(n), tree(n * 2, DEFAULT) {}
void reset(){
tree = vector<int> (2 * n, DEFAULT);
}
void update(int k, T val) {
k += n;
tree[k] = val;
for (k /= 2; k >= 1; k /= 2) {
tree[k] = max(tree[2 * k], tree[2 * k + 1]);
}
}
T query(int a, int b) {
T s = DEFAULT;
for (a += n, b += n; a <= b; a /= 2, b /= 2) {
if (a % 2 == 1) { s = max(s, tree[a++]); }
if (b % 2 == 0) { s = max(s, tree[b--]); }
}
return s;
}
};
const int MAXN = 2e5 + 5;
int n, d;
int v[MAXN];
int ps[MAXN];
int ss[MAXN];
vector<int> cc;
ht<int, int> mp;
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> d;
for(int i = 0; i < n; i++){
cin >> v[i];
cc.pb(v[i]);
cc.pb(v[i] - d);
cc.pb(v[i] + d);
}
sort(all(cc));
cc.erase(unique(all(cc)), cc.end());
for(int i = 0; i < sz(cc); i++){
mp[cc[i]] = i;
}
segtree<int> tree(sz(cc) + 2);
for(int i = 0; i < n; i++){
int curr = mp[v[i]];
ps[i] = tree.query(0, curr - 1) + 1;
if(tree.query(curr, curr) < ps[i]) {
tree.update(curr, ps[i]);
}
}
tree.reset();
for(int i = n - 1; i >= 0; i--){
int curr = mp[v[i]];
ss[i] = tree.query(curr + 1, sz(cc) - 1) + 1;
if(tree.query(curr, curr) < ss[i]){
tree.update(curr, ss[i]);
}
}
tree.reset();
int ans = 0;
for(int i = n - 1; i >= 0; i--){
ans = max(ans, ps[i] + tree.query(mp[v[i] - d] + 1, sz(cc) - 1));
if(tree.query(mp[v[i]], mp[v[i]]) < ss[i]){
tree.update(mp[v[i]], ss[i]);
}
}
cout << ans << '\n';
// for(int i = 0; i < n; i++){
// cout << ss[i] << ' ';
// }
return 0;
}
Compilation message
glo.cpp: In instantiation of 'segtree<T>::segtree(int) [with T = int]':
glo.cpp:78:33: required from here
glo.cpp:28:9: warning: 'segtree<int>::n' will be initialized after [-Wreorder]
28 | int n;
| ^
glo.cpp:27:15: warning: 'std::vector<int> segtree<int>::tree' [-Wreorder]
27 | vector<T> tree;
| ^~~~
glo.cpp:31:5: warning: when initialized here [-Wreorder]
31 | segtree(int n) : n(n), tree(n * 2, DEFAULT) {}
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
17684 KB |
Output is correct |
2 |
Correct |
167 ms |
17596 KB |
Output is correct |
3 |
Correct |
162 ms |
17600 KB |
Output is correct |
4 |
Correct |
162 ms |
17600 KB |
Output is correct |
5 |
Correct |
88 ms |
12224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
11728 KB |
Output is correct |
2 |
Correct |
52 ms |
11724 KB |
Output is correct |
3 |
Correct |
53 ms |
11608 KB |
Output is correct |
4 |
Correct |
27 ms |
6868 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
25 ms |
4568 KB |
Output is correct |
7 |
Correct |
41 ms |
7124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
22992 KB |
Output is correct |
2 |
Correct |
102 ms |
22988 KB |
Output is correct |
3 |
Correct |
244 ms |
45492 KB |
Output is correct |
4 |
Correct |
130 ms |
26300 KB |
Output is correct |
5 |
Correct |
88 ms |
22464 KB |
Output is correct |
6 |
Correct |
175 ms |
44468 KB |
Output is correct |
7 |
Correct |
177 ms |
45236 KB |
Output is correct |
8 |
Correct |
98 ms |
22976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
171 ms |
17684 KB |
Output is correct |
28 |
Correct |
167 ms |
17596 KB |
Output is correct |
29 |
Correct |
162 ms |
17600 KB |
Output is correct |
30 |
Correct |
162 ms |
17600 KB |
Output is correct |
31 |
Correct |
88 ms |
12224 KB |
Output is correct |
32 |
Correct |
49 ms |
11728 KB |
Output is correct |
33 |
Correct |
52 ms |
11724 KB |
Output is correct |
34 |
Correct |
53 ms |
11608 KB |
Output is correct |
35 |
Correct |
27 ms |
6868 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
25 ms |
4568 KB |
Output is correct |
38 |
Correct |
41 ms |
7124 KB |
Output is correct |
39 |
Correct |
108 ms |
22992 KB |
Output is correct |
40 |
Correct |
102 ms |
22988 KB |
Output is correct |
41 |
Correct |
244 ms |
45492 KB |
Output is correct |
42 |
Correct |
130 ms |
26300 KB |
Output is correct |
43 |
Correct |
88 ms |
22464 KB |
Output is correct |
44 |
Correct |
175 ms |
44468 KB |
Output is correct |
45 |
Correct |
177 ms |
45236 KB |
Output is correct |
46 |
Correct |
98 ms |
22976 KB |
Output is correct |
47 |
Correct |
109 ms |
22980 KB |
Output is correct |
48 |
Correct |
106 ms |
22836 KB |
Output is correct |
49 |
Correct |
247 ms |
45492 KB |
Output is correct |
50 |
Correct |
121 ms |
26284 KB |
Output is correct |
51 |
Correct |
93 ms |
14272 KB |
Output is correct |
52 |
Correct |
126 ms |
17088 KB |
Output is correct |
53 |
Correct |
206 ms |
44860 KB |
Output is correct |
54 |
Correct |
207 ms |
45496 KB |
Output is correct |
55 |
Correct |
182 ms |
27072 KB |
Output is correct |