Submission #537585

#TimeUsernameProblemLanguageResultExecution timeMemory
537585Jarif_RahmanFood Court (JOI21_foodcourt)C++17
100 / 100
841 ms67068 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; const ll inf = 1e16; struct segtree_beats{ struct node{ ll mn[2], cnt[2]; ll sm, lazy_add; node(){ mn[0] = 0, mn[1] = inf; cnt[0] = 1, cnt[1] = 0; sm = 0; lazy_add = 0; } node operator+(node a){ node rt; if(mn[0] < a.mn[0]){ rt.mn[0] = mn[0], rt.cnt[0] = cnt[0]; if(mn[1] < a.mn[0]) rt.mn[1] = mn[1], rt.cnt[1] = cnt[1]; else if(mn[1] > a.mn[0]) rt.mn[1] = a.mn[0], rt.cnt[1] = a.cnt[0]; else rt.mn[1] = mn[1], rt.cnt[1] = cnt[1]+a.cnt[0]; } else if(mn[0] > a.mn[0]){ rt.mn[0] = a.mn[0], rt.cnt[0] = a.cnt[0]; if(mn[0] < a.mn[1]) rt.mn[1] = mn[0], rt.cnt[1] = cnt[0]; else if(mn[0] > a.mn[1]) rt.mn[1] = a.mn[1], rt.cnt[1] = a.cnt[1]; else rt.mn[1] = mn[0], rt.cnt[1] = cnt[0]+a.cnt[1]; } else{ rt.mn[0] = mn[0], rt.cnt[0] = cnt[0]+a.cnt[0]; if(mn[1] < a.mn[1]) rt.mn[1] = mn[1], rt.cnt[1] = cnt[1]; else if(mn[1] > a.mn[1]) rt.mn[1] = a.mn[1], rt.cnt[1] = a.cnt[1]; else rt.mn[1] = mn[1], rt.cnt[1] = cnt[1]+a.cnt[1]; } rt.sm = sm+a.sm; return rt; } void propagate(node nd, int sz){ lazy_add+=nd.lazy_add; sm+=nd.lazy_add*sz; mn[0]+=nd.lazy_add; if(mn[1] != inf) mn[1]+=nd.lazy_add; if(nd.mn[0] <= mn[0]) return; sm+=(nd.mn[0]-mn[0])*sz; mn[0] = nd.mn[0]; } }; int k; vector<node> v; segtree_beats(int n){ k = 1; while(k < n) k*=2; k*=2; v.resize(k, node()); for(int i = k/2-1; i > 0; i--) v[i] = v[2*i]+v[2*i+1]; } void add(int l, int r, int nd, int a, int b, ll x){ if(a > r || b < l){ return; } if(a >= l && b <= r){ node nd0; nd0.lazy_add = x; nd0.mn[0] = -inf+1; v[nd].propagate(nd0, b-a+1); return; } int md = (a+b)/2; v[2*nd].propagate(v[nd], md-a+1); v[2*nd+1].propagate(v[nd], b-md); v[nd].lazy_add = 0; add(l, r, 2*nd, a, md, x); add(l, r, 2*nd+1, md+1, b, x); v[nd] = v[2*nd]+v[2*nd+1]; } void add(int l, int r, ll x){ add(l, r, 1, 0, k/2-1, x); } void chmx(int l, int r, int nd, int a, int b, ll x){ if(a > r || b < l || v[nd].mn[0] >= x){ return; } if(a >= l && b <= r && v[nd].mn[1] > x){ node nd0; nd0.mn[0] = x; v[nd].propagate(nd0, b-a+1); return; } int md = (a+b)/2; v[2*nd].propagate(v[nd], md-a+1); v[2*nd+1].propagate(v[nd], b-md); v[nd].lazy_add = 0; chmx(l, r, 2*nd, a, md, x); chmx(l, r, 2*nd+1, md+1, b, x); v[nd] = v[2*nd]+v[2*nd+1]; } void chmx(int l, int r, ll x){ chmx(l, r, 1, 0, k/2-1, x); } ll sum(int l, int r, int nd, int a, int b){ if(a > r || b < l){ return 0; } if(a >= l && b <= r){ return v[nd].sm; } int md = (a+b)/2; v[2*nd].propagate(v[nd], md-a+1); v[2*nd+1].propagate(v[nd], b-md); v[nd].lazy_add = 0; ll rt = sum(l, r, 2*nd, a, md) + sum(l, r, 2*nd+1, md+1, b); v[nd] = v[2*nd]+v[2*nd+1]; return rt; } ll sum(int l, int r){ return sum(l, r, 1, 0, k/2-1); } }; struct BIT{ int n; vector<ll> sm; BIT(int _n){ n = _n; sm.resize(n); } void add(int in, ll x){ in++; while(in <= n) sm[in-1]+=x, in+=in&-in; } ll sum(int in){ in++; ll s = 0; while(in >= 1) s+=sm[in-1], in-=in&-in; return s; } ll sum(int l, int r){ return sum(r)-(l == 0? 0:sum(l-1)); } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> ql(n), qr(n); vector<int> ans; vector<pair<int, ll>> queries(q, {-1, -1}); vector<vector<pair<ll, int>>> qq(n); BIT sum(n+1); segtree_beats beats(n); for(int i = 0; i < q; i++){ int tt; cin >> tt; if(tt == 1){ int l, r, c; ll k; cin >> l >> r >> c >> k; l--, r--; queries[i] = {c, k}; ql[l].pb(i); qr[r].pb(i); sum.add(l, k); sum.add(r+1, -k); beats.add(l, r, k); } else if(tt == 2){ int l, r; ll k; cin >> l >> r >> k; l--, r--; beats.add(l, r, -k); beats.chmx(l, r, 0); } else{ int a; ll b; cin >> a >> b; a--; ll s = sum.sum(0, a); ll pos = s-beats.sum(a, a)+b; if(s < pos) ans.pb(0); else{ qq[a].pb({pos, ans.size()}); ans.pb(-1); } } } BIT Q(q); for(int i = n-1; i >= 0; i--){ for(int x: qr[i]) Q.add(x, queries[x].sc); for(auto [b, in]: qq[i]){ int l = 0, r = q-1; while(l < r){ int md = (l+r)/2; if(Q.sum(0, md) >= b) r = md; else l = md+1; } ans[in] = queries[l].f; } for(int x: ql[i]) Q.add(x, -queries[x].sc); } for(int x: ans) cout << x << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...