Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Mancala 2

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;

    vector<long long> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    for (int i = 0; i < m; i++) {
        long long total = a[b[i]], equal_split = total / n, remain = total % n;
        a[b[i]] = 0;
        for (int j = 0; j < n; j++) {
            a[j] += equal_split;
        }
        for (int j = (b[i] + 1) % n; remain > 0; j = (j + 1) % n) {
            a[j]++;
            remain--;
        }
    }

    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

int main() {
    solve();
    return 0;
}
#include <atcoder/segtree>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

long long op(long long a, long long b) { return a + b; }

long long e() { return 0; }

class BlackBox {
  public:
    segtree<long long, op, e> *diff_array;
    BlackBox(int n) { diff_array = new segtree<long long, op, e>(n); }

    void range_inc(int l, int r, long long delta) {
        diff_array->set(l, diff_array->get(l) + delta);
        diff_array->set(r + 1, diff_array->get(r + 1) - delta);
    }

    long long point_query(int idx) {
        // Atcoder's segtree prod function has range [l, r).
        // Hence, we add + 1 to make inclusive range.
        return diff_array->prod(0, idx + 1);
    }
};

void solve() {
    int n, m;
    cin >> n >> m;

    vector<long long> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    BlackBox box(n + 5);
    for (int i = 0; i < n; i++) {
        box.range_inc(i, i, a[i]);
    }
    for (int i = 0; i < m; i++) {
        long long total = box.point_query(b[i]), equal_split = total / n,
                  remain = total % n;
        box.range_inc(b[i], b[i], -total);
        box.range_inc(0, n - 1, equal_split);

        int start = (b[i] + 1) % n;
        if (start + remain - 1 < n) {
            box.range_inc(start, start + remain - 1, 1);
        } else {
            box.range_inc(start, n - 1, 1);
            remain -= (n - 1 - start + 1);
            box.range_inc(0, 0 + remain - 1, 1);
        }
    }

    for (int i = 0; i < n; i++) {
        cout << box.point_query(i) << " ";
    }
    cout << endl;
}

int main() {
    solve();
    return 0;
}