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;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
ll a[(int) 3e5 + 10];
struct SegmentTree {
vector<ll> sgt;
SegmentTree(int n) {
sgt.resize(n);
for (int i = 0; i < n; i++) sgt[i] = LLONG_MAX;
}
ll get(int k, int l, int r, ll v) {
if (l == r) return l;
int m = (l + r) / 2;
if (sgt[k * 2 + 1] < v) return get(k * 2 + 1, m + 1, r, v);
return get(k * 2, l, m, v);
}
pair<ll, ll> dif(int k, int l, int r, ll v) {
if (l == r) return {(sgt[k * 2 + 1] >= v && sgt[k * 2 + 1] != LLONG_MAX) * l, sgt[k * 2 + 1]};
int m = (l + r) / 2;
if (sgt[k * 2 + 1] >= v && sgt[k * 2 + 1] != LLONG_MAX) return dif(k * 2 + 1, m + 1, r, v);
return dif(k * 2, l, m, v);
}
void update(int k, int l, int r, int i, ll v) {
if (l > i || r < i) return;
if (l == r) {
sgt[k] = v;
return;
}
int m = (l + r) / 2;
update(k * 2, l, m, i, v);
update(k * 2 + 1, m + 1, r, i, v);
sgt[k] = min(sgt[k * 2], sgt[k * 2 + 1]);
}
} sgt(4 * (3e5 + 10));
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 0;
multiset<ll> lis[n + 10];
lis[0].insert(0);
sgt.update(1, 0, n, 0, 0);
ll dp[n + 10];
dp[0] = 0;
for (int i = 1; i <= n; i++) {
ll mn;
if (i > d + 1) {
mn = *lis[dp[i - d - 1]].begin();
lis[dp[i - d - 1]].erase(lis[dp[i - d - 1]].find(a[i - d - 1]));
if (lis[dp[i - d - 1]].size()) {
if (*lis[dp[i - d - 1]].begin() != mn) sgt.update(1, 0, n, dp[i - d - 1], *lis[dp[i - d - 1]].begin());
}
else sgt.update(1, 0, n, dp[i - d - 1], LLONG_MAX);
}
ll ans1 = sgt.get(1, 0, n, a[i]) + 1;
pair<ll, ll> ans2 = sgt.dif(1, 0, n, a[i]);
dp[i] = max(ans1, ans2.first);
if (ans2.first > ans1) a[i] = ans2.second;
if (ans2.first == ans1) a[i] = min(a[i], ans2.second);
if (lis[dp[i]].size()) mn = *lis[dp[i]].begin();
lis[dp[i]].insert(a[i]);
if (lis[dp[i]].size() == 1 || a[i] < mn) sgt.update(1, 0, n, dp[i], a[i]);
ans = max(ans, dp[i]);
// cout << i << " : " << dp[i] << endl;
}
cout << ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:75:36: warning: 'mn' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | if (lis[dp[i]].size() == 1 || a[i] < mn) sgt.update(1, 0, n, dp[i], a[i]);
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# | 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... |