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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
template<typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
class PointSegmentTree{
private:
int size_;
vector<ll> v;
void update(int p, ll val, int k, int l, int r)
{
if(p < l || r < p) return;
if(l == r){
v[k]=val; //modification
return;
}
int mid = (l+r)>>1;
update(p, val, k*2, l, mid);
update(p, val, k*2+1, mid+1, r);
v[k] = merge(v[k*2], v[k*2+1]);
}
ll query(int s, int e, int k, int l, int r)
{
if(e < l || r < s) return 0; //dummy value
if(s <= l && r <= e) return v[k];
int mid = (l+r)>>1;
ll lc = query(s, e, k*2, l, mid);
ll rc = query(s, e, k*2+1, mid+1, r);
return merge(lc, rc);
}
public:
PointSegmentTree(): v(vector<ll>()) {}
PointSegmentTree(int n){
for(size_=1;size_<n;) size_<<=1;
v.resize(size_*4);
}
//void reset(){}
inline ll merge(ll x, ll y){
return max(x,y);
}
inline void update(int p, ll val){
update(p, val, 1, 0, size_-1);
}
inline ll query(int l, int r){
return query(l, r, 1, 0, size_-1);
}
};
struct DSU {
struct Node{ int p, sz, l; };
vector<Node> dsu; int cc;
Node& operator[](int id){ return dsu[rt(id)]; }
DSU(int n){ dsu.resize(n);
forn(i,0,n){ cc=n; dsu[i]={i,1,i}; }
}
inline int rt(int u){ return (dsu[u].p==u) ? u : dsu[u].p=rt(dsu[u].p); }
inline bool sameset(int u, int v){ return rt(u)==rt(v); }
void merge(int u, int v){
u = rt(u); v = rt(v);
if(u == v) return;
if(dsu[u].sz < dsu[v].sz) swap(u,v);
dsu[v].p = u;
dsu[u].sz += dsu[v].sz;
dsu[u].l = min(dsu[u].l, dsu[v].l);
cc--;
}
inline Node& get(int u){ return dsu[rt(u)]; }
};
const bool DEBUG = 0;
const int MAXN = 300005;
int n,D;
int a[MAXN];
vii eve;
PointSegmentTree st;
DSU dsu(0);
bool vst[MAXN];
set<int> ban;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>D;
forn(i,0,n)
{
cin>>a[i];
eve.pb({a[i],i});
}
auto cmp=[&](ii x, ii y){ return ii(-x.F, x.S)<ii(-y.F, y.S); };
sort(eve.begin(), eve.end(), cmp);
st=PointSegmentTree(n);
dsu=DSU(n);
for(ii tmp: eve)
{
int i=tmp.S;
auto rp=ban.lower_bound(i);
int r;
if(rp==ban.end()) r=n;
else r=*rp+D;
st.update(i, 1+st.query(i+1, r-1));
//cout<<"st("<<i+1<<")="<<st.query(i,i)<<" r="<<r<<'\n';
//cout<<"ban: "; for(int x: ban) cout<<x+1<<" "; cout<<'\n';
if(i && vst[i-1]) dsu.merge(i,i-1);
if(i<n-1 && vst[i+1]) dsu.merge(i,i+1);
if(dsu.get(i).sz>=D) ban.insert(dsu.get(i).l);
vst[i]=1;
}
cout<<st.query(0,n-1)<<'\n';
return 0;
}
# | 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... |