USACO 2020 Gold Open P1: Cow Haircut

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

ll N;
vector<ll> hairs, tree;

void update(ll i, ll x) {
    while (i < tree.size()) {
        tree[i] += x;
        i = i + (i & (-i));
    }
}

ll query(ll i) {
    ll ans = 0;
    while (i > 0) {
        ans += tree[i];
        i = i - (i & (-i));
    }
    return ans;
}

int main() {
    ifstream cin("haircut.in");
    ofstream cout("haircut.out");

    cin >> N;

    hairs.resize(N + 1);
    tree.resize(N + 2);

    vector<int> inverse(N + 1);
    for (int i = 1; i < N + 1; i++) {
        cin >> hairs[i];
        hairs[i]++;
        // i - 1 = values before, also query(N + 1)
        inverse[i] = i - 1 - query(hairs[i]);
        update(hairs[i], 1);
    }

    vector<int> order(N);
    // get new order
    iota(order.begin(), order.end(), 1);
    sort(order.begin(), order.end(), [](int a, int b) {return hairs[a] < hairs[b];});

    ll ans = 0;
    int i = 0;
    cout << ans << endl;
    for(int j = 1; j < N; j++) {
        while (i < N && hairs[order[i]] <= j) {
            ans += inverse[order[i]];
            i++;
        }
        cout << ans << endl;
    }
}