Submission #660057

#TimeUsernameProblemLanguageResultExecution timeMemory
660057MahdiThe short shank; Redemption (BOI21_prison)C++17
0 / 100
1 ms340 KiB
#include<bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define F first #define S second typedef long long ll; typedef pair<int, int> pii; const int N=2e6+1e5; int n, d, t, a[N], mx[N], nx[N], be[N], h[N]; set<int>zr; struct fenwick{ int bit[N]; void add(int i, int val=1){ for(;i<n;i|=(i+1)) bit[i]+=val; } int sum(int r){ int res=0; for(;r>=0;r=(r&(r+1))-1) res+=bit[r]; return res; } int sum(int l, int r){ if(r>l) return sum(r-1)-sum(l-1); return 0; } } A; inline void del(int l, int r){ auto it=zr.lower_bound(l); while(it!=zr.end() && *it<r){ A.add(*it, -1); zr.erase(it); it=zr.lower_bound(l); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>d>>t; for(int i=0;i<n;++i) cin>>a[i]; int ans=0; for(int i=0;i<n;++i){ mx[i]=t-a[i]+i+1; if(a[i]<=t) ++ans; else{ zr.insert(i); A.add(i); } } for(int i=0;i<n-1;++i){ if(mx[i]>i+1 && mx[i+1]<=i+1) h[i]=1; } iota(nx, nx+n, 1); iota(be, be+n, -1); set<pii>st; for(int i=0;i<n-1;++i){ st.insert({h[i], i}); } for(int i=n-1;i>d;--i){ pii z=*st.begin(); st.erase(st.begin()); ans+=z.F; int r=nx[z.S]; int l=be[z.S]; del(z.S+1, min(mx[z.S], r+1)); be[r]=l; if(l>=0) nx[l]=r; if(r!=n-1){ mx[r]=max(mx[r], mx[z.S]); st.erase({h[r], r}); h[r]=A.sum(r+1, min(nx[r]+1, mx[r])); st.insert({h[r], r}); } if(l!=-1){ st.erase({h[l], l}); h[r]=A.sum(l+1, min(r+1, mx[l])); st.insert({h[l], l}); } } cout<<ans<<'\n'; }
#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...