Submission #445259

#TimeUsernameProblemLanguageResultExecution timeMemory
445259MilosMilutinovic사탕 분배 (IOI21_candies)C++17
40 / 100
147 ms16800 KiB
#include <bits/stdc++.h>

using ll = long long;

#define vi vector<int>

#define FOR(i,a,b) for (int i = a; i <= b; i++)
#define ROF(i,a,b) for (int i = a; i >= b; i--)

using namespace std;

const int MX = 2e5 + 5;
const int BF = 2e3 + 5;

ll pref[MX];
ll mx[MX], mn[MX];

vi distribute_candies(vi c, vi l, vi r, vi v) {
    int N = (int) c.size();
    int Q = (int) l.size();
    if (N <= BF && Q <= BF) {
        vi ans(N);
        FOR(i, 0, Q - 1) {
            FOR(j, l[i], r[i]) {
                ans[j] += v[i];
                ans[j] = max(ans[j], 0);
                ans[j] = min(ans[j], c[j]);
            }
        }
        return ans;
    }
    if (*min_element(v.begin(), v.end()) > 0) {
        FOR(i, 0, Q - 1) {
            pref[l[i]] += v[i];
            pref[r[i] + 1] -= v[i];
        }
        vi ans(N);
        ll sum = 0;
        FOR(i, 0, N - 1) {
            sum += pref[i];
            ans[i] = min((long long) c[i], sum);
        }
        return ans;
    }
    FOR(i,0,Q - 1) {
        if (i > 0) pref[i] = pref[i - 1];
        pref[i] += v[i];
    }
    ROF(i,Q - 1,0) {
        if (i == Q - 1) {
            mx[i] = pref[i];
            mn[i] = pref[i];
        } else {
            mx[i] = max(mx[i + 1], pref[i]);
            mn[i] = min(mn[i + 1], pref[i]);
        }
    }
    vi res(N);
    FOR(i,0,N - 1) {
        if (mx[0] - mn[0] <= c[i]) {
            res[i] = pref[Q - 1] - mn[0];
            continue;
        }
        int bot = 0, top = Q - 1, ans = 0;
        while (top - bot > 1) {
            int mid = bot + top >> 1;
            if (mx[mid] - mn[mid] > c[i]) ans = mid, bot = mid;
            else top = mid;
        }
        if (pref[bot] < pref[Q - 1]) res[i] = c[i] - (mx[bot] - pref[Q - 1]);
        else res[i] = pref[Q - 1] - mn[bot];
    }
    return res;
}

Compilation message (stderr)

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:66:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |             int mid = bot + top >> 1;
      |                       ~~~~^~~~~
candies.cpp:64:35: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
   64 |         int bot = 0, top = Q - 1, ans = 0;
      |                                   ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...