답안 #892724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892724 2023-12-25T18:21:23 Z box Construction of Highway (JOI18_construction) C++17
100 / 100
390 ms 38640 KB
#include <bits/stdc++.h>
using namespace std;

#define cerr if (0) cerr

#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;
    }
    cerr << "size(" << i+1 << ") => " << s << endl;
    return s;
}

void dfs_hvy(int r, int i) {
    cerr << "dfs_hvy(" << r+1 << ", " << i+1 << ")\n";
    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) {
        cerr << "heavy chain: ";
        for (int i = 0; i < sz(v); i++) {
            s.insert({i, i, c[v[i]]});
            cerr << v[i] + 1 << " \n"[i == sz(v) - 1];
        }
    }
    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);
        s.insert(two);
        return s.find(two);
    }
    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 i = 0; i < n; i++) cerr << c[i] << " \n"[i == n - 1];
    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) {
        cerr << "heavy root: " << i+1 << '\n';
        ds[i] = DS(chain[i]);
    }
    for (auto [i, j] : con) {
        vector<pii> v;
        for (auto [r, k] : path(i)) ds[r].qry(k, v);
        for (auto [c, k] : v) cerr << "(color=" << c << ", count=" << k << "), ";
        cerr << endl;
        cout << num_inversions(v) << '\n';
        for (auto [r, k] : path(j)) ds[r].color(k, c[j]);
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11612 KB Output is correct
2 Correct 2 ms 11356 KB Output is correct
3 Correct 2 ms 11356 KB Output is correct
4 Correct 2 ms 11352 KB Output is correct
5 Correct 3 ms 11352 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11544 KB Output is correct
8 Correct 2 ms 11352 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 2 ms 11356 KB Output is correct
11 Correct 3 ms 11380 KB Output is correct
12 Correct 3 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11352 KB Output is correct
17 Correct 3 ms 11356 KB Output is correct
18 Correct 3 ms 11440 KB Output is correct
19 Correct 2 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11352 KB Output is correct
22 Correct 3 ms 11356 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 11356 KB Output is correct
26 Correct 2 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 3 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11356 KB Output is correct
34 Correct 3 ms 11356 KB Output is correct
35 Correct 3 ms 11356 KB Output is correct
36 Correct 3 ms 11532 KB Output is correct
37 Correct 3 ms 11556 KB Output is correct
38 Correct 2 ms 11352 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 2 ms 11356 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11612 KB Output is correct
2 Correct 2 ms 11356 KB Output is correct
3 Correct 2 ms 11356 KB Output is correct
4 Correct 2 ms 11352 KB Output is correct
5 Correct 3 ms 11352 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11544 KB Output is correct
8 Correct 2 ms 11352 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 2 ms 11356 KB Output is correct
11 Correct 3 ms 11380 KB Output is correct
12 Correct 3 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11352 KB Output is correct
17 Correct 3 ms 11356 KB Output is correct
18 Correct 3 ms 11440 KB Output is correct
19 Correct 2 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11352 KB Output is correct
22 Correct 3 ms 11356 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 11356 KB Output is correct
26 Correct 2 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 3 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11356 KB Output is correct
34 Correct 3 ms 11356 KB Output is correct
35 Correct 3 ms 11356 KB Output is correct
36 Correct 3 ms 11532 KB Output is correct
37 Correct 3 ms 11556 KB Output is correct
38 Correct 2 ms 11352 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 2 ms 11356 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
45 Correct 4 ms 11612 KB Output is correct
46 Correct 10 ms 12124 KB Output is correct
47 Correct 9 ms 12124 KB Output is correct
48 Correct 9 ms 12124 KB Output is correct
49 Correct 7 ms 12380 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 12376 KB Output is correct
54 Correct 7 ms 12376 KB Output is correct
55 Correct 6 ms 12280 KB Output is correct
56 Correct 7 ms 12380 KB Output is correct
57 Correct 11 ms 12124 KB Output is correct
58 Correct 12 ms 12152 KB Output is correct
59 Correct 12 ms 12228 KB Output is correct
60 Correct 13 ms 12224 KB Output is correct
61 Correct 8 ms 12376 KB Output is correct
62 Correct 6 ms 12380 KB Output is correct
63 Correct 6 ms 12380 KB Output is correct
64 Correct 8 ms 11868 KB Output is correct
65 Correct 8 ms 11868 KB Output is correct
66 Correct 9 ms 11868 KB Output is correct
67 Correct 8 ms 11868 KB Output is correct
68 Correct 5 ms 12124 KB Output is correct
69 Correct 7 ms 12380 KB Output is correct
70 Correct 6 ms 12380 KB Output is correct
71 Correct 5 ms 12120 KB Output is correct
72 Correct 13 ms 12120 KB Output is correct
73 Correct 11 ms 11868 KB Output is correct
74 Correct 6 ms 12124 KB Output is correct
75 Correct 7 ms 12124 KB Output is correct
76 Correct 6 ms 12124 KB Output is correct
77 Correct 6 ms 12124 KB Output is correct
78 Correct 5 ms 11868 KB Output is correct
79 Correct 6 ms 11868 KB Output is correct
80 Correct 6 ms 11868 KB Output is correct
81 Correct 8 ms 12124 KB Output is correct
82 Correct 7 ms 12196 KB Output is correct
83 Correct 7 ms 12124 KB Output is correct
84 Correct 7 ms 11928 KB Output is correct
85 Correct 6 ms 12120 KB Output is correct
86 Correct 6 ms 11868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11612 KB Output is correct
2 Correct 2 ms 11356 KB Output is correct
3 Correct 2 ms 11356 KB Output is correct
4 Correct 2 ms 11352 KB Output is correct
5 Correct 3 ms 11352 KB Output is correct
6 Correct 3 ms 11356 KB Output is correct
7 Correct 3 ms 11544 KB Output is correct
8 Correct 2 ms 11352 KB Output is correct
9 Correct 4 ms 11356 KB Output is correct
10 Correct 2 ms 11356 KB Output is correct
11 Correct 3 ms 11380 KB Output is correct
12 Correct 3 ms 11356 KB Output is correct
13 Correct 3 ms 11356 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 3 ms 11356 KB Output is correct
16 Correct 3 ms 11352 KB Output is correct
17 Correct 3 ms 11356 KB Output is correct
18 Correct 3 ms 11440 KB Output is correct
19 Correct 2 ms 11356 KB Output is correct
20 Correct 3 ms 11356 KB Output is correct
21 Correct 3 ms 11352 KB Output is correct
22 Correct 3 ms 11356 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 11356 KB Output is correct
26 Correct 2 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 3 ms 11356 KB Output is correct
32 Correct 3 ms 11356 KB Output is correct
33 Correct 3 ms 11356 KB Output is correct
34 Correct 3 ms 11356 KB Output is correct
35 Correct 3 ms 11356 KB Output is correct
36 Correct 3 ms 11532 KB Output is correct
37 Correct 3 ms 11556 KB Output is correct
38 Correct 2 ms 11352 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 2 ms 11356 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
45 Correct 4 ms 11612 KB Output is correct
46 Correct 10 ms 12124 KB Output is correct
47 Correct 9 ms 12124 KB Output is correct
48 Correct 9 ms 12124 KB Output is correct
49 Correct 7 ms 12380 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 12376 KB Output is correct
54 Correct 7 ms 12376 KB Output is correct
55 Correct 6 ms 12280 KB Output is correct
56 Correct 7 ms 12380 KB Output is correct
57 Correct 11 ms 12124 KB Output is correct
58 Correct 12 ms 12152 KB Output is correct
59 Correct 12 ms 12228 KB Output is correct
60 Correct 13 ms 12224 KB Output is correct
61 Correct 8 ms 12376 KB Output is correct
62 Correct 6 ms 12380 KB Output is correct
63 Correct 6 ms 12380 KB Output is correct
64 Correct 8 ms 11868 KB Output is correct
65 Correct 8 ms 11868 KB Output is correct
66 Correct 9 ms 11868 KB Output is correct
67 Correct 8 ms 11868 KB Output is correct
68 Correct 5 ms 12124 KB Output is correct
69 Correct 7 ms 12380 KB Output is correct
70 Correct 6 ms 12380 KB Output is correct
71 Correct 5 ms 12120 KB Output is correct
72 Correct 13 ms 12120 KB Output is correct
73 Correct 11 ms 11868 KB Output is correct
74 Correct 6 ms 12124 KB Output is correct
75 Correct 7 ms 12124 KB Output is correct
76 Correct 6 ms 12124 KB Output is correct
77 Correct 6 ms 12124 KB Output is correct
78 Correct 5 ms 11868 KB Output is correct
79 Correct 6 ms 11868 KB Output is correct
80 Correct 6 ms 11868 KB Output is correct
81 Correct 8 ms 12124 KB Output is correct
82 Correct 7 ms 12196 KB Output is correct
83 Correct 7 ms 12124 KB Output is correct
84 Correct 7 ms 11928 KB Output is correct
85 Correct 6 ms 12120 KB Output is correct
86 Correct 6 ms 11868 KB Output is correct
87 Correct 22 ms 13400 KB Output is correct
88 Correct 62 ms 17360 KB Output is correct
89 Correct 243 ms 31428 KB Output is correct
90 Correct 239 ms 31612 KB Output is correct
91 Correct 255 ms 31616 KB Output is correct
92 Correct 148 ms 38160 KB Output is correct
93 Correct 136 ms 38300 KB Output is correct
94 Correct 143 ms 38640 KB Output is correct
95 Correct 152 ms 35780 KB Output is correct
96 Correct 159 ms 36356 KB Output is correct
97 Correct 157 ms 36316 KB Output is correct
98 Correct 156 ms 36324 KB Output is correct
99 Correct 182 ms 35484 KB Output is correct
100 Correct 365 ms 30920 KB Output is correct
101 Correct 352 ms 30988 KB Output is correct
102 Correct 384 ms 31172 KB Output is correct
103 Correct 390 ms 30900 KB Output is correct
104 Correct 160 ms 35484 KB Output is correct
105 Correct 150 ms 35364 KB Output is correct
106 Correct 148 ms 35528 KB Output is correct
107 Correct 227 ms 25512 KB Output is correct
108 Correct 207 ms 25280 KB Output is correct
109 Correct 247 ms 27240 KB Output is correct
110 Correct 104 ms 32200 KB Output is correct
111 Correct 156 ms 35820 KB Output is correct
112 Correct 118 ms 30148 KB Output is correct
113 Correct 126 ms 29380 KB Output is correct
114 Correct 325 ms 30616 KB Output is correct
115 Correct 322 ms 24956 KB Output is correct
116 Correct 109 ms 29380 KB Output is correct
117 Correct 142 ms 32964 KB Output is correct
118 Correct 150 ms 32288 KB Output is correct
119 Correct 154 ms 32196 KB Output is correct
120 Correct 109 ms 27328 KB Output is correct
121 Correct 118 ms 26844 KB Output is correct
122 Correct 115 ms 26576 KB Output is correct
123 Correct 164 ms 33144 KB Output is correct
124 Correct 173 ms 32452 KB Output is correct
125 Correct 160 ms 31948 KB Output is correct
126 Correct 118 ms 27584 KB Output is correct
127 Correct 122 ms 26740 KB Output is correct
128 Correct 123 ms 26564 KB Output is correct