Submission #562109

# Submission time Handle Problem Language Result Execution time Memory
562109 2022-05-14T05:33:22 Z kartel Distributing Candies (IOI21_candies) C++17
100 / 100
2653 ms 68868 KB
#include <bits/stdc++.h>

//#include "grader.cpp"
#include "candies.h"

#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#define el "\n"
#define all(x) (x).begin(), (x).end()

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")

using namespace std;
typedef long long ll;

struct node {
    ll mn, mx, D;

    node() {mn = 2e18; mx = -2e18; D = 0;}
    node(ll x) {mn = mx = x; D = 0;}

    void mrg(node &l, node &r) {
        mn = min(l.mn, r.mn);
        mx = max(l.mx, r.mx);
    }
};

struct seg_tree {
    vector <node> t;
    int n;

    seg_tree() {}
    seg_tree(int _n) {
        n = _n;
        t.resize(8 * n, node(0));
    }

    void push(int v) {
        if (t[v].D) {
            t[v].mn += t[v].D;
            t[v].mx += t[v].D;
            t[v * 2].D += t[v].D;
            t[v * 2 + 1].D += t[v].D;
            t[v].D = 0;
        }
    }

    void upd(int v, int l, int r, int tl, int tr, ll val) {
        push(v);
        if (l > r || tl > tr || tl > r || l > tr) {
            return;
        }
        if (l >= tl && r <= tr) {
            t[v].D += val;
            push(v);
            return;
        }
        int md = (l + r) >> 1;
        upd(v * 2, l, md, tl, tr, val);
        upd(v * 2 + 1, md + 1, r, tl, tr, val);

        t[v].mrg(t[v * 2], t[v * 2 + 1]);
    }

    node get(int v, int l, int r, int tl, int tr) {
        push(v);
        if (l > r || tl > tr || tl > r || l > tr) {
            return node();
        }

        if (l >= tl && r <= tr) {
            return t[v];
        }
        int md = (l + r) >> 1;
        node L = get(v * 2, l, md, tl, tr);
        node R = get(v * 2 + 1, md + 1, r, tl, tr);
        node cur = node();
        cur.mrg(L, R);
        return cur;
    }

    void upd(int l, int r, ll val) {
        upd(1, 0, n - 1, l, r, val);
    }

    node get(int l, int r) {
        return get(1, 0, n - 1, l, r);
    }
};

vector <int> distribute_candies(vector <int> c, vector <int> l, vector <int> r, vector <int> v) {
    int n = sz(c), q = sz(l);
    vector <vector <int> > add(n + 1), del(n + 1);

    for (int i = 0; i < q; i++) {
        add[l[i]].pb(i + 1);
        del[r[i] + 1].pb(i + 1);
    }

    seg_tree t(q + 1);
    vector <int> ans;
    for (int i = 0; i < n; i++) {
        for (auto &id : add[i]) {
            t.upd(id, q, v[id - 1]);
        }
        for (auto &id : del[i]) {
            t.upd(id, q, -v[id - 1]);
        }

        int l = 0;
        int r = q;
        while (l < r) {
            int md = (l + r + 1) >> 1;
            node cur = t.get(md, q);

            if (cur.mx - cur.mn >= c[i]) {
                l = md;
            } else {
                r = md - 1;
            }
        }

        int suf = r;
        node sf = t.get(suf, q);
        if (sf.mx - sf.mn < c[i]) {
            ans.pb(t.get(q, q).mx - sf.mn);
            continue;
        }


        l = 0; r = q;
        while (l < r) {
            int md = (l + r + 1) >> 1;
            node cur = t.get(md, q);

            if (cur.mn <= sf.mn) {
                l = md;
            } else {
                r = md - 1;
            }
        }
        int pos_mn = r;

        l = 0; r = q;
        while (l < r) {
            int md = (l + r + 1) >> 1;
            node cur = t.get(md, q);

            if (cur.mx >= sf.mx) {
                l = md;
            } else {
                r = md - 1;
            }
        }
        int pos_mx = r;

        if (pos_mx >= pos_mn) {
            ll val = t.get(q, q).mx;
            ans.pb(val - sf.mx + c[i]);
        } else {
            ans.pb(t.get(q, q).mx - sf.mn);
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 700 KB Output is correct
5 Correct 10 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2401 ms 62044 KB Output is correct
2 Correct 2420 ms 61864 KB Output is correct
3 Correct 2179 ms 61988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 273 ms 48724 KB Output is correct
3 Correct 594 ms 15120 KB Output is correct
4 Correct 2218 ms 67948 KB Output is correct
5 Correct 2318 ms 68320 KB Output is correct
6 Correct 2288 ms 68868 KB Output is correct
7 Correct 2309 ms 68256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 145 ms 44344 KB Output is correct
4 Correct 613 ms 14012 KB Output is correct
5 Correct 1548 ms 60148 KB Output is correct
6 Correct 1696 ms 60972 KB Output is correct
7 Correct 2137 ms 61368 KB Output is correct
8 Correct 1733 ms 59976 KB Output is correct
9 Correct 2064 ms 61468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 700 KB Output is correct
5 Correct 10 ms 852 KB Output is correct
6 Correct 2401 ms 62044 KB Output is correct
7 Correct 2420 ms 61864 KB Output is correct
8 Correct 2179 ms 61988 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 273 ms 48724 KB Output is correct
11 Correct 594 ms 15120 KB Output is correct
12 Correct 2218 ms 67948 KB Output is correct
13 Correct 2318 ms 68320 KB Output is correct
14 Correct 2288 ms 68868 KB Output is correct
15 Correct 2309 ms 68256 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 145 ms 44344 KB Output is correct
19 Correct 613 ms 14012 KB Output is correct
20 Correct 1548 ms 60148 KB Output is correct
21 Correct 1696 ms 60972 KB Output is correct
22 Correct 2137 ms 61368 KB Output is correct
23 Correct 1733 ms 59976 KB Output is correct
24 Correct 2064 ms 61468 KB Output is correct
25 Correct 1 ms 304 KB Output is correct
26 Correct 491 ms 14164 KB Output is correct
27 Correct 284 ms 48088 KB Output is correct
28 Correct 2243 ms 66428 KB Output is correct
29 Correct 2400 ms 66708 KB Output is correct
30 Correct 2653 ms 67004 KB Output is correct
31 Correct 2587 ms 67204 KB Output is correct
32 Correct 2565 ms 67448 KB Output is correct