This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define UI unsigned int
#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef pair <ll, ll> pii;
const int N = 2e5 + 7;
const UI ofs = (1 << 31);
UI n, x, cnt=1, pos, xx;
UI a[N], dp[N], dp2[N];
struct node {
	UI lo, hi, le, ri, val;
} t[35*N];
void Update(int x = 1) {
	UI mid = ((ll)t[x].lo + (ll)t[x].hi) / 2;
	if (t[x].hi == t[x].lo) {
		t[x].val = max(t[x].val, xx);
		return;
	}
	if (pos <= mid) {
		if (!t[x].le) {
			t[x].le = ++cnt;
			t[cnt].lo = t[x].lo;
			t[cnt].hi = mid;
		}
		Update(t[x].le);
	}
	else {
		if (!t[x].ri) {
			t[x].ri = ++cnt;
			t[cnt].lo = mid+1;
			t[cnt].hi = t[x].hi;
		}
		Update(t[x].ri);
	}
	t[x].val = max(t[t[x].le].val, t[t[x].ri].val);
}
UI Query(int x = 1) {
	if (x == 0) return 0;
	if (t[x].hi < pos && xx < t[x].lo) return 0;
	if (pos <= t[x].lo && t[x].hi <= xx) return t[x].val;
	return max(Query(t[x].le), Query(t[x].ri));
}
int main () {
	FIO;
	cin >> n >> x;
	
	t[1].lo = 0;
	t[1].hi = ofs-1;
	
	for (UI i = 1; i <= n; i++) {
		cin >> a[i];
		pos = 1; xx = a[i]-1;
		dp[i] = Query() + 1;
		pos = a[i];
		xx = dp[i];
		Update();
	}
	
	memset(t, 0, sizeof t);
	
	t[1].lo = 0;
	t[1].hi = ofs-1;
	cnt = 1;
	
	UI mx = 0;
	for (int i = n; i; i--) {
		pos = a[i]+x+1; xx = ofs-1;
		dp2[i] = Query() + 1;
		pos = a[i]+1;
		mx = max(mx, dp[i] + Query());
		pos = a[i]+x;
		xx = dp2[i];
		Update();
	}
	
	cout << mx;
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |