Submission #1309150

#TimeUsernameProblemLanguageResultExecution timeMemory
1309150ppmn_6Global Warming (CEOI18_glo)C++20
27 / 100
203 ms7984 KiB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
 
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
// https://codeforces.com/blog/entry/79148
class Timer: chrono::high_resolution_clock {
    const time_point start_time;
public:
    Timer(): start_time(now()) {}
    rep elapsed_time() const {
		return chrono::duration_cast<chrono::milliseconds>(now() - start_time).count();
	}
} timer;

struct SegmentTree {
	struct Vertex {
		int ma;
		Vertex() {}
		Vertex(int x) {
			ma = x;
		}
		Vertex(Vertex l, Vertex r) {
			ma = max(l.ma, r.ma);
		}
	};
	int n;
	vector<Vertex> tree;
	SegmentTree(vector<int> v) {
		n = v.size();
		tree.resize(4 * n);
		auto build = [&] (auto self, int p, int rl, int rr)->void {
			if (rl == rr) {
				tree[p] = Vertex(v[rl]);
			}
			else {
				int rm = (rl + rr) / 2;
				self(self, p * 2, rl, rm);
				self(self, p * 2 + 1, rm + 1, rr);
				tree[p] = Vertex(tree[p * 2], tree[p * 2 + 1]);
			}
		};
		build(build, 1, 0, n - 1);
	}
	void update(int p, int rl, int rr, int pos, int x) {
		if (rl == rr) {
			tree[p].ma = max(tree[p].ma, x);
		}
		else {
			int rm = (rl + rr) / 2;
			if (pos <= rm) {
				update(p * 2, rl, rm, pos, x);
			}
			else {
				update(p * 2 + 1, rm + 1, rr, pos, x);
			}
			tree[p] = Vertex(tree[p * 2], tree[p * 2 + 1]);
		}
	}
	int query(int p, int rl, int rr, int l, int r) {
		if (r < l) {
			return 0;
		}
		if (l == rl && r == rr) {
			return tree[p].ma;
		}
		int rm = (rl + rr) / 2, res = 0;
		if (l <= rm) {
			res = max(res, query(p * 2, rl, rm, l, min(r, rm)));
		}
		if (r > rm) {
			res = max(res, query(p * 2 + 1, rm + 1, rr, max(l, rm + 1), r));
		}
		return res;
	}
};
 
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n, x;
    cin >> n >> x;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<int> b = a;
	b.push_back(0);
	sort(b.begin(), b.end());
	b.resize(unique(b.begin(), b.end()) - b.begin());
	SegmentTree dp0(vector<int>(b.size(), 0)), dp1 = dp0;
	for (int i = 0; i < n; i++) {
		int pos = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
		int res1 = dp0.query(1, 0, b.size() - 1, 0, pos - 1);
		dp0.update(1, 0, b.size() - 1, pos, res1 + 1);
		int pos2 = lower_bound(b.begin(), b.end(), a[i] + x) - b.begin() - 1;
		int res2 = dp0.query(1, 0, b.size() - 1, 0, pos2);
		dp1.update(1, 0, b.size() - 1, pos, res2 + 1);
		int res3 = dp1.query(1, 0, b.size() - 1, 0, pos - 1);
		dp1.update(1, 0, b.size() - 1, pos, res3 + 1);
	}
	cout << dp1.query(1, 0, b.size() - 1, 0, b.size() - 1);
	
    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...