제출 #1160682

#제출 시각아이디문제언어결과실행 시간메모리
1160682fzyzzz_zTwo Dishes (JOI19_dishes)C++20
100 / 100
3093 ms215288 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1000001; template<class Info, class Tag> struct LazySegmentTree { int n; std::vector<Info> info; std::vector<Tag> tag; LazySegmentTree() : n(0) {} LazySegmentTree(int n_, Info v_ = Info()) { init(n_, v_); } template<class T> LazySegmentTree(std::vector<T> init_) { init(init_); } void init(int n_, Info v_ = Info()) { init(std::vector<Info>(n_, v_)); } template<class T> void init(std::vector<T> init_) { n = init_.size(); info.assign(4 << std::__lg(n), Info()); tag.assign(4 << std::__lg(n), Tag()); std::function<void(int, int, int)> build = [&](int p, int l, int r) { if (r - l == 1) { info[p] = init_[l]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); pull(p); }; build(1, 0, n); } void pull(int p) { info[p] = info[2 * p] + info[2 * p + 1]; } void apply(int p, const Tag &v) { info[p].apply(v); tag[p].apply(v); } void push(int p) { apply(2 * p, tag[p]); apply(2 * p + 1, tag[p]); tag[p] = Tag(); } void modify(int p, int l, int r, int x, const Info &v) { if (r - l == 1) { info[p] = v; return; } int m = (l + r) / 2; push(p); if (x < m) { modify(2 * p, l, m, x, v); } else { modify(2 * p + 1, m, r, x, v); } pull(p); } void modify(int p, const Info &v) { modify(1, 0, n, p, v); } Info rangeQuery(int p, int l, int r, int x, int y) { if (l >= y || r <= x) { return Info(); } if (l >= x && r <= y) { return info[p]; } int m = (l + r) / 2; push(p); return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y); } Info rangeQuery(int l, int r) { return rangeQuery(1, 0, n, l, r); } void rangeApply(int p, int l, int r, int x, int y, const Tag &v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { apply(p, v); return; } int m = (l + r) / 2; push(p); rangeApply(2 * p, l, m, x, y, v); rangeApply(2 * p + 1, m, r, x, y, v); pull(p); } void rangeApply(int l, int r, const Tag &v) { return rangeApply(1, 0, n, l, r, v); } template<class F> int findFirst(int p, int l, int r, int x, int y, F pred) { if (l >= y || r <= x || !pred(info[p])) { return -1; } if (r - l == 1) { return l; } int m = (l + r) / 2; push(p); int res = findFirst(2 * p, l, m, x, y, pred); if (res == -1) { res = findFirst(2 * p + 1, m, r, x, y, pred); } return res; } template<class F> int findFirst(int l, int r, F pred) { return findFirst(1, 0, n, l, r, pred); } template<class F> int findLast(int p, int l, int r, int x, int y, F pred) { if (l >= y || r <= x || !pred(info[p])) { return -1; } if (r - l == 1) { return l; } int m = (l + r) / 2; push(p); int res = findLast(2 * p + 1, m, r, x, y, pred); if (res == -1) { res = findLast(2 * p, l, m, x, y, pred); } return res; } template<class F> int findLast(int l, int r, F pred) { return findLast(1, 0, n, l, r, pred); } }; struct Tag { ll add = 0; // To store the value to be added to the range ll set = -1; // To store the value to set the range to. -1 means no set operation. Tag(ll x, ll y) { add = x; set = y; } Tag() {} void apply(Tag t) & { if (t.set != -1) { // If there is a "set" operation set = t.set; // Mark this segment as set to a specific value add = 0; // Reset add because "set" takes precedence over "add" } add += t.add; // Accumulate the add operation } }; struct Info { ll max = 0; // To store the maximum value in the segment // Apply a tag operation to this Info Info(ll x) { max = x; } Info() {} void apply(Tag t) & { if (t.set != -1) { max = t.set; // If a "set" operation exists, set the max to that value } max += t.add; // Apply any add operation (this will add to the current max) } }; // Combine two `Info` structures by taking the maximum of their values Info operator+(const Info &a, const Info &b) { return Info(max(a.max, b.max)); } // [l, r) for all ops array<ll, 3> a[N], b[N]; ll pa[N], pb[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; // {time, req, score} for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { cin >> a[i][j]; } } for (int i = 0; i < m; ++i) { for (int j = 0; j < 3; ++j) { cin >> b[i][j]; } } for (int i = 0; i < n; ++i) { pa[i + 1] = pa[i] + a[i][0]; } for (int i = 0; i < m; ++i) { pb[i + 1] = pb[i] + b[i][0]; } ll ans = 0; // edges[i] : {time, dir, score} -1 means A[i]->B vector<vector<array<ll, 3>>> edges(n); for (int i = 0; i < n; ++i) { auto can_get = [&] (int x) -> bool { // can process x'th from B and still get award for A[i] return pb[x + 1] + pa[i + 1] <= a[i][1]; }; if (can_get(m - 1)) { ans += a[i][2]; continue; } if (!can_get(-1)) { continue; } int lo = -1, hi = m - 1; while (lo < hi) { int md = (lo + hi + 1) / 2; if (can_get(md)) lo = md; else hi = md - 1; } if (a[i][2] > 0) { // do A[i] before B[lo + 1] edges[i].push_back({lo + 1, -1, a[i][2]}); } else { // do B[lo + 1] before A[i] ans += a[i][2]; edges[i].push_back({lo + 1, 1, -a[i][2]}); } } for (int i = 0; i < m; ++i) { auto can_get = [&] (int x) -> bool { return pa[x + 1] + pb[i + 1] <= b[i][1]; }; if (can_get(n - 1)) { ans += b[i][2]; continue; } if (!can_get(-1)) { continue; } int lo = -1, hi = n - 1; while (lo < hi) { int md = (lo + hi + 1) / 2; if (can_get(md)) lo = md; else hi = md - 1; } if (b[i][2] > 0) { // do B[i] before A[lo + 1] edges[lo + 1].push_back({i, 1, b[i][2]}); } else { // do A[lo + 1] before B[i] ans += b[i][2]; edges[lo + 1].push_back({i, -1, -b[i][2]}); } } LazySegmentTree<Info, Tag> st(m + 2); for (int t = 0; t < n; ++t) { for (auto [at, dir, score]: edges[t]) { if (dir == 1) { st.rangeApply(at + 1, m + 2, Tag(score, -1)); } } map<int, ll> add_pref; for (auto [at, dir, score]: edges[t]) { if (dir == -1) { add_pref[at] += score; } } vector<int> ps; for (auto [at, add]: add_pref) { st.rangeApply(0, at + 1, Tag(add, -1)); ps.push_back(at); } for (int i = 0; i < ps.size(); ++i) { int next = (i + 1 < ps.size() ? ps[i + 1] : m + 1); ll cv = st.rangeQuery(ps[i], ps[i] + 1).max; int p = st.findFirst(ps[i] + 1, next + 1, [&](const Info & nd){ return nd.max >= cv; }); if (p == -1) { st.rangeApply(ps[i] + 1, next + 1, Tag(0, cv)); } else if (p > ps[i] + 1) { st.rangeApply(ps[i] + 1, p, Tag(0, cv)); } } // for (int i = 0; i <= m; ++i) { // cout << st.rangeQuery(i, i + 1).max << " \n"[i == m]; // } } cout << (ans + st.rangeQuery(0, m + 1).max) << '\n'; 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...