Submission #728514

#TimeUsernameProblemLanguageResultExecution timeMemory
728514piOOE푸드 코트 (JOI21_foodcourt)C++17
100 / 100
875 ms61160 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; namespace SegmentTree { struct Info { ll sum = 0, mn = 0, mn2 = 3e18; int cntmn = 0, len = 0; }; Info operator+(const Info &a, const Info &b) { Info res{}; res.sum = a.sum + b.sum, res.len = a.len + b.len; res.mn = min(a.mn, b.mn); res.mn2 = min(a.mn == res.mn ? a.mn2 : a.mn, b.mn == res.mn ? b.mn2 : b.mn); res.cntmn = (a.mn == res.mn) * a.cntmn + (b.mn == res.mn) * b.cntmn; return res; } vector<Info> t; vector<ll> tag; int sz = 1; void pull(int x) { t[x] = t[x << 1] + t[x << 1 | 1]; } void applyAdd(int x, ll tg) { tag[x] += tg; t[x].sum += t[x].len * tg; t[x].mn += tg, t[x].mn2 += tg; } void applyMin(int x, ll d) { if (t[x].mn < d) { assert(t[x].mn2 > d); t[x].sum += t[x].cntmn * (d - t[x].mn); t[x].mn = d; } } void push(int x) { if (tag[x]) { applyAdd(x << 1, tag[x]); applyAdd(x << 1 | 1, tag[x]); tag[x] = 0; } applyMin(x << 1, t[x].mn); applyMin(x << 1 | 1, t[x].mn); } void init(int n) { sz = 1 << __lg(n) + !!(n & (n - 1)); t.assign(sz << 1, {}), tag.assign(sz << 1, {}); for (int i = 0; i < n; ++i) { t[i + sz].cntmn = t[i + sz].len = 1; } for (int i = sz - 1; i > 0; --i) { pull(i); } } void rangeAdd(int l, int r, ll d, int x, int lx, int rx) { if (lx >= r || l >= rx) { return; } if (l <= lx && rx <= r) { return applyAdd(x, d); } push(x); int mid = (lx + rx) >> 1; rangeAdd(l, r, d, x << 1, lx, mid), rangeAdd(l, r, d, x << 1 | 1, mid, rx); pull(x); } void rangeSetMax(int l, int r, ll d, int x = 1, int lx = 0, int rx = sz) { if (l >= rx || lx >= r || t[x].mn >= d) { return; } if (l <= lx && rx <= r && t[x].mn2 > d) { return applyMin(x, d); } push(x); int mid = lx + rx >> 1; rangeSetMax(l, r, d, x << 1, lx, mid), rangeSetMax(l, r, d, x << 1 | 1, mid, rx); pull(x); } void rangeAdd(int l, int r, ll d) { rangeAdd(l, r, d, 1, 0, sz); rangeSetMax(0, sz, 0); } ll sum(int i, int x = 1, int lx = 0, int rx = sz) { while (x < sz) { push(x); int mid = lx + rx >> 1; if (i < mid) { x = x << 1; rx = mid; } else { x = x << 1 | 1; lx = mid; } } return t[x].sum; } } namespace SegmentTreeMin { constexpr int N_ = 1 << 19; pair<ll, int> t[N_]; // f(x) = max(0, x - t[i].first) + t[i].second ll tag[N_]; int sz = 1; void init(int n) { sz = 1 << __lg(n) + !!(n & (n - 1)); for (int i = 0; i < sz; ++i) { t[i + sz] = {i < n ? 0 : 3e18, i}; } for (int i = sz - 1; i > 0; --i) { t[i] = min(t[i << 1], t[i << 1 | 1]); } } void apply(int x, ll d) { tag[x] += d, t[x].first += d; } void rangeAdd(int l, int r, ll a, int x = 1, int lx = 0, int rx = sz) { if (l >= rx || lx >= r) { return; } if (l <= lx && rx <= r) { return apply(x, a); } int mid = lx + rx >> 1; rangeAdd(l, r, a, x << 1, lx, mid), rangeAdd(l, r, a, x << 1 | 1, mid, rx); t[x] = min(t[x << 1], t[x << 1 | 1]); t[x].first += tag[x]; } pair<ll, int> top() { return t[1]; } } template<typename T> struct Fenwick { int n; vector<T> a; Fenwick() = default; explicit Fenwick(int n) : n(n), a(n + 1) {} void modify(int x, T v) { for (int i = x + 1; i <= n; i += i & -i) { a[i] += v; } } void modify(int l, int r, T v) { if (l >= r) return; modify(l, v), modify(r, -v); } T sum(int x) { T ans = 0; for (int i = x + 1; i > 0; i -= i & -i) { ans += a[i]; } return ans; } }; constexpr int N = 2.5e5 + 10; vector<pair<ll, int>> que[N]; int pnt[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, q; cin >> n >> m >> q; struct Query { int t, l, r, c; ll k; }; vector<Query> queries(q); Fenwick<ll> fn(n), fnAdd(n); SegmentTree::init(n); SegmentTreeMin::init(n); vector<ll> ans(q); for (int i = 0; i < q; ++i) { cin >> queries[i].t; if (queries[i].t == 1) { cin >> queries[i].l >> queries[i].r >> queries[i].c >> queries[i].k; } else if (queries[i].t == 2) { cin >> queries[i].l >> queries[i].r >> queries[i].k; } else { cin >> queries[i].l >> queries[i].k; } queries[i].l -= 1; if (queries[i].t == 1) { SegmentTree::rangeAdd(queries[i].l, queries[i].r, queries[i].k); fnAdd.modify(queries[i].l, queries[i].r, queries[i].k); } else if (queries[i].t == 2) { SegmentTree::rangeAdd(queries[i].l, queries[i].r, -queries[i].k); fn.modify(queries[i].l, queries[i].r, queries[i].k); } else { ll s = fn.sum(queries[i].l); ll have = SegmentTree::sum(queries[i].l) + s; ll need = queries[i].k + s; if (have >= need) { que[queries[i].l].emplace_back(fnAdd.sum(queries[i].l) - (have - s - queries[i].k), i); } } } auto relax = [&](int i) { if (pnt[i] == que[i].size()) { SegmentTreeMin::rangeAdd(i, i + 1, 3e18); } else { SegmentTreeMin::rangeAdd(i, i + 1, que[i][pnt[i]].first - (pnt[i] == 0 ? 0 : que[i][pnt[i] - 1].first)); } }; for (int i = 0; i < n; ++i) { sort(que[i].begin(), que[i].end()); relax(i); } for (int i = 0; i < q; ++i) { if (queries[i].t == 1) { SegmentTreeMin::rangeAdd(queries[i].l, queries[i].r, -queries[i].k); } while (SegmentTreeMin::t[1].first <= 0) { int j = SegmentTreeMin::t[1].second; ans[que[j][pnt[j]].second] = queries[i].c; ++pnt[j]; relax(j); } } for (int i = 0; i < q; ++i) { if (queries[i].t == 3) { cout << ans[i] << '\n'; } } return 0; }

Compilation message (stderr)

foodcourt.cpp: In function 'void SegmentTree::init(int)':
foodcourt.cpp:54:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   54 |         sz = 1 << __lg(n) + !!(n & (n - 1));
      |                   ~~~~~~~~^~~~~~~~~~~~~~~~~
foodcourt.cpp: In function 'void SegmentTree::rangeSetMax(int, int, ll, int, int, int)':
foodcourt.cpp:86:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |         int mid = lx + rx >> 1;
      |                   ~~~^~~~
foodcourt.cpp: In function 'll SegmentTree::sum(int, int, int, int)':
foodcourt.cpp:99:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |             int mid = lx + rx >> 1;
      |                       ~~~^~~~
foodcourt.cpp: In function 'void SegmentTreeMin::init(int)':
foodcourt.cpp:120:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  120 |         sz = 1 << __lg(n) + !!(n & (n - 1));
      |                   ~~~~~~~~^~~~~~~~~~~~~~~~~
foodcourt.cpp: In function 'void SegmentTreeMin::rangeAdd(int, int, ll, int, int, int)':
foodcourt.cpp:140:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  140 |         int mid = lx + rx >> 1;
      |                   ~~~^~~~
foodcourt.cpp: In lambda function:
foodcourt.cpp:234:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  234 |         if (pnt[i] == que[i].size()) {
      |             ~~~~~~~^~~~~~~~~~~~~~~~
#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...