This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 1e6+23, lg = 21;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, d, T, t[N], ans, seg[5*N], res[N], l[N];
int lazy[5*N], seg2[5*N];
void update2(int ind, int val) {
if(ind == 0) return;
if(ind >= (1<<lg)) {
seg[ind] = val;
} else {
seg[ind] = min(seg[2*ind], seg[2*ind+1]);
}
update2(ind/2, val);
}
int find(int val, int ind=1) {
if(seg[ind] > val) return 0;
if(ind >= (1<<lg)) {
return (ind-(1<<lg)+1);
}
if(seg[2*ind+1] <= val) return find(val, 2*ind+1);
return find(val, 2*ind);
}
void pushdown(int ind) {
seg[ind] += lazy[ind];
if(ind < (1<<lg)) {
lazy[2*ind] += lazy[ind];
lazy[2*ind+1] += lazy[ind];
}
lazy[ind] = 0;
}
void update(int l, int r, int val, int ind=1, int lc=1, int rc=(1<<lg)+1) {
pushdown(ind);
if(r<=lc || rc<=l) return;
if(lc>=l && rc<=r) {
lazy[ind] += val;
pushdown(ind);
return;
}
int mid = (lc+rc)/2;
update(l, r, val, 2*ind, lc, mid);
update(l, r, val, 2*ind+1, mid, rc);
seg[ind] = max(seg[2*ind], seg[2*ind+1]);
}
int query(int ind=1, int lc=1, int rc=(1<<lg)+1) {
pushdown(ind);
if(ind >= (1<<lg)) {
return lc;
}
pushdown(2*ind); pushdown(2*ind+1);
int mid = (lc+rc)/2;
if(seg[ind] == seg[2*ind]) {
return query(2*ind, lc, mid);
}
return query(2*ind+1, mid, rc);
}
void update3(int ind, int val) {
if(ind == 0) return;
if(ind >= (1<<lg)) {
seg2[ind] = val;
} else {
seg2[ind] = (l[seg2[2*ind]]<l[seg2[2*ind+1]] ? seg2[2*ind] : seg2[2*ind+1]);
}
update3(ind/2, val);
}
int query3(int l2, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(lc>=r || l2>=rc) return 0;
if(lc>=l2 && rc<=r) {
return seg2[ind];
}
int mid = (lc+rc)/2;
int x = query3(l2, r, 2*ind, lc, mid), y = query3(l2, r, 2*ind+1, mid, rc);
return (l[x]<l[y] ? x : y);
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>d>>T;
l[0] = n+1;
for(int i=1; i<=n; i++) {cin>>t[i];}
fill(seg, seg+4*N, (int)2e9);
for(int i=1; i<=n; i++) {
update2(i+(1<<lg)-1, t[i]-i);
int j = find(T-i);
if(j==0 || j == i) {
l[i] = n+1;
ans+=(j<i);
} else {
l[i] = j+1;
}
}
fill(seg, seg+4*N, 0);
for(int i=1; i<=n; i++) {
if(l[i] > n) continue;
update(l[i], i+1, 1);
update3(i+(1<<lg)-1, i);
}
for(int i=1; i<=d; i++) {
int wh = query();
ans += seg[(1<<lg)+wh-1];
while(l[query3(wh, n+1)] <= wh) {
int ff = query3(wh, n+1);
update(l[ff], ff+1, -1);
l[ff] = n+1;
update3(ff+(1<<lg)-1, ff);
}
}
cout<<n-ans<<endl;
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... |