Submission #558846

# Submission time Handle Problem Language Result Execution time Memory
558846 2022-05-08T18:25:17 Z teraqqq Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 52916 KB
#include "candies.h"

#include <iostream>
#include <vector>

using namespace std;
using vi = vector<int>;
using ll = long long;

const ll LINF = 1e18;

struct SegmentTreeMin {
    int n;
    vector<pair<ll, int>> tmin;
    vector<ll> tadd;

    SegmentTreeMin(int n) : n(n), tmin(4*n), tadd(4*n) { }

    void build(int v, int vl, int vr, const vector<int>& w) {
        tadd[v] = 0;

        if (vl + 1 == vr) {
            tmin[v] = { w[vl], vl };
        } else {
            int vm = (vl + vr) / 2;
            build(2*v, vl, vm, w);
            build(2*v+1, vm, vr, w);
            tmin[v] = min(tmin[2*v], tmin[2*v+1]);
        }
    }

    void build(const vector<int>& w) {
        build(1, 0, n, w);
    }

    void add(int v, int vl, int vr, int l, int r, ll x) {
        if (l <= vl && vr <= r) {
            tadd[v]       += x;
            tmin[v].first += x;
        } else if (r <= vl || vr <= l) {
            /* nope :D */
        } else {
            int vm = (vl + vr) / 2;
            add(2*v,   vl, vm, l, r, x);
            add(2*v+1, vm, vr, l, r, x);
            tmin[v] = min(tmin[2*v], tmin[2*v+1]);
            tmin[v].first += tadd[v];
        }
    }

    void add(int l, int r, ll x) {
        add(1, 0, n, l, r, x);
    }

    pair<ll, int> query() {
        return tmin[1];
    }
};

vi distribute_candies(vi c, vi l, vi r, vi v) {
    const int n = c.size(), q = v.size();

    for (int& x : r) ++x;

    vector<int> ans_l(n), ans_r(n);
    for (int i = n; i--; ) ans_r[i] = c[i] + 1;

    SegmentTreeMin sgt_lb(n), sgt_ub(n);
    vector<int> lb_out(n), ub_out(n), ub_outh(n);
    vector<ll> lb_outx(n), ub_outx(n);

    for (int iter = 30; iter--; ) {
        vector<int> wlb(n), wrb(n);
        for (int i = n; i--; ) {
            wlb[i] = (ans_l[i] + ans_r[i]) / 2;
            wrb[i] = c[i] - wlb[i];
        }

        sgt_lb.build(wlb);
        sgt_ub.build(wrb);

        fill(lb_out.begin(), lb_out.end(), -1);
        fill(ub_out.begin(), ub_out.end(), -1);
        fill(ub_outh.begin(), ub_outh.end(), -1);

        for (int j = q; j >= 0; j--) {
            if (j != q) {
                sgt_lb.add(l[j], r[j], -v[j]);
                sgt_ub.add(l[j], r[j], +v[j]);
            }

            while (true) {
                const auto [val, i] = sgt_lb.query();
                if (val > 0) break;
                lb_outx[i] = val;
                lb_out[i] = j;
                sgt_lb.add(i, i+1, LINF);
            }

            while (true) {
                const auto [val, i] = sgt_ub.query();
                if (val > 0) break;
                ub_outx[i] = val;
                ub_out[i] = j;
                sgt_ub.add(i, i+1, LINF);
            }
        }

        sgt_ub.build(wrb);

        for (int j = q; j >= 0; j--) {
            if (j != q) {
                sgt_ub.add(l[j], r[j], +v[j]);
            }

            while (true) {
                const auto [val, i] = sgt_ub.query();
                if (val >= 0) break;
                ub_outh[i] = j;
                sgt_ub.add(i, i+1, LINF);
            }
        }

        for (int i = 0; i < n; ++i) {
            if (lb_out[i] == -1 && ub_out[i] == -1) {
                ans_r[i] = wlb[i];
            } else {
                if (lb_out[i] > ub_out[i]) {
                    ans_l[i] = wlb[i];
                } else {
                    if (ub_outx[i] == 0) {
                        if (lb_out[i] > ub_outh[i]) {
                            ans_l[i] = wlb[i];
                        } else {
                            ans_r[i] = wlb[i];
                        }
                    } else {
                        ans_r[i] = wlb[i];
                    }
                }
            }
        }
    }

    return ans_l;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 29 ms 372 KB Output is correct
5 Correct 92 ms 796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5094 ms 52916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 468 KB Output is correct
2 Execution timed out 5079 ms 5352 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 18 ms 468 KB Output is correct
3 Correct 182 ms 5468 KB Output is correct
4 Execution timed out 5052 ms 48456 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 29 ms 372 KB Output is correct
5 Correct 92 ms 796 KB Output is correct
6 Execution timed out 5094 ms 52916 KB Time limit exceeded
7 Halted 0 ms 0 KB -