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 <random>
#include <chrono>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//typedef pair<double,double> pdd;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
const int MOD1=1e9+7;
const int MOD2=998244353;
const ll INF=3e18;
const ld PI=3.14159265358979323846;
ll gcd(ll a,ll b){if(b==0) return a; return gcd(b,a%b);}
ll fpow(ll a,ll b,ll m)
{
if(!b) return 1;
ll ans=fpow(a*a%m,b/2,m);
return (b%2?ans*a%m:ans);
}
ll inv(ll a,ll m) {return fpow(a,m-2,m);}
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
//#define int ll
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define repk(i,m,n) for(int i=m;i<n;i++)
#define F first
#define S second
#define pb push_back
#define uni(c) c.resize(distance(c.begin(),unique(c.begin(),c.end())))
#define unisort(c) sort(c.begin(),c.end()),uni(c)
const int N=3e5+5;
int a[N];
struct SegTree
{
int st[N<<2];
void upd(int l,int r,int x,int val,int id)
{
if(l==r) st[id]=val;
else
{
int mid=l+r>>1;
if(x<=mid) upd(l,mid,x,val,id<<1);
else upd(mid+1,r,x,val,id<<1|1);
st[id]=max(st[id<<1],st[id<<1|1]);
}
}
int qry(int l,int r,int ql,int qr,int id)
{
if(ql<=l&&r<=qr) return st[id];
else
{
int mid=l+r>>1;
if(qr<=mid) return qry(l,mid,ql,qr,id<<1);
else if(ql>mid) return qry(mid+1,r,ql,qr,id<<1|1);
else return max(qry(l,mid,ql,mid,id<<1),qry(mid+1,r,mid+1,qr,id<<1|1));
}
}
} seg;
struct DSU
{
int p[N];
void init(int n){rep1(i,n) p[i]=i;}
int fp(int x){return x==p[x]?x:p[x]=fp(p[x]);}
void unite(int u,int v)
{
int x=fp(u),y=fp(v);
if(x==y) return;
if(x>y) swap(x,y);
p[y]=x;
}
} dsu;
bool cmp(int i,int j)
{
if(a[i]!=a[j]) return a[i]<a[j];
return i>j;
}
set<int> s;
signed main()
{
MottoHayaku
int n,d,ans=1;
cin>>n>>d;
vector<int> v;
rep1(i,n)
{
cin>>a[i];
v.pb(i);
}
sort(v.begin(),v.end(),cmp);
dsu.init(n);
for(int i:v)
{
auto it=s.lower_bound(i);
if(it!=s.end())
{
int u=*it;
if(u-i<=d) dsu.unite(i,u);
}
if(it!=s.begin())
{
int u=*prev(it);
if(i-u<=d) dsu.unite(i,u);
}
s.insert(i);
int tmp=1;
if(dsu.fp(i)<=i-1) tmp=max(tmp,1+seg.qry(1,n,dsu.fp(i),i-1,1));
seg.upd(1,n,i,tmp,1);
ans=max(ans,tmp);
}
cout<<ans;
}
Compilation message (stderr)
Main.cpp: In member function 'void SegTree::upd(int, int, int, int, int)':
Main.cpp:53:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid=l+r>>1;
| ~^~
Main.cpp: In member function 'int SegTree::qry(int, int, int, int, int)':
Main.cpp:64:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid=l+r>>1;
| ~^~
# | 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... |