Submission #892773

# Submission time Handle Problem Language Result Execution time Memory
892773 2023-12-25T23:15:34 Z box Construction of Highway (JOI18_construction) C++17
100 / 100
413 ms 37408 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
using pii = pair<int, int>;

const int N = 1e5;

int n, c[N];
vector<int> adj[N];
vector<pii> con;
vector<int> chain[N];
int parent[N], child[N], root[N], depth[N];

int dfs_sz(int i) {
    int s = 1, x = 0;
    child[i] = -1;
    for (int j : adj[i]) if (parent[i] != j) {
        parent[j] = i;
        depth[j] = depth[i] + 1;
        int y = dfs_sz(j);
        if (y > x) {
            x = y;
            child[i] = j;
        }
        s += y;
    }
    return s;
}

void dfs_hvy(int r, int i) {
    root[i] = r;
    chain[r].push_back(i);
    if (~child[i]) dfs_hvy(r, child[i]);
    for (int j : adj[i]) if (parent[i] != j && child[i] != j) dfs_hvy(j, j);
}

struct node {
    int l, r, c;
    bool operator<(const node o) const {
        return r < o.r;
    }
};

struct DS {
    set<node> s;
    DS() {}
    DS(vector<int> v) {
        for (int i = 0; i < sz(v); i++)
            s.insert({i, i, c[v[i]]});
    }
    auto cut(int k) {
        auto it = s.lower_bound({0, k, 0});
        if (it == end(s) || it->l == k) return it;
        node one{it->l, k - 1, it->c};
        node two{k, it->r, it->c};
        s.erase(it);
        s.insert(one);
        return s.insert(two).first;
    }
    void color(int k, int c) {
        auto r = cut(k);
        s.erase(begin(s), r);
        s.insert({0, k - 1, c});
    }
    void qry(int k, vector<pii> &v) {
        auto r = cut(k);
        for (auto l = begin(s); l != r; ++l) v.push_back({l->c, l->r - l->l + 1});
    }
} ds[N];

struct {
    int t[N];
    void add(int i, int x) {
        while (i < N) {
            t[i] += x;
            i |= i + 1;
        }
    }
    int qry(int i) {
        int x = 0;
        while (i >= 0) {
            x += t[i];
            i &= i + 1, i--;
        }
        return x;
    }
} ft;

i64 num_inversions(vector<pii> v) {
    i64 num = 0;
    reverse(begin(v), end(v));
    for (auto [c, k] : v) {
        num += (i64) ft.qry(c - 1) * k;
        ft.add(c, k);
    }
    for (auto [c, k] : v) ft.add(c, -k);
    return num;
}

