| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1283706 | thaibeo123 | Construction of Highway (JOI18_construction) | C++20 | 2095 ms | 2728 KiB | 
#include <bits/stdc++.h>
using namespace std;
#define NAME "A"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define MASK(x) (1ll << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
const int N = 2e5 + 5;
struct FenwickTree {
    int n;
    vector<int> fen;
    FenwickTree() {}
    FenwickTree(int n): n(n), fen(n + 1) {}
    void update(int x, int v) {
        for (int i = x; i <= n; i += i & -i) {
            fen[i] += v;
        }
    }
    int get(int x) {
        int res = 0;
        for (int i = x; i > 0; i -= i & -i) {
            res += fen[i];
        }
        return res;
    }
};
FenwickTree fen;
int n;
int c[N], a[N], p[N];
vector<int> vals;
int getans(int u) {
    int ans = 0;
    while (u != 0) {
        ans += fen.get(a[u] - 1);
        //cout << u << " " << a[u] << " " << fen.get(a[u]) << "\n";
        fen.update(a[u], 1);
        u = p[u];
    }
    return ans;
}
void input() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        vals.pb(c[i]);
    }
    sort(all(vals));
    vals.erase(unique(all(vals)), vals.end());
    for (int i = 1; i <= n; i++) {
        c[i] = upper_bound(all(vals), c[i]) - vals.begin();
        a[i] = c[i];
    }
}
void solve() {
    fen = FenwickTree(vals.size());
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        p[v] = u;
        int ans = getans(u);
        while (u != 0) {
            fen.update(a[u], -1);
            u = p[u];
        }
        int cost = c[v];
        while (v != 0) {
            a[v] = cost;
            v = p[v];
        } 
        cout << ans << "\n";
    }
}
signed main() {
    if (fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        input();
        solve();
    }
    return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
