답안 #437568

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
437568 2021-06-26T14:46:39 Z blue 사탕 분배 (IOI21_candies) C++17
27 / 100
815 ms 49392 KB
#include "candies.h"
// #include <vector>
using namespace std;

/*
Consider 'machines' of √Q updates.

Each machine has three numbers a, b and c such that
-> If <= a candies are inserted, a+c candies are obtained.
-> If >= b candies are inserted, b+c candies are obtained.
-> If I candies are inserted, where a <= I <= b, I+c candies are obtained.

To compute the numbers of a machine:
- Pass 0 through the machine to get l
- Pass INF through the machine to get r
- r-l = b-a


*/
int lim; //GOOD

struct machine //GOOD
{
    int a;
    int b;
    int c;

    int res(int u) //GOOD
    {
        if(u < a) return a+c;
        else if(a <= u && u <= b) return u+c;
        else return b+c;
    }
};

machine get_machine(int v)
{
    int a, b, c;
    c = v;
    if(v > 0)
    {
        a = 0;
        b = max(0, lim - v);
    }
    else
    {
        a = min(-v, lim);
        b = lim;
    }
    return machine{a, b, c};
}

machine empty_machine()
{
    return machine{0, lim, +0};
}

machine combine(machine A, machine B)
{
    if(A.b + A.c < B.a || A.a + A.c > B.b) return machine{0, 0, B.res(A.res(0))};
    else return machine{max(A.a, B.a - A.c), min(A.b, B.b - A.c), A.c + B.c};
}

struct segtree
{
    int l;
    int r;
    machine x = empty_machine();

    segtree* left = NULL;
    segtree* right = NULL;

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        if(l == r) return;
        int m = (l+r)/2;
        left = new segtree(l, m);
        right = new segtree(m+1, r);
    }

    void update(int I, machine X)
    {
        if(I < l || r < I) return;
        else if(l == r) x = X;
        else
        {
            left->update(I, X);
            right->update(I, X);
            x = combine(left->x, right->x);
        }
    }

    int pass_input(int H)
    {
        return x.res(H);
    }
};

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v)
{
    int N = c.size();
    int Q = l.size();

    lim = c[0];

    segtree S(0, Q);

    vector<int> insertions[N], removals[N];
    for(int i = 0; i < Q; i++)
    {
        insertions[ l[i] ].push_back(i);
        removals[ r[i] ].push_back(i);
    }

    vector<int> res(N, 0);
    for(int i = 0; i < N; i++)
    {
        for(int o: insertions[i])
        {
            S.update(o, get_machine(v[o]));
        }

        res[i] = S.pass_input(0);

        for(int o: removals[i])
        {
            S.update(o, empty_machine());
        }
    }

    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 693 ms 43720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 455 ms 27748 KB Output is correct
3 Correct 78 ms 14136 KB Output is correct
4 Correct 701 ms 45660 KB Output is correct
5 Correct 749 ms 49092 KB Output is correct
6 Correct 815 ms 49392 KB Output is correct
7 Correct 729 ms 48732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -