Submission #1280576

#TimeUsernameProblemLanguageResultExecution timeMemory
1280576quangminh412Food Court (JOI21_foodcourt)C++17
100 / 100
436 ms40060 KiB
/*
  Ben Watson

  Quang Minh MasterDDDDD
*/

#include <bits/stdc++.h>
using namespace std;

#define ll long long

const string name = "test";

void solve();
signed main()
{
    if (fopen((name + ".inp").c_str(), "r"))
    {
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    solve();

    return 0;
}

// main program
const ll oo = 0x3f3f3f3f3f3f3f3f;
const int maxn = 25e4 + 5;

struct SegmentTree
{
    struct Node
    {
        ll mn, lazy;
        int idx;

        Node() : mn(oo), idx(-1), lazy(0) {};
        Node(ll mn, int idx) : mn(mn), idx(idx), lazy(0) {};
        Node operator + (const Node& other)
        {
            Node res;
            if (idx == -1) return other;
            if (other.idx == -1) return (*this);

            res.mn = min(mn, other.mn);
            res.idx = (res.mn == mn ? idx : other.idx);
            return res;
        }
    };
    vector<Node> st;
    int n;

    SegmentTree(int n, vector<pair<ll, int>>& a) : n(n)
    {
        st.resize(4 * n + 1);
        build(1, 1, n, a);
    }

    void build(int head, int l, int r, vector<pair<ll, int>>& a)
    {
        if (l == r)
        {
            st[head] = Node(a[l].first, a[l].second);
            return;
        }
        int mid = l + r >> 1;
        build(head << 1, l, mid, a);
        build(head << 1 | 1, mid + 1, r, a);
        st[head] = st[head << 1] + st[head << 1 | 1];
    }

    void pass(int head, int l, int r)
    {
        if (st[head].lazy == 0)
            return;
        st[head].mn += st[head].lazy;
        if (l != r)
        {
            st[head << 1].lazy += st[head].lazy;
            st[head << 1 | 1].lazy += st[head].lazy;
        }
        st[head].lazy = 0;
    }

    void update(int head, int l, int r, int u, int v, ll val)
    {
        pass(head, l, r);
        if (l > v || r < u) return;
        if (u <= l && r <= v)
        {
            st[head].lazy += val;
            pass(head, l, r);
            return;
        }
        int mid = l + r >> 1;
        update(head << 1, l, mid, u, v, val);
        update(head << 1 | 1, mid + 1, r, u, v, val);
        st[head] = st[head << 1] + st[head << 1 | 1];
    }
    void update(int u, int v, ll val) { update(1, 1, n, u, v, val); }

    Node query(int head, int l, int r, int u, int v)
    {
        pass(head, l, r);
        if (l > v || r < u) return Node();
        if (u <= l && r <= v) return st[head];
        int mid = l + r >> 1;
        return query(head << 1, l, mid, u, v) + query(head << 1 | 1, mid + 1, r, u, v);
    }
    pair<ll, int> query(int u, int v) { Node res = query(1, 1, n, u, v); return {res.mn, res.idx}; }
};

struct SegmentTreeBeats
{
    struct Node
    {
        ll cap, add;

        Node() : cap(0), add(0) {};
    };
    vector<Node> st;
    int n;

    SegmentTreeBeats(int n) : n(n) { st.resize(4 * n + 1); }

    void apply(int head, ll c, ll a)
    {
        st[head].cap = min(st[head].cap + a, c);
        st[head].add += a;
    }

    void pass(int head)
    {
        apply(head << 1, st[head].cap, st[head].add);
        apply(head << 1 | 1, st[head].cap, st[head].add);
        st[head].cap = oo;
        st[head].add = 0;
    }

    void update(int head, int l, int r, int u, int v, ll val)
    {
        if (l > v || r < u) return;
        if (u <= l && r <= v)
        {
            apply(head, oo, val);
            return;
        }
        pass(head);
        int mid = l + r >> 1;
        update(head << 1, l, mid, u, v, val);
        update(head << 1 | 1, mid + 1, r, u, v, val);
    }
    void update(int u, int v, ll val)
    {
        update(1, 1, n, u, v, val);
        apply(1, 0, 0);
    }

    ll query(int head, int l, int r, int pos)
    {
        if (pos < l || r < pos) return -oo;
        if (l == r) return st[head].cap;
        pass(head);
        int mid = l + r >> 1;
        return max(query(head << 1, l, mid, pos), query(head << 1 | 1, mid + 1, r, pos));
    }
    ll query(int pos) { return query(1, 1, n, pos); }
};

struct FenwickTree
{
    vector<ll> bits;
    int n;

    FenwickTree(int n) : n(n) { bits.resize(n + 1, 0); }

    void update(int pos, ll val)
    {
        for (; pos <= n; pos += (pos & (-pos)))
            bits[pos] += val;
    }
    void update(int l, int r, ll val)
    {
        update(l, val);
        update(r + 1, -val);
    }

    ll query(int pos)
    {
        ll res = 0;
        for (; pos > 0; pos -= (pos & (-pos)))
            res += bits[pos];
        return res;
    }
};

vector<pair<int, int>> proc;
int ans[maxn], rev[maxn];
ll idx[maxn];

int L[maxn], R[maxn], C[maxn], K[maxn], A[maxn], type[maxn];
ll B[maxn];
int n, m, q;

void solve()
{
    cin >> n >> m >> q;
    for (int i = 1; i <= q; i++)
    {
        cin >> type[i];
        if (type[i] == 1)
            cin >> L[i] >> R[i] >> C[i] >> K[i];
        else
        if (type[i] == 2)
            cin >> L[i] >> R[i] >> K[i];
        else
            cin >> A[i] >> B[i];
    }

    FenwickTree bit(n);
    SegmentTreeBeats seg(n);
    for (int i = 1; i <= q; i++)
    {
        if (type[i] == 1)
        {
            seg.update(L[i], R[i], -K[i]);
            bit.update(L[i], R[i], K[i]);
        }
        else
        if (type[i] == 2)
            seg.update(L[i], R[i], K[i]);
        else
        {
            ll cur = -seg.query(A[i]);
            idx[i] = (cur < B[i] ? -1 : bit.query(A[i]) - (cur - B[i]));
            if (idx[i] != -1)
                proc.push_back({A[i], i});
        }
    }

    sort(proc.begin(), proc.end());
    vector<pair<ll, int>> transfer;
    transfer.push_back({0, 0});
    int N = 0;
    vector<int> save;
    for (pair<int, int> it : proc)
    {
        N++;
        rev[N] = it.second;
        save.push_back(it.first);
        transfer.push_back({idx[it.second], N});
    }

    if (N != 0)
    {
        SegmentTree st(N, transfer);
        for (int i = 1; i <= q; i++)
        {
            if (type[i] != 1)
                continue;

            int posR = upper_bound(save.begin(), save.end(), R[i]) - save.begin();
            if (posR == 0)
                continue;
            int posL = lower_bound(save.begin(), save.end(), L[i]) - save.begin();
            posL++;
            st.update(posL, posR, -K[i]);
            while (1)
            {
                pair<ll, int> tmp = st.query(posL, posR);
                if (tmp.first <= 0)
                {
                    int id = tmp.second;
                    ans[rev[id]] = C[i];
                    st.update(id, id, oo);
                } else
                    break;
            }
        }
    }

    for (int i = 1; i <= q; i++)
        if (type[i] == 3)
            cout << ans[i] << '\n';
}

Compilation message (stderr)

foodcourt.cpp: In function 'int main()':
foodcourt.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen((name + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foodcourt.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...