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>
using namespace std;
using pii = pair<int, int>;
const int N = 3e5, N_ = 1 << 19;
int n, d, a[N];
int order[N];
int dp[N];
pii seg[N_ * 2];
int big[N_ * 2];
void bld(int k, int l, int r) {
big[k] = 0;
seg[k] = {-1, -1};
if (l < r) {
int m = (l + r) / 2;
bld(k * 2, l, m), bld(k * 2 + 1, m + 1, r);
}
}
pii operator+(const pii one, const pii two) {
if (one == pii{-2, -2} || two == pii{-2, -2}) return {-2, -2};
if (one == pii{-1, -1}) return two;
if (two == pii{-1, -1}) return one;
if (two.first - one.second > d) return {-2, -2};
return {one.first, two.second};
}
void upd(int k, int l, int r, int i) {
if (l == r) {
big[k] = dp[i];
seg[k] = {i, i};
} else {
int m = (l + r) / 2;
i <= m ? upd(k * 2, l, m, i) : upd(k * 2 + 1, m + 1, r, i);
big[k] = max(big[k * 2], big[k * 2 + 1]);
seg[k] = seg[k * 2] + seg[k * 2 + 1];
}
}
bool pos(int k, int l, int r, int ql, int qr, pii &v) {
if (ql > r || qr < l) return 0;
if (ql <= l && qr >= r) {
if ((seg[k] + v) != pii{-2, -2}) {
v = seg[k] + v;
return 0;
}
if (l == r) return 1;
int m = (l + r) / 2;
if ((seg[k * 2 + 1] + v) != pii{-2, -2}) {
v = seg[k * 2 + 1] + v;
return pos(k * 2, l, m, ql, qr, v);
}
return pos(k * 2 + 1, m + 1, r, ql, qr, v);
}
int m = (l + r) / 2;
return pos(k * 2 + 1, m + 1, r, ql, qr, v) ? 1 : pos(k * 2, l, m, ql, qr, v);
}
int qry(int k, int l, int r, int ql, int qr) {
if (ql > r || qr < l) return 0;
if (ql <= l && qr >= r) return big[k];
int m = (l + r) / 2;
return max(qry(k * 2, l, m, ql, qr), qry(k * 2 + 1, m + 1, r, ql, qr));
}
void calc_dp(int i) {
pii v = {-1, -1};
pos(1, 0, n - 1, 0, i, v);
// cout << i << " => " << v.first << ' ' << v.second << endl;
dp[i] = 1 + [&]() {
if (v == pii{-1, -1}) return 0;
if (i - v.second > d) return 0;
int l = max(0, v.first - d);
return qry(1, 0, n - 1, l, i);
}();
upd(1, 0, n - 1, i);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> d;
for (int i = 0; i < n; i++) cin >> a[i];
iota(order, order + n, 0);
sort(order, order + n, [&](int i, int j) {
if (a[i] == a[j]) return i > j;
return a[i] < a[j];
});
bld(1, 0, n - 1);
for (int i = 0; i < n; i++) calc_dp(order[i]);
cout << *max_element(dp, dp + n) << '\n';
}
# | 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... |