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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<pii,pii>
#define vi vector<int>
#define pb push_back
//#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define int long long
const int mod=9901,mxn=3*1e5+5,inf=1e18,lg=25,minf=-1e18;
int n;
struct seg{
int v[2*mxn+10];
void update(int pos,int val){
pos+=n;
v[pos]=val;
for(;pos>0;pos>>=1)v[pos>>1]=max(v[pos],v[pos^1]);
}
int qry(int l,int r){
int ans=0;
for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
if(l&1)ans=max(ans,v[l++]);
if(!(r&1))ans=max(ans,v[r--]);
}
return ans;
}
}t[2];
set<int>s;
int32_t main(){
fastio
int d,l,r,pos,mid,ans,lastval;cin>>n>>d;
vector<pii>v(n);
for(int i=0;i<n;i++)cin>>v[i].f,v[i].s=i;
sort(all(v));
vector<pii>up;
for(int i=0;i<v.size();i++){
int cur=i;
up.clear();
while(cur<n&&v[i].f==v[cur].f){
l=0,r=v[cur].s;
pos=inf;
s.insert(v[cur].s);
auto it=s.lb(v[cur].s);
if(it!=s.begin()){
it--;
lastval=t[1].v[(*it)+n];
t[1].update((*it),v[cur].s-(*it));
it++;
}
while(l<=r){
mid=l+(r-l)/2;
if(t[1].qry(mid,v[cur].s)<=d)pos=min(pos,mid),r=mid-1;
else l=mid+1;
}
if(it!=s.begin())it--,t[1].update((*it),lastval),it++;
s.erase(it);
up.pb({v[cur].s,t[0].qry(pos,v[cur].s)+1});
cur++;
}
i=cur-1;
for(auto i:up){
ans=i.s;
s.insert(i.f);
auto it=s.lb(i.f);
if(it!=s.begin()){
it--;
ans=(i.f-(*it)<=d)?ans:1ll;
t[1].update((*it),i.f-(*it));
it++;
}
it++;
if(it!=s.end())t[1].update(i.f,(*it)-i.f);
t[0].update(i.f,ans);
}
}
cout<<t[0].qry(0,n);
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:58:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
Main.cpp:38:15: warning: 'lastval' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | v[pos]=val;
| ~~~~~~^~~~
Main.cpp:53:27: note: 'lastval' was declared here
53 | int d,l,r,pos,mid,ans,lastval;cin>>n>>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... |