Submission #433358

#TimeUsernameProblemLanguageResultExecution timeMemory
433358couplefireThe short shank; Redemption (BOI21_prison)C++17
80 / 100
2101 ms288376 KiB
#include<bits/stdc++.h> using namespace std; const int N=2e6+5,mod=1e9+7; int t,tree[4*N],n,a[N],lazy[4*N],ans,D,removed[N],mn[N][24]; pair<int,int> tr[4*N]; pair<int,int> w[4*N]; void build(int u,int l,int r){ if(l==r) { tree[u] = a[l] - l; tr[u].second = l; return; } int mid=(l+r)/2; build(2*u,l,mid); build(2*u+1,mid+1,r); tree[u] = min(tree[2*u],tree[2*u+1]); tr[u].second = l; } void update(int u,int start,int end,int l,int r,int val) { if(lazy[u]) { tr[u].first += lazy[u]; if(l!=r){ lazy[2*u] += lazy[u]; lazy[2*u+1] += lazy[u]; } lazy[u] = 0; } if(l>end || r<start) return; if(start<=l && r<=end) { lazy[u] = val; tr[u].first += lazy[u]; if(l!=r){ lazy[2*u] += lazy[u]; lazy[2*u+1] += lazy[u]; } lazy[u] = 0; return; } int mid = (l+r)/2; update(2*u,start,end,l,mid,val); update(2*u+1,start,end,mid+1,r,val); tr[u] = max(tr[2*u],tr[2*u+1]); } void insert(int u,int ind,int l,int r,int st,int en) { if(l>ind || r<ind) return; if(l==r) { w[u] = {st,en}; return; } int mid = (l+r)/2; insert(2*u,ind,l,mid,st,en); insert(2*u+1,ind,mid+1,r,st,en); w[u] = min(w[2*u],w[2*u+1]); } pair<int, int> get(int u,int start,int end,int l,int r) { if(l>end || r<start) return {n+5,0}; if(start <= l && r <= end) { return w[u]; } int mid=(l+r)/2; return min(get(2*u,start,end,l,mid),get(2*u+1,start,end,mid+1,r)); } signed main(){ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>D>>t; for(int i=1;i<=n;i++) { cin >> a[i]; mn[i][0] = a[i] - i; } int Lg = log2(n) + 1; for(int i=1;i<=Lg;i++) { for(int j=1;j<=n;j++) { if(j+(1<<(i-1)) <= n)mn[j][i] = min(mn[j][i-1],mn[j+(1<<(i-1))][i-1]); else mn[j][i] = mn[j][i-1]; } } build(1,1,n); for(int i=1;i<=n;i++){ int l = 1, r =i,ind = 0; while(l<=r){ int mid=(l+r)/2; int lg = log2(i-mid+1); if(min(mn[i-(1<<lg)+1][lg],mn[mid][lg]) <= t-i) { ind = mid; l = mid + 1; } else r = mid - 1; } if(ind) ans++; if(ind && ind!=i) { update(1,ind+1,i,1,n,1); insert(1,i,1,n,ind+1,i); } else { insert(1,i,1,n,n+1,i); } } int b = 0; int cnt = 0; while(tr[1].first && D) { int c = tr[1].second; D--; ans -= tr[1].first; while(get(1,c,n,1,n).first <= c) { cnt++; if(cnt > n) return 0; pair<int,int> g = get(1,c,n,1,n); insert(1,g.second,1,n,n+1,g.second); removed[g.second] = 1; b ++ ; update(1,g.first,g.second,1,n,-1); } } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...