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;
struct tree {
typedef int T;
static constexpr T unit = 0;
T f(T a, T b) { return max(a, b); } // (any associative fn)
vector<T> s;
int n;
tree(int nn = 0, T def = unit) : s(2 * nn, def), n(nn) {}
void update(int pos, T val)
{
for(s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e)
{
T ra = unit, rb = unit;
for(b += n, e += n + 1; b < e; b /= 2, e /= 2) {
if(b % 2) ra = f(ra, s[b++]);
if(e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n, d;
cin >> n >> d;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> par(n + 1), mini(n + 1);
for(int i = 1; i <= n; i++) {
par[i] = mini[i] = i;
}
function<int(int)> f = [&](int u) {
return u == par[u] ? u : par[u] = f(par[u]);
};
auto u = [&](int x, int y) {
x = f(x);
y = f(y);
mini[x] = min(mini[x], mini[y]);
par[y] = x;
};
vector<int> ord(n);
for(int i = 0; i < n; i++) {
ord[i] = i + 1;
}
sort(ord.begin(), ord.end(), [&](int i, int j) {
return a[i] == a[j] ? i > j : a[i] < a[j];
});
set<int> alive;
tree Tree(n + 1);
for(int i : ord) {
auto it = alive.upper_bound(i);
if(it != alive.end()) {
if(*it - i <= d) {
u(i, *it);
}
}
if(it != alive.begin()) {
if(i - *(--it) <= d) {
u(i, *it);
}
}
int dp = Tree.query(mini[f(i)], i) + 1;
Tree.update(i, dp);
alive.emplace(i);
}
cout << Tree.query(1, 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... |