Submission #882557

#TimeUsernameProblemLanguageResultExecution timeMemory
882557viwlesxqConstruction of Highway (JOI18_construction)C++17
16 / 100
2067 ms5436 KiB
//     ↘ ⬇ ⬇ ⬇ ⬇ ⬇ ↙
//     ➡  IOI, IZhO ⬅
//     ↗ ⬆ ⬆ ⬆ ⬆ ⬆ ↖
//⠀⠀⢠⣤⡄⢠⣤⣤⣤⣤⣤⣤⡄⢠⣤⡄⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠙⠃⠘⠛⠛⠛⠛⠛⠛⠃⠘⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀  ⠘⠛⠛⠛⠛⠛⠛⠛⠛⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⣠⣶⣶⠂⣰⣶⣶⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⢀⣾⣿⣿⡏⢠⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⡈⠻⣿⣿⣶⣾⣿⣿⣿⣿⠿⠋⡀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⣿⣶⣄⠙⣿⣿⣿⣿⣟⢁⣴⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⢹⣿⡏⢠⣿⠟⠛⢿⣿⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠙⠀⠀⣠⣶⣷⣤⡈⠛⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠋⠉⠀⠀


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
#define pb push_back
#define eb emplace_back

template<typename S>
bool chmin(S &a, const S b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename S>
bool chmax(S &a, const S b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const double eps = 1e-6;
const double EPS = 1e-9;

vector<int> path, g[100000];

struct Fenwick_Tree {
    int n;
    vector<int> t;
    Fenwick_Tree(int n) {
        this->n = n;
        t.assign(n, 0);
    }
    void upd(int i, int x) {
        for (; i < n; i = i | (i + 1)) {
            t[i] += x;
        }
    }
    int get(int i) {
        int res = 0;
        for (; i >= 0; i = (i & (i + 1)) - 1) {
            res += t[i];
        }
        return res;
    }
};

void compress(vector<int> &c) {
    vector<int> v;
    unordered_map<int, int> mp;
    for (int i = 0; i < size(c); ++i) {
        if (!mp.count(c[i])) {
            v.pb(c[i]);
            mp[c[i]];
        }
    }
    sort(all(v));
    for (int i = 0; i < size(v); ++i) {
        mp[v[i]] = i;
    }
    for (int i = 0; i < size(c); ++i) {
        c[i] = mp[c[i]];
    }
}

bool dfs(int v, int p, int f) {
    if (v == f) {
        path.pb(v);
        return true;
    }
    for (int to : g[v]) {
        if (to == p) {
            continue;
        }
        if (dfs(to, v, f)) {
            path.pb(v);
            return true;
        }
    }
    return false;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> c(n);
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }
    compress(c);
    Fenwick_Tree t(100000);
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        a--, --b;
        path.clear();
        dfs(0, -1, a);
        reverse(all(path));
        t.upd(c[path[0]], 1);
        g[a].pb(b);
        g[b].pb(a);
        int res = 0;
        for (int j = 1; j < size(path); ++j) {
            t.upd(c[path[j]], 1);
            res += j + 1 - t.get(c[path[j]]);
        }
        cout << res << '\n';
        for (int j = 0; j < size(path); ++j) {
            t.upd(c[path[j]], -1);
            c[path[j]] = c[b];
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...