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 : Set Add Query

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

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

    vector<long long> a(n);

    // Key is Index. Value is the initial value that the element starts with.
    map<int, long long> inside;
    long long delta = 0;
    for (int zz = 0; zz < q; zz++) {
        int x;
        cin >> x;
        x--;

        // Missing from the set. Add it with a delta offset.
        if (inside.find(x) == inside.end()) {
            inside[x] = a[x] - delta;
        } else {
            // Remove it from the set and remember the current value.
            a[x] = inside[x] + delta;
            inside.erase(x);
        }
        delta += (int)inside.size();
    }

    for (auto &[x, initial] : inside) {
        a[x] = initial + delta;
    }

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

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