Submission #444490

# Submission time Handle Problem Language Result Execution time Memory
444490 2021-07-14T07:37:04 Z palilo Snowball (JOI21_ho_t2) C++17
100 / 100
138 ms 9528 KB
#include <bits/stdc++.h>
using namespace std;

template <class T>
bool chmin(T& _old, T _new) { return _old > _new && (_old = _new, true); }
template <class T>
bool chmax(T& _old, T _new) { return _old < _new && (_old = _new, true); }

#ifdef palilo
template <typename C, typename T = decay_t<decltype(*begin(declval<C>()))>,
    typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator<<(ostream& os, const C& container) {
    stringstream ss;
    ss << '[';
    bool first = true;
    for (const auto& x : container) {
        if (!first) ss << ", ";
        first = false;
        ss << x;
    }
    ss << ']';
    return os << ss.str();
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
    stringstream ss;
    ss << '(' << p.first << ", " << p.second << ')';
    return os << ss.str();
}

template <typename T>
void debug_msg(string name, T arg) {
    cerr << name << " = " << arg << endl;
}

template <typename T1, typename... T2>
void debug_msg(string names, T1 arg, T2... args) {
    cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
    debug_msg(names.substr(names.find(',') + 2), args...);
}

#define debug(...) cerr << '(' << __LINE__ << ')' << ' ', debug_msg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...)
#endif

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef palilo
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    int n, q;
    cin >> n >> q;
    vector<int64_t> a(n), w(q);
    for (auto& x : a) {
        cin >> x;
    }
    for (auto& x : w) {
        cin >> x;
    }
    partial_sum(w.begin(), w.end(), w.begin());
    debug(w);
    vector<int64_t> d;
    for (int i = 0, j; i != q;) {
        if (w[i] == 0) {
            ++i;
            continue;
        }
        const bool lhs = w[i] < 0;
        auto farthest = w[i];
        for (j = i + 1; j != q; ++j) {
            if (lhs) {
                if (w[j] > 0) {
                    break;
                }
                chmin(farthest, w[j]);
            } else {
                if (w[j] < 0) {
                    break;
                }
                chmax(farthest, w[j]);
            }
        }
        debug(d, farthest);
        i = j;
        if (!d.empty() && (d.back() < 0) == lhs) {
            if (lhs) {
                chmin(d.back(), farthest);
            } else {
                chmax(d.back(), farthest);
            }
            continue;
        }
        if (d.size() > 1 && (d.end()[-2] < 0) == lhs && abs(d.end()[-2]) >= abs(farthest)) {
            continue;
        }
        d.emplace_back(farthest);
    }
    if (d.empty()) {
        for (int i = 0; i < n; ++i) {
            cout << "0\n";
        }
        return 0;
    }
    if (d.size() == 1) {
        const bool lhs = d.front() < 0;
        const auto snow = abs(d.front());
        if (lhs) {
            cout << snow << '\n';
        }
        for (int i = 0; i < n - 1; ++i) {
            cout << min(snow, a[i + 1] - a[i]) << '\n';
        }
        if (!lhs) {
            cout << snow;
        }
        return 0;
    }
    auto binary = [&](int64_t val) {
        int lo = 0, hi = d.size() - 2;
        while (lo != hi) {
            const int mid = (lo + hi) >> 1;
            abs(d[mid]) + abs(d[mid + 1]) < val ? lo = mid + 1 : hi = mid;
        }
        return lo;
    };
    debug(d);
    vector<int64_t> ans(n);
    ans.front() -= min(d.end()[-2], d.end()[-1]);
    ans.back() += max(d.end()[-2], d.end()[-1]);
    for (int i = 0; i < n - 1; ++i) {
        auto gap = a[i + 1] - a[i];
        const auto it = binary(gap);
        if (d[it] < 0) {
            auto snow = min(gap, -d[it]);
            ans[i + 1] += snow, gap -= snow;
            ans[i] += min(gap, d[it + 1]);
        } else {
            auto snow = min(gap, d[it]);
            ans[i] += snow, gap -= snow;
            ans[i + 1] += min(gap, -d[it + 1]);
        }
    }
    for (const auto& x : ans) {
        cout << x << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 40 ms 4036 KB Output is correct
21 Correct 32 ms 4136 KB Output is correct
22 Correct 32 ms 4040 KB Output is correct
23 Correct 30 ms 4052 KB Output is correct
24 Correct 44 ms 4192 KB Output is correct
25 Correct 115 ms 8540 KB Output is correct
26 Correct 111 ms 8500 KB Output is correct
27 Correct 115 ms 8352 KB Output is correct
28 Correct 138 ms 8244 KB Output is correct
29 Correct 106 ms 7732 KB Output is correct
30 Correct 80 ms 6856 KB Output is correct
31 Correct 88 ms 6536 KB Output is correct
32 Correct 80 ms 4844 KB Output is correct
33 Correct 11 ms 1436 KB Output is correct
34 Correct 97 ms 7748 KB Output is correct
35 Correct 118 ms 7608 KB Output is correct
36 Correct 114 ms 8516 KB Output is correct
37 Correct 111 ms 8132 KB Output is correct
38 Correct 110 ms 8252 KB Output is correct
39 Correct 110 ms 8368 KB Output is correct
40 Correct 99 ms 8248 KB Output is correct
41 Correct 34 ms 2928 KB Output is correct
42 Correct 55 ms 4972 KB Output is correct
43 Correct 128 ms 9516 KB Output is correct
44 Correct 39 ms 5184 KB Output is correct
45 Correct 97 ms 8372 KB Output is correct
46 Correct 114 ms 9528 KB Output is correct
47 Correct 94 ms 7876 KB Output is correct
48 Correct 91 ms 6328 KB Output is correct