답안 #728407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
728407 2023-04-22T10:47:12 Z piOOE 푸드 코트 (JOI21_foodcourt) C++17
41 / 100
1000 ms 46656 KB
#pragma GCC optimize("Ofast")
#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;
    }
}

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<int> que[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);

    vector<int> L(q, -1), R(q, -1);
    vector<ll> aim(q);
    vector<int> pos, pp(q, -1);

    Fenwick<ll> fn(n);
    SegmentTree::init(n);

    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;
            pp[i] = pos.size();
            pos.push_back(i);
        } else if (queries[i].t == 2) {
            cin >> queries[i].l >> queries[i].r >> queries[i].k;
            pp[i] = pos.size();
            pos.push_back(i);
        } 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);
        } 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) {
                if (m == 1) {
                    R[i] = -2;
                } else {
                    R[i] = pos.size() - 1;
                    aim[i] = need;
                }
            } else {
                R[i] = -1;
            }
        }
    }

    int cnt = 0;
    while (true) {
        assert(++cnt <= 30);

        for (int i = 0; i < q; ++i) {
            que[i].clear();
        }

        bool found = false;

        for (int i = 0; i < q; ++i) {
            if (L[i] + 1 < R[i]) {
                found = true;
                que[pos[L[i] + R[i] >> 1]].push_back(i);
            }
        }

        if (!found) {
            break;
        }

        fn = Fenwick<ll>(n);
        SegmentTree::init(n);

        for (int i = 0; i < q; ++i) {
            if (queries[i].t == 1) {
                SegmentTree::rangeAdd(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);
            }

            for (int j : que[i]) {
                ll need = aim[j];
                ll have = SegmentTree::sum(queries[j].l) + fn.sum(queries[j].l);

                if (have >= need) {
                    R[j] = pp[i];
                } else {
                    L[j] = pp[i];
                }
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        if (queries[i].t == 3) {
            if (R[i] == -1) {
                cout << 0 << '\n';
            } else if (R[i] >= 0) {
                cout << queries[pos[R[i]]].c << '\n';
            } else {
                cout << 1 << '\n';
            }
        }
    }


    return 0;
}

Compilation message

foodcourt.cpp: In function 'void SegmentTree::init(int)':
foodcourt.cpp:55:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   55 |         sz = 1 << __lg(n) + !!(n & (n - 1));
      |                   ~~~~~~~~^~~~~~~~~~~~~~~~~
foodcourt.cpp: In function 'void SegmentTree::rangeSetMax(int, int, ll, int, int, int)':
foodcourt.cpp:87:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |         int mid = lx + rx >> 1;
      |                   ~~~^~~~
foodcourt.cpp: In function 'll SegmentTree::sum(int, int, int, int)':
foodcourt.cpp:100:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  100 |             int mid = lx + rx >> 1;
      |                       ~~~^~~~
foodcourt.cpp: In function 'int main()':
foodcourt.cpp:219:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  219 |                 que[pos[L[i] + R[i] >> 1]].push_back(i);
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6484 KB Output is correct
2 Correct 15 ms 6504 KB Output is correct
3 Correct 10 ms 6472 KB Output is correct
4 Correct 14 ms 6484 KB Output is correct
5 Correct 5 ms 6228 KB Output is correct
6 Correct 5 ms 6228 KB Output is correct
7 Correct 14 ms 6484 KB Output is correct
8 Correct 14 ms 6452 KB Output is correct
9 Correct 17 ms 6484 KB Output is correct
10 Correct 14 ms 6484 KB Output is correct
11 Correct 18 ms 6524 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 7 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 9 ms 6484 KB Output is correct
16 Correct 12 ms 6484 KB Output is correct
17 Correct 15 ms 6604 KB Output is correct
18 Correct 17 ms 6484 KB Output is correct
19 Correct 11 ms 6480 KB Output is correct
20 Correct 12 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6484 KB Output is correct
2 Correct 15 ms 6504 KB Output is correct
3 Correct 10 ms 6472 KB Output is correct
4 Correct 14 ms 6484 KB Output is correct
5 Correct 5 ms 6228 KB Output is correct
6 Correct 5 ms 6228 KB Output is correct
7 Correct 14 ms 6484 KB Output is correct
8 Correct 14 ms 6452 KB Output is correct
9 Correct 17 ms 6484 KB Output is correct
10 Correct 14 ms 6484 KB Output is correct
11 Correct 18 ms 6524 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 7 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 9 ms 6484 KB Output is correct
16 Correct 12 ms 6484 KB Output is correct
17 Correct 15 ms 6604 KB Output is correct
18 Correct 17 ms 6484 KB Output is correct
19 Correct 11 ms 6480 KB Output is correct
20 Correct 12 ms 6484 KB Output is correct
21 Correct 15 ms 6484 KB Output is correct
22 Correct 14 ms 6484 KB Output is correct
23 Correct 12 ms 6520 KB Output is correct
24 Correct 14 ms 6484 KB Output is correct
25 Correct 5 ms 6228 KB Output is correct
26 Correct 5 ms 6256 KB Output is correct
27 Correct 14 ms 6484 KB Output is correct
28 Correct 14 ms 6520 KB Output is correct
29 Correct 16 ms 6520 KB Output is correct
30 Correct 14 ms 6504 KB Output is correct
31 Correct 15 ms 6520 KB Output is correct
32 Correct 18 ms 6484 KB Output is correct
33 Correct 8 ms 6484 KB Output is correct
34 Correct 7 ms 6484 KB Output is correct
35 Correct 11 ms 6476 KB Output is correct
36 Correct 13 ms 6520 KB Output is correct
37 Correct 10 ms 6448 KB Output is correct
38 Correct 12 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 859 ms 15952 KB Output is correct
2 Correct 863 ms 17096 KB Output is correct
3 Correct 842 ms 17120 KB Output is correct
4 Correct 911 ms 16984 KB Output is correct
5 Correct 894 ms 17128 KB Output is correct
6 Correct 876 ms 17088 KB Output is correct
7 Correct 45 ms 10712 KB Output is correct
8 Correct 60 ms 10948 KB Output is correct
9 Correct 853 ms 17140 KB Output is correct
10 Correct 915 ms 17096 KB Output is correct
11 Execution timed out 1014 ms 17108 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 343 ms 39512 KB Output is correct
2 Correct 374 ms 41080 KB Output is correct
3 Correct 363 ms 46232 KB Output is correct
4 Correct 307 ms 41932 KB Output is correct
5 Correct 256 ms 41712 KB Output is correct
6 Correct 370 ms 46532 KB Output is correct
7 Correct 87 ms 22000 KB Output is correct
8 Correct 91 ms 21948 KB Output is correct
9 Correct 372 ms 46284 KB Output is correct
10 Correct 303 ms 46400 KB Output is correct
11 Correct 362 ms 46632 KB Output is correct
12 Correct 358 ms 46536 KB Output is correct
13 Correct 422 ms 46492 KB Output is correct
14 Correct 412 ms 46520 KB Output is correct
15 Correct 408 ms 46372 KB Output is correct
16 Correct 417 ms 46408 KB Output is correct
17 Correct 404 ms 46356 KB Output is correct
18 Correct 389 ms 46656 KB Output is correct
19 Correct 440 ms 46452 KB Output is correct
20 Correct 399 ms 46412 KB Output is correct
21 Correct 431 ms 46556 KB Output is correct
22 Correct 426 ms 46420 KB Output is correct
23 Correct 378 ms 46464 KB Output is correct
24 Correct 406 ms 46384 KB Output is correct
25 Correct 284 ms 45408 KB Output is correct
26 Correct 278 ms 46004 KB Output is correct
27 Correct 364 ms 46236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6484 KB Output is correct
2 Correct 15 ms 6504 KB Output is correct
3 Correct 10 ms 6472 KB Output is correct
4 Correct 14 ms 6484 KB Output is correct
5 Correct 5 ms 6228 KB Output is correct
6 Correct 5 ms 6228 KB Output is correct
7 Correct 14 ms 6484 KB Output is correct
8 Correct 14 ms 6452 KB Output is correct
9 Correct 17 ms 6484 KB Output is correct
10 Correct 14 ms 6484 KB Output is correct
11 Correct 18 ms 6524 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 7 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 9 ms 6484 KB Output is correct
16 Correct 12 ms 6484 KB Output is correct
17 Correct 15 ms 6604 KB Output is correct
18 Correct 17 ms 6484 KB Output is correct
19 Correct 11 ms 6480 KB Output is correct
20 Correct 12 ms 6484 KB Output is correct
21 Correct 859 ms 15952 KB Output is correct
22 Correct 863 ms 17096 KB Output is correct
23 Correct 842 ms 17120 KB Output is correct
24 Correct 911 ms 16984 KB Output is correct
25 Correct 894 ms 17128 KB Output is correct
26 Correct 876 ms 17088 KB Output is correct
27 Correct 45 ms 10712 KB Output is correct
28 Correct 60 ms 10948 KB Output is correct
29 Correct 853 ms 17140 KB Output is correct
30 Correct 915 ms 17096 KB Output is correct
31 Execution timed out 1014 ms 17108 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 492 ms 16144 KB Output is correct
2 Correct 767 ms 16304 KB Output is correct
3 Correct 732 ms 16688 KB Output is correct
4 Correct 370 ms 15360 KB Output is correct
5 Correct 463 ms 16044 KB Output is correct
6 Correct 563 ms 16584 KB Output is correct
7 Correct 58 ms 9932 KB Output is correct
8 Correct 61 ms 9532 KB Output is correct
9 Correct 270 ms 16584 KB Output is correct
10 Correct 327 ms 15172 KB Output is correct
11 Correct 461 ms 16584 KB Output is correct
12 Correct 497 ms 16584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6484 KB Output is correct
2 Correct 15 ms 6504 KB Output is correct
3 Correct 10 ms 6472 KB Output is correct
4 Correct 14 ms 6484 KB Output is correct
5 Correct 5 ms 6228 KB Output is correct
6 Correct 5 ms 6228 KB Output is correct
7 Correct 14 ms 6484 KB Output is correct
8 Correct 14 ms 6452 KB Output is correct
9 Correct 17 ms 6484 KB Output is correct
10 Correct 14 ms 6484 KB Output is correct
11 Correct 18 ms 6524 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 7 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 9 ms 6484 KB Output is correct
16 Correct 12 ms 6484 KB Output is correct
17 Correct 15 ms 6604 KB Output is correct
18 Correct 17 ms 6484 KB Output is correct
19 Correct 11 ms 6480 KB Output is correct
20 Correct 12 ms 6484 KB Output is correct
21 Correct 15 ms 6484 KB Output is correct
22 Correct 14 ms 6484 KB Output is correct
23 Correct 12 ms 6520 KB Output is correct
24 Correct 14 ms 6484 KB Output is correct
25 Correct 5 ms 6228 KB Output is correct
26 Correct 5 ms 6256 KB Output is correct
27 Correct 14 ms 6484 KB Output is correct
28 Correct 14 ms 6520 KB Output is correct
29 Correct 16 ms 6520 KB Output is correct
30 Correct 14 ms 6504 KB Output is correct
31 Correct 15 ms 6520 KB Output is correct
32 Correct 18 ms 6484 KB Output is correct
33 Correct 8 ms 6484 KB Output is correct
34 Correct 7 ms 6484 KB Output is correct
35 Correct 11 ms 6476 KB Output is correct
36 Correct 13 ms 6520 KB Output is correct
37 Correct 10 ms 6448 KB Output is correct
38 Correct 12 ms 6484 KB Output is correct
39 Correct 859 ms 15952 KB Output is correct
40 Correct 863 ms 17096 KB Output is correct
41 Correct 842 ms 17120 KB Output is correct
42 Correct 911 ms 16984 KB Output is correct
43 Correct 894 ms 17128 KB Output is correct
44 Correct 876 ms 17088 KB Output is correct
45 Correct 45 ms 10712 KB Output is correct
46 Correct 60 ms 10948 KB Output is correct
47 Correct 853 ms 17140 KB Output is correct
48 Correct 915 ms 17096 KB Output is correct
49 Execution timed out 1014 ms 17108 KB Time limit exceeded
50 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6484 KB Output is correct
2 Correct 15 ms 6504 KB Output is correct
3 Correct 10 ms 6472 KB Output is correct
4 Correct 14 ms 6484 KB Output is correct
5 Correct 5 ms 6228 KB Output is correct
6 Correct 5 ms 6228 KB Output is correct
7 Correct 14 ms 6484 KB Output is correct
8 Correct 14 ms 6452 KB Output is correct
9 Correct 17 ms 6484 KB Output is correct
10 Correct 14 ms 6484 KB Output is correct
11 Correct 18 ms 6524 KB Output is correct
12 Correct 22 ms 6484 KB Output is correct
13 Correct 7 ms 6484 KB Output is correct
14 Correct 7 ms 6484 KB Output is correct
15 Correct 9 ms 6484 KB Output is correct
16 Correct 12 ms 6484 KB Output is correct
17 Correct 15 ms 6604 KB Output is correct
18 Correct 17 ms 6484 KB Output is correct
19 Correct 11 ms 6480 KB Output is correct
20 Correct 12 ms 6484 KB Output is correct
21 Correct 15 ms 6484 KB Output is correct
22 Correct 14 ms 6484 KB Output is correct
23 Correct 12 ms 6520 KB Output is correct
24 Correct 14 ms 6484 KB Output is correct
25 Correct 5 ms 6228 KB Output is correct
26 Correct 5 ms 6256 KB Output is correct
27 Correct 14 ms 6484 KB Output is correct
28 Correct 14 ms 6520 KB Output is correct
29 Correct 16 ms 6520 KB Output is correct
30 Correct 14 ms 6504 KB Output is correct
31 Correct 15 ms 6520 KB Output is correct
32 Correct 18 ms 6484 KB Output is correct
33 Correct 8 ms 6484 KB Output is correct
34 Correct 7 ms 6484 KB Output is correct
35 Correct 11 ms 6476 KB Output is correct
36 Correct 13 ms 6520 KB Output is correct
37 Correct 10 ms 6448 KB Output is correct
38 Correct 12 ms 6484 KB Output is correct
39 Correct 859 ms 15952 KB Output is correct
40 Correct 863 ms 17096 KB Output is correct
41 Correct 842 ms 17120 KB Output is correct
42 Correct 911 ms 16984 KB Output is correct
43 Correct 894 ms 17128 KB Output is correct
44 Correct 876 ms 17088 KB Output is correct
45 Correct 45 ms 10712 KB Output is correct
46 Correct 60 ms 10948 KB Output is correct
47 Correct 853 ms 17140 KB Output is correct
48 Correct 915 ms 17096 KB Output is correct
49 Execution timed out 1014 ms 17108 KB Time limit exceeded
50 Halted 0 ms 0 KB -