Submission #1005404

#TimeUsernameProblemLanguageResultExecution timeMemory
1005404GrindMachineThe short shank; Redemption (BOI21_prison)C++17
100 / 100
1709 ms377388 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2") #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; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* refs: edi */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; template<typename T> struct sparse_table { /*============================*/ T merge(T a, T b) { return min(a,b); } /*============================*/ vector<vector<T>> table; vector<int> bin_log; int LOG = 0; sparse_table() { } sparse_table(vector<T> &a, int n) { while ((1 << LOG) <= n) LOG++; table = vector<vector<T>>(n, vector<T>(LOG)); bin_log = vector<int>(n + 1); rep(i, n) table[i][0] = a[i]; rep1(j, LOG - 1) { rep(i, n) { int jump = 1 << (j - 1); if (i + jump >= n) { break; } table[i][j] = merge(table[i][j - 1], table[i + jump][j - 1]); } } bin_log[1] = 0; for (int i = 2; i <= n; ++i) { bin_log[i] = bin_log[i / 2] + 1; } } T query(int l, int r) { int len = r - l + 1; int k = bin_log[len]; T val1 = table[l][k]; T val2 = table[r - (1 << k) + 1][k]; return merge(val1, val2); } }; template<typename T> struct lazysegtree { /*=======================================================*/ struct data { int mx,ind; }; struct lazy { int a; }; data d_neutral = {-inf1,-1}; lazy l_neutral = {0}; void merge(data &curr, data &left, data &right) { if(left.mx >= right.mx) curr = left; else curr = right; } void create(int x, int lx, int rx, T v) { tr[x] = {v,lx}; } void modify(int x, int lx, int rx, T v) { lz[x].a = v; } void propagate(int x, int lx, int rx) { int v = lz[x].a; if(!v) return; tr[x].mx += v; if(rx-lx > 1){ lz[x<<1].a += v; lz[x<<1|1].a += v; } lz[x] = l_neutral; } /*=======================================================*/ int siz = 1; vector<data> tr; vector<lazy> lz; lazysegtree() { } lazysegtree(int n) { while (siz < n) siz *= 2; tr.assign(2 * siz, d_neutral); lz.assign(2 * siz, l_neutral); } void build(vector<T> &a, int n, int x, int lx, int rx) { if (rx - lx == 1) { if (lx < n) { create(x, lx, rx, a[lx]); } return; } int mid = (lx + rx) >> 1; build(a, n, x<<1, lx, mid); build(a, n, x<<1|1, mid, rx); merge(tr[x], tr[x<<1], tr[x<<1|1]); } void build(vector<T> &a, int n) { build(a, n, 1, 0, siz); } void rupd(int l, int r, T v, int x, int lx, int rx) { propagate(x, lx, rx); if (lx >= r or rx <= l) return; if (lx >= l and rx <= r) { modify(x, lx, rx, v); propagate(x, lx, rx); return; } int mid = (lx + rx) >> 1; rupd(l, r, v, x<<1, lx, mid); rupd(l, r, v, x<<1|1, mid, rx); merge(tr[x], tr[x<<1], tr[x<<1|1]); } void rupd(int l, int r, T v) { rupd(l, r + 1, v, 1, 0, siz); } data query(int l, int r, int x, int lx, int rx) { propagate(x, lx, rx); if (lx >= r or rx <= l) return d_neutral; if (lx >= l and rx <= r) return tr[x]; int mid = (lx + rx) >> 1; data curr; data left = query(l, r, x<<1, lx, mid); data right = query(l, r, x<<1|1, mid, rx); merge(curr, left, right); return curr; } data query(int l, int r) { return query(l, r + 1, 1, 0, siz); } }; template<typename T> struct segtree { // https://codeforces.com/blog/entry/18051 /*=======================================================*/ struct data { int mx,ind; }; data neutral = {-inf1,-1}; data merge(data &left, data &right) { data curr; if(left.mx >= right.mx) curr = left; else curr = right; return curr; } void create(int i, T v) { tr[i] = {v,i-n}; } void modify(int i, T v) { tr[i] = {v,i-n}; } /*=======================================================*/ int n; vector<data> tr; segtree() { } segtree(int siz) { init(siz); } void init(int siz) { n = siz; tr.assign(2 * n, neutral); } void build(vector<T> &a, int siz) { rep(i, siz) create(i + n, a[i]); rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]); } void pupd(int i, T v) { modify(i + n, v); for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]); } data query(int l, int r) { data resl = neutral, resr = neutral; for (l += n, r += n; l <= r; l >>= 1, r >>= 1) { if (l & 1) resl = merge(resl, tr[l++]); if (!(r & 1)) resr = merge(tr[r--], resr); } return merge(resl, resr); } }; void solve(int test_case) { int n,d,t; cin >> n >> d >> t; vector<int> a(n+5); rep1(i,n) cin >> a[i]; vector<int> b(n+5); rep1(i,n) b[i] = a[i] - i; sparse_table<int> sparse(b,n+1); vector<int> lx(n+5); // lx[i] = first pos to the left of i s.t if we start making ops from this pos, a[i] <= t rep1(i,n){ // a[j] + (i-j) = (a[j]-j) + i int l = 1, r = i; while(l <= r){ int mid = (l+r) >> 1; if(sparse.query(mid,i) + i <= t){ lx[i] = mid; l = mid + 1; } else{ r = mid - 1; } } } vector<bool> active(n+5); int ignore = 0; rep1(i,n){ if(a[i] > t){ if(!lx[i]){ ignore++; } else{ active[i] = 1; } } } vector<int> diff(n+5); vector<pii> segs; rep1(i,n){ if(!active[i]) conts; segs.pb({lx[i]+1,i}); } sort(all(segs)); for(auto [l,r] : segs){ diff[l]++, diff[r+1]--; } rep1(i,n) diff[i] += diff[i-1]; lazysegtree<int> st(n+5); st.build(diff,n+1); int siz = sz(segs); vector<int> seg_build(siz); rep(i,siz){ seg_build[i] = segs[i].ss; } segtree<int> seg(siz+5); seg.build(seg_build,siz); rep1(iter,d){ auto [mx,ind] = st.query(1,n); if(ind == -1) break; pii px = {ind+1,-1}; int pos = upper_bound(all(segs),px)-segs.begin()-1; while(mx--){ auto [mxr,ind2] = seg.query(0,pos); assert(mxr >= ind); st.rupd(segs[ind2].ff,segs[ind2].ss,-1); seg.pupd(ind2,-inf1); ignore++; } } int ans = n-ignore; cout << ans << endl; } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } return 0; }
#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...