Submission #113092

# Submission time Handle Problem Language Result Execution time Memory
113092 2019-05-23T14:57:56 Z popovicirobert Construction of Highway (JOI18_construction) C++14
0 / 100
2 ms 384 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

struct Fenwick {
    vector <int> aib;
    int n;

    inline void init(int _n) {
        n = _n;
        aib.resize(n + 1);
    }

    inline void update(int pos, int val) {
        for(int i = pos; i <= n; i += lsb(i)) {
            aib[i] += val;
        }
    }

    inline int query(int pos) {
        int ans = 0;
        for(int i = pos; i > 0; i -= lsb(i)) {
            ans += aib[i];
        }
        return ans;
    }
};

struct SegTree {
    vector <int> aint;
    int n;

    inline void init(int _n) {
        n = _n;
        aint.resize(4 * n + 1, 0);
    }

    inline void refresh(int nod) {
        aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    }

    void update(int nod, int left, int right, int pos, int val) {
        if(left == right) {
            aint[nod] = val;
        }
        else {
            int mid = (left + right) / 2;

            if(pos <= mid) update(2 * nod, left, mid, pos, val);
            else update(2 * nod + 1, mid + 1, right, pos, val);

            refresh(nod);
        }
    }

    int query(int nod, int left, int right, int l, int r) {
        if(l <= left && right <= r) {
            return aint[nod];
        }
        else {
            int mid = (left + right) / 2;
            int ans = 0;

            if(l <= mid) ans = max(ans, query(2 * nod, left, mid, l, r));
            if(mid < r) ans = max(ans, query(2 * nod + 1, mid + 1, right, l, r));

            return ans;
        }
    }
};

vector < vector <int> > g, anc;
vector <int> idl, idr;
int sz, n;

void dfs(int nod, int par) {

    anc[nod][0] = par;
    for(int bit = 1; (1 << bit) <= n; bit++) {
        anc[nod][bit] = anc[anc[nod][bit - 1]][bit - 1];
    }

    idl[nod] = ++sz;
    for(auto it : g[nod]) {
        if(it != par) {
            dfs(it, nod);
        }
    }
    idr[nod] = sz;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    vector < pair <int, int> > aux(n + 1);

    for(i = 1; i <= n; i++) {
        cin >> aux[i].first;
        aux[i].second = i;
    }

    sort(aux.begin(), aux.end());

    vector <int> c(n + 1);
    int cur = 0;

    for(i = 1; i <= n; i++) {
        if(aux[i].first != aux[i - 1].first) {
            cur++;
        }
        c[aux[i].second] = cur;
    }

    vector < pair <int, int> > edges(n);
    g.resize(n + 1);

    for(i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        edges[i] = {x, y};
        g[x].push_back(y);
        g[y].push_back(x);
    }

    idl.resize(n + 1), idr.resize(n + 1);
    anc.resize(n + 1, vector <int>(18));

    dfs(1, 0);

    SegTree st; st.init(n);
    Fenwick fen; fen.init(n);

    st.update(1, 1, n, 1, 1);

    for(i = 1; i < n; i++) {

        int x = edges[i].first, y = edges[i].second;

        vector < pair <int, int> > vals;
        int nod = x;

        ll ans = 0;

        while(nod != 0) {

            int cur = st.query(1, 1, n, idl[nod], idr[nod]);
            int len = 1;

            for(int bit = 17; bit >= 0; bit--) {
                if(anc[nod][bit] > 0 && st.query(1, 1, n, idl[anc[nod][bit]], idr[anc[nod][bit]]) == cur) {
                    nod = anc[nod][bit];
                    len += (1 << bit);
                }
            }

            ans += 1LL * len * fen.query(c[cur] - 1);

            fen.update(c[cur], len);
            vals.push_back({c[cur], len});

            nod = anc[nod][0];
        }

        cout << ans << "\n";

        for(auto it : vals) {
            fen.update(it.first, -it.second);
        }

        st.update(1, 1, n, idl[y], i + 1);
    }

    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -