제출 #766531

#제출 시각아이디문제언어결과실행 시간메모리
766531TobGlobal Warming (CEOI18_glo)C++14
100 / 100
447 ms141536 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")

#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...