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>
#include<unordered_map>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long long inf = 1e15;
using namespace std;
vector<ll> p(1e6);
vector<ll> Ur(1e6);
ll UfindSet(ll cur) {
if (p[cur] == cur)return cur;
return p[cur] = UfindSet(p[cur]);
}
void Umerge(ll j, ll i) {
ll sj = UfindSet(j);
ll si = UfindSet(i);
if (si == sj)return;
if (Ur[sj] > Ur[si])swap(sj, si);
p[sj] = si;
Ur[si]++;
return;
}
struct tree {
typedef ll T;
static constexpr T unit = -inf;
T f(T a, T b) { return max(a, b); }
vector<T> s;
ll n;
tree(ll n=0, T def=unit) : n(n),s(2*n,def){}
void update(ll pos, T val) {
for (s[pos += n] = val; pos /= 2;) {
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
}
T query(ll l, ll r) {
T a = unit, b = unit;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l % 2)a = f(a, s[l++]);
if (r % 2)b = f(b, s[--r]);
}
return f(a, b);
}
};
int main() {
cin.sync_with_stdio(false);
int n, d;
cin >> n >> d;
vector<pair<ll, int>> v(n);
rep(i, 0, n) {
cin >> v[i].first;
v[i].second = -i;
}
sort(all(v));
vector<ll> dp(n, -inf);
rep(i, 0, 1e6)p[i] = i;
tree seg(n);
rep(i, 0, n) {
dp[-v[i].second] = 1;
ll cur = d;
int end = max(-1, -v[i].second - d-1 );
int start = -v[i].second;
ll l = 0, r = d;
while (l <= r) {
int mid = l + (r - l) / 2;
if (UfindSet(max(0, -v[i].second -mid)) == UfindSet(-v[i].second)) {
start = -v[i].second - mid;
r = mid - 1;
}
else l = mid + 1;
}
rrep(j, start, end) {
Umerge(end+1, j);
if (j && UfindSet(j) == UfindSet(j - 1))break;
}
ll left = -v[i].second - 1;
l = 0, r = max(0,-v[i].second - 1);
while (l <= r) {
ll mid = l + (r - l) / 2;
if (UfindSet(mid) == UfindSet(-v[i].second)) {
left = mid;
r = mid - 1;
}
else l = mid + 1;
}
ll FuckMinAndMax = 1;
dp[-v[i].second] = max(FuckMinAndMax, seg.query(left, -v[i].second)+1);
seg.update(-v[i].second, dp[-v[i].second]);
}
ll ans = 1;
trav(a, dp)ans = max(ans, a);
cout << ans << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In constructor 'tree::tree(ll, tree::T)':
Main.cpp:37:5: warning: 'tree::n' will be initialized after [-Wreorder]
37 | ll n;
| ^
Main.cpp:36:12: warning: 'std::vector<long long int> tree::s' [-Wreorder]
36 | vector<T> s;
| ^
Main.cpp:38:2: warning: when initialized here [-Wreorder]
38 | tree(ll n=0, T def=unit) : n(n),s(2*n,def){}
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:69:6: warning: unused variable 'cur' [-Wunused-variable]
69 | ll cur = d;
| ^~~
# | 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... |