vector<pii> path(int i) {
    vector<pii> v;
    while (~i) {
        v.push_back({root[i], depth[i] - depth[root[i]] + 1});
        i = parent[root[i]];
    }
    reverse(begin(v), end(v));
    return v;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> c[i];
    auto sc = set(c, c + n);
    auto vc = vector(begin(sc), end(sc));
    for (int i = 0; i < n; i++) c[i] = lower_bound(begin(vc), end(vc), c[i]) - begin(vc);
    for (int h = 0; h < n - 1; h++) {
        int i, j; 
        cin >> i >> j, i--, j--;
        adj[i].push_back(j);
        adj[j].push_back(i);
        con.push_back({i, j});
    }
    parent[0] = -1;
    dfs_sz(0);
    dfs_hvy(0, 0);
    for (int i = 0; i < n; i++) if (root[i] == i) ds[i] = DS(chain[i]);
    for (auto [i, j] : con) {
        vector<pii> v;
        for (auto [r, k] : path(i)) ds[r].qry(k, v);
        cout << num_inversions(v) << '\n';
        for (auto [r, k] : path(j)) ds[r].color(k, c[j]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11356 KB Output is correct
2 Correct 2 ms 11416 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 4 ms 11612 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 3 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 3 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11384 KB Output is correct
17 Correct 4 ms 11356 KB Output is correct
18 Correct 3 ms 11356 KB Output is correct
19 Correct 3 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11356 KB Output is correct
22 Correct 3 ms 11388 KB Output is correct
23 Correct 3 ms 11356 KB Output is correct
24 Correct 3 ms 11356 KB Output is correct
25 Correct 3 ms 11424 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 3 ms 11356 KB Output is correct
28 Correct 3 ms 11356 KB Output is correct
29 Correct 3 ms 11356 KB Output is correct
30 Correct 3 ms 11356 KB Output is correct
31 Correct 4 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11508 KB Output is correct
34 Correct 3 ms 11352 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 3 ms 11428 KB Output is correct
37 Correct 3 ms 11352 KB Output is correct
38 Correct 3 ms 11356 KB Output is correct
39 Correct 3 ms 11356 KB Output is correct
40 Correct 3 ms 11356 KB Output is correct
41 Correct 3 ms 11356 KB Output is correct
42 Correct 3 ms 11356 KB Output is correct
43 Correct 3 ms 11352 KB Output is correct
44 Correct 3 ms 11352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11356 KB Output is correct
2 Correct 2 ms 11416 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 4 ms 11612 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 3 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 3 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11384 KB Output is correct
17 Correct 4 ms 11356 KB Output is correct
18 Correct 3 ms 11356 KB Output is correct
19 Correct 3 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11356 KB Output is correct
22 Correct 3 ms 11388 KB Output is correct
23 Correct 3 ms 11356 KB Output is correct
24 Correct 3 ms 11356 KB Output is correct
25 Correct 3 ms 11424 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 3 ms 11356 KB Output is correct
28 Correct 3 ms 11356 KB Output is correct
29 Correct 3 ms 11356 KB Output is correct
30 Correct 3 ms 11356 KB Output is correct
31 Correct 4 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11508 KB Output is correct
34 Correct 3 ms 11352 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 3 ms 11428 KB Output is correct
37 Correct 3 ms 11352 KB Output is correct
38 Correct 3 ms 11356 KB Output is correct
39 Correct 3 ms 11356 KB Output is correct
40 Correct 3 ms 11356 KB Output is correct
41 Correct 3 ms 11356 KB Output is correct
42 Correct 3 ms 11356 KB Output is correct
43 Correct 3 ms 11352 KB Output is correct
44 Correct 3 ms 11352 KB Output is correct
45 Correct 5 ms 11608 KB Output is correct
46 Correct 9 ms 12124 KB Output is correct
47 Correct 9 ms 12016 KB Output is correct
48 Correct 12 ms 12124 KB Output is correct
49 Correct 8 ms 12376 KB Output is correct
50 Correct 6 ms 12380 KB Output is correct
51 Correct 6 ms 12380 KB Output is correct
52 Correct 6 ms 12380 KB Output is correct
53 Correct 6 ms 12380 KB Output is correct
54 Correct 7 ms 12196 KB Output is correct
55 Correct 7 ms 12380 KB Output is correct
56 Correct 6 ms 12380 KB Output is correct
57 Correct 11 ms 12124 KB Output is correct
58 Correct 11 ms 12124 KB Output is correct
59 Correct 12 ms 12124 KB Output is correct
60 Correct 12 ms 12124 KB Output is correct
61 Correct 6 ms 12380 KB Output is correct
62 Correct 7 ms 12380 KB Output is correct
63 Correct 7 ms 12352 KB Output is correct
64 Correct 8 ms 11944 KB Output is correct
65 Correct 8 ms 11868 KB Output is correct
66 Correct 8 ms 12040 KB Output is correct
67 Correct 11 ms 12096 KB Output is correct
68 Correct 5 ms 12124 KB Output is correct
69 Correct 8 ms 12232 KB Output is correct
70 Correct 6 ms 12192 KB Output is correct
71 Correct 6 ms 12148 KB Output is correct
72 Correct 15 ms 12200 KB Output is correct
73 Correct 12 ms 11868 KB Output is correct
74 Correct 6 ms 12036 KB Output is correct
75 Correct 7 ms 12124 KB Output is correct
76 Correct 7 ms 12120 KB Output is correct
77 Correct 7 ms 12124 KB Output is correct
78 Correct 6 ms 11868 KB Output is correct
79 Correct 6 ms 11864 KB Output is correct
80 Correct 8 ms 12032 KB Output is correct
81 Correct 7 ms 12196 KB Output is correct
82 Correct 7 ms 12376 KB Output is correct
83 Correct 9 ms 12196 KB Output is correct
84 Correct 7 ms 11868 KB Output is correct
85 Correct 7 ms 11944 KB Output is correct
86 Correct 6 ms 11868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11356 KB Output is correct
2 Correct 2 ms 11416 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 3 ms 11356 KB Output is correct
5 Correct 3 ms 11356 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11356 KB Output is correct
8 Correct 3 ms 11356 KB Output is correct
9 Correct 4 ms 11612 KB Output is correct
10 Correct 3 ms 11356 KB Output is correct
11 Correct 3 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 3 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11384 KB Output is correct
17 Correct 4 ms 11356 KB Output is correct
18 Correct 3 ms 11356 KB Output is correct
19 Correct 3 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11356 KB Output is correct
22 Correct 3 ms 11388 KB Output is correct
23 Correct 3 ms 11356 KB Output is correct
24 Correct 3 ms 11356 KB Output is correct
25 Correct 3 ms 11424 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 3 ms 11356 KB Output is correct
28 Correct 3 ms 11356 KB Output is correct
29 Correct 3 ms 11356 KB Output is correct
30 Correct 3 ms 11356 KB Output is correct
31 Correct 4 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11508 KB Output is correct
34 Correct 3 ms 11352 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 3 ms 11428 KB Output is correct
37 Correct 3 ms 11352 KB Output is correct
38 Correct 3 ms 11356 KB Output is correct
39 Correct 3 ms 11356 KB Output is correct
40 Correct 3 ms 11356 KB Output is correct
41 Correct 3 ms 11356 KB Output is correct
42 Correct 3 ms 11356 KB Output is correct
43 Correct 3 ms 11352 KB Output is correct
44 Correct 3 ms 11352 KB Output is correct
45 Correct 5 ms 11608 KB Output is correct
46 Correct 9 ms 12124 KB Output is correct
47 Correct 9 ms 12016 KB Output is correct
48 Correct 12 ms 12124 KB Output is correct
49 Correct 8 ms 12376 KB Output is correct
50 Correct 6 ms 12380 KB Output is correct
51 Correct 6 ms 12380 KB Output is correct
52 Correct 6 ms 12380 KB Output is correct
53 Correct 6 ms 12380 KB Output is correct
54 Correct 7 ms 12196 KB Output is correct
55 Correct 7 ms 12380 KB Output is correct
56 Correct 6 ms 12380 KB Output is correct
57 Correct 11 ms 12124 KB Output is correct
58 Correct 11 ms 12124 KB Output is correct
59 Correct 12 ms 12124 KB Output is correct
60 Correct 12 ms 12124 KB Output is correct
61 Correct 6 ms 12380 KB Output is correct
62 Correct 7 ms 12380 KB Output is correct
63 Correct 7 ms 12352 KB Output is correct
64 Correct 8 ms 11944 KB Output is correct
65 Correct 8 ms 11868 KB Output is correct
66 Correct 8 ms 12040 KB Output is correct
67 Correct 11 ms 12096 KB Output is correct
68 Correct 5 ms 12124 KB Output is correct
69 Correct 8 ms 12232 KB Output is correct
70 Correct 6 ms 12192 KB Output is correct
71 Correct 6 ms 12148 KB Output is correct
72 Correct 15 ms 12200 KB Output is correct
73 Correct 12 ms 11868 KB Output is correct
74 Correct 6 ms 12036 KB Output is correct
75 Correct 7 ms 12124 KB Output is correct
76 Correct 7 ms 12120 KB Output is correct
77 Correct 7 ms 12124 KB Output is correct
78 Correct 6 ms 11868 KB Output is correct
79 Correct 6 ms 11864 KB Output is correct
80 Correct 8 ms 12032 KB Output is correct
81 Correct 7 ms 12196 KB Output is correct
82 Correct 7 ms 12376 KB Output is correct
83 Correct 9 ms 12196 KB Output is correct
84 Correct 7 ms 11868 KB Output is correct
85 Correct 7 ms 11944 KB Output is correct
86 Correct 6 ms 11868 KB Output is correct
87 Correct 21 ms 13656 KB Output is correct
88 Correct 63 ms 17360 KB Output is correct
89 Correct 284 ms 30976 KB Output is correct
90 Correct 263 ms 30664 KB Output is correct
91 Correct 249 ms 30812 KB Output is correct
92 Correct 135 ms 37324 KB Output is correct
93 Correct 139 ms 37408 KB Output is correct
94 Correct 135 ms 37316 KB Output is correct
95 Correct 161 ms 35012 KB Output is correct
96 Correct 160 ms 35324 KB Output is correct
97 Correct 155 ms 35492 KB Output is correct
98 Correct 158 ms 35780 KB Output is correct
99 Correct 147 ms 34616 KB Output is correct
100 Correct 368 ms 30152 KB Output is correct
101 Correct 364 ms 30280 KB Output is correct
102 Correct 413 ms 29968 KB Output is correct
103 Correct 362 ms 30128 KB Output is correct
104 Correct 165 ms 34908 KB Output is correct
105 Correct 166 ms 34736 KB Output is correct
106 Correct 159 ms 34500 KB Output is correct
107 Correct 214 ms 25024 KB Output is correct
108 Correct 204 ms 25028 KB Output is correct
109 Correct 245 ms 26564 KB Output is correct
110 Correct 115 ms 31704 KB Output is correct
111 Correct 158 ms 35228 KB Output is correct
112 Correct 117 ms 29636 KB Output is correct
113 Correct 141 ms 29124 KB Output is correct
114 Correct 339 ms 29808 KB Output is correct
115 Correct 350 ms 24756 KB Output is correct
116 Correct 114 ms 28864 KB Output is correct
117 Correct 147 ms 32456 KB Output is correct
118 Correct 149 ms 31688 KB Output is correct
119 Correct 174 ms 31428 KB Output is correct
120 Correct 113 ms 26888 KB Output is correct
121 Correct 118 ms 26304 KB Output is correct
122 Correct 130 ms 26316 KB Output is correct
123 Correct 156 ms 32452 KB Output is correct
124 Correct 156 ms 31832 KB Output is correct
125 Correct 168 ms 31684 KB Output is correct
126 Correct 125 ms 27052 KB Output is correct
127 Correct 135 ms 26416 KB Output is correct
128 Correct 126 ms 26052 KB Output is correct