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 ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
#define fr first
#define sc second
#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int nmax = 300005;
int n, a[nmax], d, tree[4*nmax], tree2[4*nmax], lazy[4*nmax];
vector<int>vecc;
void build(int l, int r, int nod){
if(l == r){
tree[nod] = a[l];
}
else{
int mid = l + (r - l) / 2;
build(l, mid, 2*nod);
build(mid + 1, r, 2*nod+1);
tree[nod] = min(tree[2*nod], tree[2*nod+1]);
}
}
int query1(int l, int r, int le, int ri, int nod){
if(l > ri || r < le) return 1e18;
else if(l >= le && r <= ri) return tree[nod];
else{
int mid = l + (r - l) / 2;
return min(query1(l, mid, le, ri, 2*nod), query1(mid + 1, r, le, ri, 2*nod + 1));
}
}
void push(int l, int r, int nod){
if(lazy[nod] == 1){
tree2[nod] = 0;
if(l != r){
lazy[2*nod] = 1;
lazy[2*nod+1] = 1;
}
lazy[nod] = 0;
}
}
void update2(int l, int r, int le, int ri, int nod){
push(l, r, nod);
if(r < le || l > ri) return;
else if(l >= le && r <= ri){
lazy[nod] = 1;
push(l, r, nod);
}
else{
int mid = l + (r - l) / 2;
update2(l, mid, le, ri, 2*nod);
update2(mid+1, r, le, ri, 2*nod + 1);
tree2[nod] = max(tree2[2*nod], tree2[2*nod + 1]);
}
}
int query2(int l, int r, int le, int ri, int nod){
push(l, r, nod);
if(l > ri || r < le){
return 0;
}
else if(l >= le && r <= ri){
return tree2[nod];
}
else{
int mid = l + (r - l) / 2;
return max(query2(l, mid, le, ri, 2*nod), query2(mid+1, r, le, ri, 2*nod +1));
}
}
void update3(int l, int r, int pos, int val, int nod){
push(l, r, nod);
if(l == r){
tree2[nod] = max(tree2[nod], val);
}
else{
int mid = l + (r - l) / 2;
tree2[nod] = max(tree2[nod], val);
if(pos <= mid){
update3(l, mid, pos, val, 2*nod);
}
else{
update3(mid + 1, r, pos, val, 2*nod + 1);
}
}
}
int32_t main(){
//ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
// freopen("input.in", "r", stdin);
cin >> n >> d;
for(int i=1;i<=n;i++){
cin >> a[i];
vecc.push_back(a[i]);
}
sort(all(vecc));
vecc.resize(unique(all(vecc)) - vecc.begin());
for(int i=1;i<=n;i++) a[i] = lower_bound(all(vecc), a[i]) - vecc.begin() + 1LL;
build(1, n, 1);
int nn = (int)vecc.size();
for(int i=1;i<=n;i++){
if(i >= 2){
int curr = query1(1, n, max(1LL, i - d), i - 1, 1);
if(curr >= 2LL) update2(1, nn, 1, curr - 1LL, 1);
}
int val = 1LL;
if(a[i] >= 2LL){
val += query2(1, nn, 1, a[i] - 1LL, 1);
}
update3(1, nn, a[i], val, 1);
}
cout << tree2[1];
}
// 1. Solve the problem
// 2. ???
// 3. Profit
# | 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... |