#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int n, x, a[N];
int nNode;
int ver[N];
struct node
{
int left, right;
int ln;
node () {}
node(int ln, int left, int right) : ln(ln), left(left), right(right) {}
} it[20000000], it1[20000000];
inline void refine(int cur) {
it[cur].ln = max(it[it[cur].left].ln, it[it[cur].right].ln);
}
int update(int oldId, int l, int r, int u, int val)
{
if(l == r)
{
++nNode;
it[nNode] = node(max(it[oldId].ln, val), 0, 0);
return nNode;
}
int mid = (l + r) >> 1;
int cur = ++nNode;
if(u <= mid)
{
it[cur].left = update(it[oldId].left, l, mid, u, val);
it[cur].right = it[oldId].right;
}
else
{
it[cur].right = update(it[oldId].right, mid + 1, r, u, val);
it[cur].left = it[oldId].left;
}
refine(cur);
return cur;
}
int get(int nodeId, int l, int r, int u, int v)
{
if (v < l || r < u)
return -1;
if (u <= l && r <= v)
return it[nodeId].ln;
int mid = (l + r) >> 1;
return max(get(it[nodeId].left, l, mid, u, v),
get(it[nodeId].right, mid + 1, r, u, v));
}
inline void refine1(int cur) {
it1[cur].ln = max(it1[it1[cur].left].ln, it1[it1[cur].right].ln);
}
int update1(int oldId, int l, int r, int u, int val)
{
if(l == r)
{
++nNode;
it1[nNode] = node(max(it1[oldId].ln, val), 0, 0);
return nNode;
}
int mid = (l + r) >> 1;
int cur = ++nNode;
if(u <= mid)
{
it1[cur].left = update(it1[oldId].left, l, mid, u, val);
it1[cur].right = it1[oldId].right;
}
else
{
it1[cur].right = update(it1[oldId].right, mid + 1, r, u, val);
it1[cur].left = it1[oldId].left;
}
refine1(cur);
return cur;
}
int get1(int nodeId, int l, int r, int u, int v)
{
if (v < l || r < u)
return -1;
if (u <= l && r <= v)
return it1[nodeId].ln;
int mid = (l + r) >> 1;
return max(get(it1[nodeId].left, l, mid, u, v),
get(it1[nodeId].right, mid + 1, r, u, v));
}
int st[12 * N];
void build(int id, int l, int r)
{
if(l == r)
{
st[id] = 0;
return;
}
int mid = (l + r) >> 1;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id] = 0;
}
void upd(int id, int l, int r, int u, int v)
{
if(l == r)
{
st[id] = max(st[id], v);
return;
}
int mid = (l + r) >> 1;
if(u <= mid)
upd(id * 2, l, mid, u, v);
else
upd(id * 2 + 1, mid + 1, r, u, v);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int gett(int nodeId, int l, int r, int u, int v)
{
if (v < l || r < u)
return -1;
if (u <= l && r <= v)
return st[nodeId];
int mid = (l + r) >> 1;
return max(gett(nodeId * 2, l, mid, u, v),
gett(nodeId * 2 + 1, mid + 1, r, u, v));
}
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> x;
vector<int> nen;
nen.push_back(-1e9);
nen.push_back(1e9);
for(int i = 1; i <= n; i++)
{
cin >> a[i];
nen.push_back(a[i]);
nen.push_back(a[i] + x);
nen.push_back(a[i] - x);
}
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()), nen.end());
int m = nen.size();
for(int i = n; i >= 1; i--)
{
int l = upper_bound(nen.begin(), nen.end(), a[i]) - nen.begin();
int tmp = get(ver[i + 1], 1, m, l + 1, m);
// cout << tmp << '\n';
ver[i] = update(ver[i + 1], 1, m, l, tmp + 1);
// cout << i << " " << l << " " << tmp + 1 << '\n';
}
int ans = it[ver[1]].ln;
for(int i = 1; i <= n; i++)
{
int r = upper_bound(nen.begin(), nen.end(), a[i]) - nen.begin(), l = upper_bound(nen.begin(), nen.end(), a[i] - x) - nen.begin();
int tmp = gett(1, 1, m, 1, r - 1);
// cout << tmp << " " << get(ver[i + 1], 1, m, l + 1, m) << " " << l << " " << m << "huhu\n";
ans = max(ans, tmp + 1 + get(ver[i + 1], 1, m, l + 1, m));
upd(1, 1, m, r, tmp + 1);
}
build(1, 1, m);
memset(ver, 0, sizeof ver);
for(int i = 1; i <= n; i++)
{
int l = upper_bound(nen.begin(), nen.end(), a[i]) - nen.begin();
int tmp = get1(ver[i - 1], 1, m, 1, l - 1);
// cout << tmp << '\n';
ver[i] = update1(ver[i - 1], 1, m, l, tmp + 1);
// cout << i << " " << l << " " << tmp + 1 << '\n';
}
for(int i = n; i >= 1; i--)
{
int l = upper_bound(nen.begin(), nen.end(), a[i]) - nen.begin(), r = upper_bound(nen.begin(), nen.end(), a[i] + x) - nen.begin();
int tmp = gett(1, 1, m, l + 1, m);
// cout << tmp << " " << get(ver[i + 1], 1, m, l + 1, m) << " " << l << " " << m << "huhu\n";
ans = max(ans, tmp + 1 + get1(ver[i - 1], 1, m, 1, r - 1));
upd(1, 1, m, l, tmp + 1);
}
cout << ans;
return 0;
}
Compilation message
glo.cpp: In constructor 'node::node(int, int, int)':
glo.cpp:14:9: warning: 'node::ln' will be initialized after [-Wreorder]
14 | int ln;
| ^~
glo.cpp:13:9: warning: 'int node::left' [-Wreorder]
13 | int left, right;
| ^~~~
glo.cpp:16:5: warning: when initialized here [-Wreorder]
16 | node(int ln, int left, int right) : ln(ln), left(left), right(right) {}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4752 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4852 KB |
Output is correct |
24 |
Correct |
2 ms |
4828 KB |
Output is correct |
25 |
Correct |
2 ms |
4700 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
672 ms |
142688 KB |
Output is correct |
2 |
Correct |
644 ms |
143552 KB |
Output is correct |
3 |
Correct |
636 ms |
143040 KB |
Output is correct |
4 |
Correct |
660 ms |
143804 KB |
Output is correct |
5 |
Correct |
300 ms |
136684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
40756 KB |
Output is correct |
2 |
Correct |
128 ms |
40740 KB |
Output is correct |
3 |
Correct |
165 ms |
40752 KB |
Output is correct |
4 |
Correct |
72 ms |
37500 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
65 ms |
34952 KB |
Output is correct |
7 |
Correct |
114 ms |
36152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
278 ms |
77752 KB |
Output is correct |
2 |
Correct |
273 ms |
77740 KB |
Output is correct |
3 |
Correct |
647 ms |
159680 KB |
Output is correct |
4 |
Correct |
308 ms |
147900 KB |
Output is correct |
5 |
Correct |
189 ms |
76152 KB |
Output is correct |
6 |
Correct |
325 ms |
152004 KB |
Output is correct |
7 |
Correct |
328 ms |
151840 KB |
Output is correct |
8 |
Correct |
242 ms |
77736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4752 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4852 KB |
Output is correct |
24 |
Correct |
2 ms |
4828 KB |
Output is correct |
25 |
Correct |
2 ms |
4700 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
672 ms |
142688 KB |
Output is correct |
28 |
Correct |
644 ms |
143552 KB |
Output is correct |
29 |
Correct |
636 ms |
143040 KB |
Output is correct |
30 |
Correct |
660 ms |
143804 KB |
Output is correct |
31 |
Correct |
300 ms |
136684 KB |
Output is correct |
32 |
Correct |
127 ms |
40756 KB |
Output is correct |
33 |
Correct |
128 ms |
40740 KB |
Output is correct |
34 |
Correct |
165 ms |
40752 KB |
Output is correct |
35 |
Correct |
72 ms |
37500 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
65 ms |
34952 KB |
Output is correct |
38 |
Correct |
114 ms |
36152 KB |
Output is correct |
39 |
Correct |
278 ms |
77752 KB |
Output is correct |
40 |
Correct |
273 ms |
77740 KB |
Output is correct |
41 |
Correct |
647 ms |
159680 KB |
Output is correct |
42 |
Correct |
308 ms |
147900 KB |
Output is correct |
43 |
Correct |
189 ms |
76152 KB |
Output is correct |
44 |
Correct |
325 ms |
152004 KB |
Output is correct |
45 |
Correct |
328 ms |
151840 KB |
Output is correct |
46 |
Correct |
242 ms |
77736 KB |
Output is correct |
47 |
Correct |
314 ms |
77640 KB |
Output is correct |
48 |
Correct |
331 ms |
77752 KB |
Output is correct |
49 |
Correct |
805 ms |
158540 KB |
Output is correct |
50 |
Correct |
377 ms |
147652 KB |
Output is correct |
51 |
Correct |
246 ms |
105972 KB |
Output is correct |
52 |
Correct |
346 ms |
142024 KB |
Output is correct |
53 |
Correct |
375 ms |
157372 KB |
Output is correct |
54 |
Correct |
376 ms |
158144 KB |
Output is correct |
55 |
Correct |
549 ms |
154052 KB |
Output is correct |