Submission #562116

# Submission time Handle Problem Language Result Execution time Memory
562116 2022-05-14T05:39:45 Z kartel Distributing Candies (IOI21_candies) C++17
8 / 100
1603 ms 74564 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;
    int pos_mn, pos_mx;

    node() {mn = 2e18; mx = -2e18; D = 0;}
    node(ll x, int i) {mn = mx = x; pos_mn = pos_mx = i; D = 0;}

    void mrg(node &l, node &r) {
        if (l.mn < r.mn) {
            mn = l.mn;
            pos_mn = l.pos_mn;
        } else {
            mn = r.mn;
            pos_mn = r.pos_mn;
        }
        if (l.mx > r.mx) {
            mx = l.mx;
            pos_mx = l.pos_mx;
        } else {
            mx = r.mx;
            pos_mx = r.pos_mx;
        }
    }
};

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

    void build(int v, int l, int r) {
        if (l == r) {
            t[v] = node(0, l);
        } else {
            int md = (l + r) >> 1;
            build(v * 2, l, md);
            build(v * 2 + 1, md + 1, r);
            t[v].mrg(t[v * 2], t[v * 2 + 1]);
        }
    }

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

    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(0, -1e9);
        }

        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(0, -1e9);
        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;
        }
        int pos_mn = sf.pos_mn;
        int pos_mx = sf.pos_mx;

        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 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1483 ms 74564 KB Output is correct
2 Correct 1473 ms 74384 KB Output is correct
3 Correct 1603 ms 74400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -