Submission #47516

# Submission time Handle Problem Language Result Execution time Memory
47516 2018-05-04T11:38:18 Z qoo2p5 Construction of Highway (JOI18_construction) C++17
7 / 100
1000 ms 6804 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;

#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back

bool mini(auto &x, const auto &y) {
    if (y < x) {
        x = y;
        return 1;
    }
    return 0;
}

bool maxi(auto &x, const auto &y) {
    if (y > x) {
        x = y;
        return 1;
    }
    return 0;
}

void run();

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    run();
    return 0;
}

const int N = (int) 1e5 + 123;

struct F {
    int f[N];
    
    void add(int x, int y) {
        x++;
        for (; x > 0; x -= x & (-x)) f[x] += y;
    }
    
    int get(int i) {
        i++;
        int res = 0;
        for (; i < N; i += i & (-i)) res += f[i];
        return res;
    }
};

int n;
int c[N];
map<int, int> comp;
vector<int> g[N];

struct HLD {
    int up, down;
    vector<pair<int, int>> st;
    
    void set(int len, int c) {
        int alr = 0;
        while (sz(st) && st.back().second + alr <= len) {
            alr += st.back().second;
            st.pop_back();
        }
        assert(alr <= len);
        if (alr < len) {
            st.back().second -= (len - alr);
        }
        st.pb({c, len});
    }
};

int ptr;
HLD hld[N];
int id[N];
int depth[N];
int cnt[N];
int p[N];
pair<int, int> q[N];

void precalc(int v) {
    cnt[v] = 1;
    for (int u : g[v]) {
        precalc(u);
        cnt[v] += cnt[u];
    }
}

void build_hld(int v, int cur = -1) {
    if (cur == -1) {
        cur = ptr++;
        hld[cur].up = v;
    }
    id[v] = cur;
    hld[cur].down = v;
    int to = -1;
    for (int u : g[v]) {
        if (to == -1 || cnt[u] > cnt[to]) {
            to = u;
        }
    }
    if (to != -1) {
        build_hld(to, cur);
    }
    for (int u : g[v]) {
        if (u != to) {
            build_hld(u, -1);
        }
    }
}

ll get(int v) {
    static vector<pair<int, int>> cool;
    cool.clear();
    while (v > 0) {
        int w = id[v];
        int len = depth[v] - depth[hld[w].up] + 1;
        int alr = 0;
        int ptr = sz(hld[w].st) - 1;
        while (ptr >= 0 && hld[w].st[ptr].second + alr <= len) {
            alr += hld[w].st[ptr].second;
            ptr--;
        }
        if (alr < len) {
            assert(ptr >= 0);
            cool.pb({hld[w].st[ptr].first, len - alr});
        }
        rep(i, ptr + 1, sz(hld[w].st)) {
            cool.pb(hld[w].st[i]);
        }
        v = p[hld[w].up];
    }
    
    vector<int> wtf;
    for (auto it : cool) {
        while (it.second > 0) {
            wtf.pb(it.first);
            it.second--;
        }
    }
    ll ans = 0;
    rep(i, 0, sz(wtf)) {
        rep(j, i + 1, sz(wtf)) {
            if (wtf[i] < wtf[j]) {
                ans++;
            }
        }
    }
    return ans;
}

void set_color(int v, int c) {
    while (v > 0) {
        int w = id[v];
        int len = depth[v] - depth[hld[w].up] + 1;
        hld[w].set(len, c);
        v = p[hld[w].up];
    }
}

void run() {
    cin >> n;
    rep(i, 1, n + 1) {
        cin >> c[i];
        comp[c[i]] = 1;
    }
    {
        int ptr = 0;
        for (auto &it : comp) {
            it.second = ptr++;
        }
    }
    rep(i, 1, n + 1) {
        c[i] = comp[c[i]];
    }
    rep(i, 1, n) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        p[b] = a;
        depth[b] = depth[a] + 1;
        q[i] = {a, b};
    }
    precalc(1);
    build_hld(1);
    rep(i, 0, ptr) {
        hld[i].st.pb({-1, depth[hld[i].down] - depth[hld[i].up] + 1});
    }
    
    set_color(1, c[1]);
    rep(i, 1, n) {
        cout << get(q[i].first) << "\n";
        set_color(q[i].second, c[q[i].second]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5884 KB Output is correct
2 Correct 5 ms 5884 KB Output is correct
3 Correct 7 ms 5920 KB Output is correct
4 Correct 6 ms 6100 KB Output is correct
5 Correct 6 ms 6100 KB Output is correct
6 Correct 6 ms 6100 KB Output is correct
7 Correct 6 ms 6100 KB Output is correct
8 Correct 26 ms 6124 KB Output is correct
9 Correct 26 ms 6124 KB Output is correct
10 Correct 27 ms 6280 KB Output is correct
11 Correct 12 ms 6280 KB Output is correct
12 Correct 12 ms 6280 KB Output is correct
13 Correct 12 ms 6280 KB Output is correct
14 Correct 11 ms 6280 KB Output is correct
15 Correct 6 ms 6280 KB Output is correct
16 Correct 7 ms 6280 KB Output is correct
17 Correct 6 ms 6280 KB Output is correct
18 Correct 6 ms 6280 KB Output is correct
19 Correct 12 ms 6280 KB Output is correct
20 Correct 12 ms 6280 KB Output is correct
21 Correct 12 ms 6280 KB Output is correct
22 Correct 6 ms 6280 KB Output is correct
23 Correct 6 ms 6280 KB Output is correct
24 Correct 6 ms 6280 KB Output is correct
25 Correct 6 ms 6280 KB Output is correct
26 Correct 26 ms 6280 KB Output is correct
27 Correct 12 ms 6280 KB Output is correct
28 Correct 11 ms 6280 KB Output is correct
29 Correct 11 ms 6280 KB Output is correct
30 Correct 6 ms 6280 KB Output is correct
31 Correct 6 ms 6280 KB Output is correct
32 Correct 12 ms 6280 KB Output is correct
33 Correct 8 ms 6280 KB Output is correct
34 Correct 7 ms 6280 KB Output is correct
35 Correct 6 ms 6280 KB Output is correct
36 Correct 8 ms 6280 KB Output is correct
37 Correct 7 ms 6280 KB Output is correct
38 Correct 6 ms 6280 KB Output is correct
39 Correct 8 ms 6280 KB Output is correct
40 Correct 7 ms 6280 KB Output is correct
41 Correct 7 ms 6280 KB Output is correct
42 Correct 8 ms 6280 KB Output is correct
43 Correct 7 ms 6280 KB Output is correct
44 Correct 7 ms 6280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5884 KB Output is correct
2 Correct 5 ms 5884 KB Output is correct
3 Correct 7 ms 5920 KB Output is correct
4 Correct 6 ms 6100 KB Output is correct
5 Correct 6 ms 6100 KB Output is correct
6 Correct 6 ms 6100 KB Output is correct
7 Correct 6 ms 6100 KB Output is correct
8 Correct 26 ms 6124 KB Output is correct
9 Correct 26 ms 6124 KB Output is correct
10 Correct 27 ms 6280 KB Output is correct
11 Correct 12 ms 6280 KB Output is correct
12 Correct 12 ms 6280 KB Output is correct
13 Correct 12 ms 6280 KB Output is correct
14 Correct 11 ms 6280 KB Output is correct
15 Correct 6 ms 6280 KB Output is correct
16 Correct 7 ms 6280 KB Output is correct
17 Correct 6 ms 6280 KB Output is correct
18 Correct 6 ms 6280 KB Output is correct
19 Correct 12 ms 6280 KB Output is correct
20 Correct 12 ms 6280 KB Output is correct
21 Correct 12 ms 6280 KB Output is correct
22 Correct 6 ms 6280 KB Output is correct
23 Correct 6 ms 6280 KB Output is correct
24 Correct 6 ms 6280 KB Output is correct
25 Correct 6 ms 6280 KB Output is correct
26 Correct 26 ms 6280 KB Output is correct
27 Correct 12 ms 6280 KB Output is correct
28 Correct 11 ms 6280 KB Output is correct
29 Correct 11 ms 6280 KB Output is correct
30 Correct 6 ms 6280 KB Output is correct
31 Correct 6 ms 6280 KB Output is correct
32 Correct 12 ms 6280 KB Output is correct
33 Correct 8 ms 6280 KB Output is correct
34 Correct 7 ms 6280 KB Output is correct
35 Correct 6 ms 6280 KB Output is correct
36 Correct 8 ms 6280 KB Output is correct
37 Correct 7 ms 6280 KB Output is correct
38 Correct 6 ms 6280 KB Output is correct
39 Correct 8 ms 6280 KB Output is correct
40 Correct 7 ms 6280 KB Output is correct
41 Correct 7 ms 6280 KB Output is correct
42 Correct 8 ms 6280 KB Output is correct
43 Correct 7 ms 6280 KB Output is correct
44 Correct 7 ms 6280 KB Output is correct
45 Correct 7 ms 6280 KB Output is correct
46 Correct 12 ms 6524 KB Output is correct
47 Correct 13 ms 6624 KB Output is correct
48 Correct 12 ms 6624 KB Output is correct
49 Execution timed out 1088 ms 6804 KB Time limit exceeded
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5884 KB Output is correct
2 Correct 5 ms 5884 KB Output is correct
3 Correct 7 ms 5920 KB Output is correct
4 Correct 6 ms 6100 KB Output is correct
5 Correct 6 ms 6100 KB Output is correct
6 Correct 6 ms 6100 KB Output is correct
7 Correct 6 ms 6100 KB Output is correct
8 Correct 26 ms 6124 KB Output is correct
9 Correct 26 ms 6124 KB Output is correct
10 Correct 27 ms 6280 KB Output is correct
11 Correct 12 ms 6280 KB Output is correct
12 Correct 12 ms 6280 KB Output is correct
13 Correct 12 ms 6280 KB Output is correct
14 Correct 11 ms 6280 KB Output is correct
15 Correct 6 ms 6280 KB Output is correct
16 Correct 7 ms 6280 KB Output is correct
17 Correct 6 ms 6280 KB Output is correct
18 Correct 6 ms 6280 KB Output is correct
19 Correct 12 ms 6280 KB Output is correct
20 Correct 12 ms 6280 KB Output is correct
21 Correct 12 ms 6280 KB Output is correct
22 Correct 6 ms 6280 KB Output is correct
23 Correct 6 ms 6280 KB Output is correct
24 Correct 6 ms 6280 KB Output is correct
25 Correct 6 ms 6280 KB Output is correct
26 Correct 26 ms 6280 KB Output is correct
27 Correct 12 ms 6280 KB Output is correct
28 Correct 11 ms 6280 KB Output is correct
29 Correct 11 ms 6280 KB Output is correct
30 Correct 6 ms 6280 KB Output is correct
31 Correct 6 ms 6280 KB Output is correct
32 Correct 12 ms 6280 KB Output is correct
33 Correct 8 ms 6280 KB Output is correct
34 Correct 7 ms 6280 KB Output is correct
35 Correct 6 ms 6280 KB Output is correct
36 Correct 8 ms 6280 KB Output is correct
37 Correct 7 ms 6280 KB Output is correct
38 Correct 6 ms 6280 KB Output is correct
39 Correct 8 ms 6280 KB Output is correct
40 Correct 7 ms 6280 KB Output is correct
41 Correct 7 ms 6280 KB Output is correct
42 Correct 8 ms 6280 KB Output is correct
43 Correct 7 ms 6280 KB Output is correct
44 Correct 7 ms 6280 KB Output is correct
45 Correct 7 ms 6280 KB Output is correct
46 Correct 12 ms 6524 KB Output is correct
47 Correct 13 ms 6624 KB Output is correct
48 Correct 12 ms 6624 KB Output is correct
49 Execution timed out 1088 ms 6804 KB Time limit exceeded
50 Halted 0 ms 0 KB -