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>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC target ("avx,avx2")
// cut here
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
segtree(int n) : segtree(std::vector<S>(n, e())) {}
segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
//assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) {
//assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) {
//assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() { return d[1]; }
template <bool (*f)(S)> int max_right(int l) {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) {
//assert(0 <= l && l <= _n);
//assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) {
//assert(0 <= r && r <= _n);
//assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
// cut here
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3D2ZDxo ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
void chmax(pii&a,pii b){
if(a.fi<b.fi){
a=b;
}else if(a.fi==b.fi){
a.se=min(a.se,b.se);
}
}
const int _n=8000011;
pii _seg[_n];
int lazy[_n];
bool flag[_n];
// struct lazy_segtree{
int n,en;
// lazy_segtree(int m){
// init(m);
// }
void init(int m){
n=m;
en=1;
while(en<=m){
en*=2;
}
en*=2;
}
void build(int node,int l,int r){
if(l==r-1){
_seg[node].fi=0;
_seg[node].se=l;
return;
}
int m=(l+r)>>1;
build(node*2,l,m);
build(node*2+1,m,r);
_seg[node].se=l;
}
void push(int l,int r,int node){
if(flag[node]){
_seg[node].fi+=lazy[node];
if(node*2<en){
lazy[node*2]+=lazy[node];
flag[node*2]=true;
}
if(node*2+1<en){
lazy[node*2+1]+=lazy[node];
flag[node*2+1]=true;
}
flag[node]=false;
lazy[node]=0;
}
}
void _set(int node,int l,int r,int x,int _l,int _r){
push(l,r,node);
if(l>=_r or r<=_l){
return;
}
if(l>=_l and r<=_r){
lazy[node]+=x;
flag[node]=true;
push(l,r,node);
return;
}
int m=(l+r)>>1;
_set(node*2,l,m,x,_l,_r);
_set(node*2+1,m,r,x,_l,_r);
// prod seg_node
_seg[node]={0,0};
chmax(_seg[node],_seg[node*2]);
chmax(_seg[node],_seg[node*2+1]);
}
void build(){
build(1,0,n);
}
void _set(int l,int r,int x){
// [l,r)
_set(1,0,n,x,l,r);
}
const int inf=1e9;
int op(int l,int r){return min(l,r);}
int e(){return inf;}
int tar;
bool f(int p){
return p>tar;
}
signed main(){
_3D2ZDxo;
int n,k,t;
cin>>n>>k>>t;
k=min(k,n);
vi a(n);
rep(i,n){
cin>>a[i];
}
vi dl(n,-1);
set<pii> st;
rep(i,n){
if(sz(st)){
auto it=st.lower_bound({i,-1});
if(it!=st.end()){
dl[i]=(*it).se;
}
}
int val=t-a[i]+i;
while(sz(st)){
auto it=st.begin();
pii p=*it;
if(p.fi>val) break;
st.erase(it);
}
st.insert({val,i});
}
atcoder::segtree<int,op,e> ids(n);
init(n);
build();
st.clear();
int ans=n;
rep(i,n){
if(a[i]<=t) continue;
if(dl[i]==-1){
ans-=1;
}else{
_set(dl[i],i+1,1);
ids.set(i,dl[i]);
}
}
while(k){
push(0,n,1);
pii p=_seg[1];
if(p.fi==0) break;
ans-=p.fi;
int frm=p.se;
while(p.fi>0){
tar=frm;
int c=ids.max_right<f>(frm);
int j=c;
_set(dl[j],j+1,-1);
ids.set(j,inf);
p.fi-=1;
}
k-=1;
}
print(ans);
}
# | 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... |