답안 #558847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
558847 2022-05-08T18:30:45 Z teraqqq 사탕 분배 (IOI21_candies) C++17
3 / 100
5000 ms 27948 KB
#include "candies.h"

#include <iostream>
#include <vector>

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

const ll LINF = 1e18;

const int N = 2e5+7;

int n;
pair<ll, int> tmin[4*N];
ll tadd[4*N];

struct SegmentTreeMin {
    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];
    }
} sgt;

vi distribute_candies(vi c, vi l, vi r, vi v) {
    const int q = v.size();
    n = c.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;

    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.build(wlb);

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

        sgt.build(wlb);
        for (int j = q; j >= 0; j--) {
            if (j != q) sgt.add(l[j], r[j], -v[j]);

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

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

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

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

            while (true) {
                const auto [val, i] = sgt.query();
                if (val >= 0) break;
                ub_outh[i] = j;
                sgt.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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 9 ms 360 KB Output is correct
4 Correct 27 ms 340 KB Output is correct
5 Correct 86 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5062 ms 27948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 340 KB Output is correct
2 Execution timed out 5063 ms 5096 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 14 ms 412 KB Output is correct
3 Correct 131 ms 5220 KB Output is correct
4 Execution timed out 5048 ms 23252 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 9 ms 360 KB Output is correct
4 Correct 27 ms 340 KB Output is correct
5 Correct 86 ms 536 KB Output is correct
6 Execution timed out 5062 ms 27948 KB Time limit exceeded
7 Halted 0 ms 0 KB -