답안 #362930

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
362930 2021-02-04T18:22:37 Z dolphingarlic Construction of Highway (JOI18_construction) C++14
100 / 100
620 ms 108780 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int c[100001], u[100001], v[100001], par[100001];
vector<int> graph[100001];
int heavy[100001], head[100001], depth[100001];
deque<pair<int, int>> hld_vals[100001];

int dfs(int node = 1) {
    int sz = 1, mx = 0;
    for (int i : graph[node]) {
        int csz = dfs(i);
        sz += csz;
        if (csz > mx) mx = csz, heavy[node] = i;
    }
    return sz;
}

void decompose(int node = 1, int h = 1, int d = 1) {
    head[node] = h; depth[node] = d;
    if (heavy[node]) decompose(heavy[node], h, d + 1);
    for (int i : graph[node]) if (i != heavy[node]) decompose(i, i, 1);
}

ll count_inversions(deque<pair<int, int>> &path) {
    map<int, int> comp;
    for (pair<int, int> i : path) comp[i.first];
    int n = 0;
    for (auto &i : comp) i.second = ++n;

    ll ans = 0;
    vector<ll> bit(n + 1);
    for (pair<int, int> i : path) {
        for (int j = comp[i.first] - 1; j; j -= j & -j) ans += bit[j] * i.second;
        for (int j = comp[i.first]; j <= n; j += j & -j) bit[j] += i.second;
    }
    return ans;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> c[i];
    for (int i = 1; i < n; i++) {
        cin >> u[i] >> v[i];
        graph[u[i]].push_back(v[i]);
        par[v[i]] = u[i];
    }
    dfs(); decompose();

    hld_vals[1].push_back({c[1], 1});
    for (int i = 1; i < n; i++) {
        deque<pair<int, int>> path;
        int curr = v[i];
        while (curr) {
            deque<pair<int, int>> ins;
            int rem = depth[curr] - (curr == v[i]), h = head[curr];
            while (rem) {
                if (hld_vals[h][0].second <= rem) {
                    rem -= hld_vals[h][0].second;
                    ins.push_front(hld_vals[h][0]);
                    hld_vals[h].pop_front();
                } else {
                    ins.push_front({hld_vals[h][0].first, rem});
                    pair<int, int> upd = {hld_vals[h][0].first, hld_vals[h][0].second - rem};
                    hld_vals[h].pop_front();
                    hld_vals[h].push_front(upd);
                    rem = 0;
                }
            }
            hld_vals[h].push_front({c[v[i]], depth[curr]});
            path.insert(path.end(), ins.begin(), ins.end());
            curr = par[h];
        }
        cout << count_inversions(path) << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 69996 KB Output is correct
2 Correct 54 ms 69996 KB Output is correct
3 Correct 54 ms 70124 KB Output is correct
4 Correct 59 ms 70124 KB Output is correct
5 Correct 66 ms 70252 KB Output is correct
6 Correct 55 ms 70252 KB Output is correct
7 Correct 58 ms 70252 KB Output is correct
8 Correct 56 ms 70124 KB Output is correct
9 Correct 53 ms 70124 KB Output is correct
10 Correct 58 ms 70252 KB Output is correct
11 Correct 56 ms 70252 KB Output is correct
12 Correct 54 ms 70380 KB Output is correct
13 Correct 54 ms 70252 KB Output is correct
14 Correct 58 ms 70272 KB Output is correct
15 Correct 54 ms 70252 KB Output is correct
16 Correct 55 ms 70252 KB Output is correct
17 Correct 56 ms 70252 KB Output is correct
18 Correct 54 ms 70252 KB Output is correct
19 Correct 68 ms 70252 KB Output is correct
20 Correct 59 ms 70252 KB Output is correct
21 Correct 59 ms 70252 KB Output is correct
22 Correct 58 ms 70252 KB Output is correct
23 Correct 55 ms 70252 KB Output is correct
24 Correct 54 ms 70252 KB Output is correct
25 Correct 54 ms 70252 KB Output is correct
26 Correct 63 ms 70124 KB Output is correct
27 Correct 55 ms 70252 KB Output is correct
28 Correct 54 ms 70252 KB Output is correct
29 Correct 59 ms 70380 KB Output is correct
30 Correct 55 ms 70252 KB Output is correct
31 Correct 57 ms 70252 KB Output is correct
32 Correct 58 ms 70252 KB Output is correct
33 Correct 54 ms 70252 KB Output is correct
34 Correct 61 ms 70252 KB Output is correct
35 Correct 67 ms 70252 KB Output is correct
36 Correct 54 ms 70252 KB Output is correct
37 Correct 60 ms 70252 KB Output is correct
38 Correct 58 ms 70252 KB Output is correct
39 Correct 57 ms 70252 KB Output is correct
40 Correct 55 ms 70252 KB Output is correct
41 Correct 54 ms 70252 KB Output is correct
42 Correct 59 ms 70380 KB Output is correct
43 Correct 54 ms 70252 KB Output is correct
44 Correct 58 ms 70256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 69996 KB Output is correct
2 Correct 54 ms 69996 KB Output is correct
3 Correct 54 ms 70124 KB Output is correct
4 Correct 59 ms 70124 KB Output is correct
5 Correct 66 ms 70252 KB Output is correct
6 Correct 55 ms 70252 KB Output is correct
7 Correct 58 ms 70252 KB Output is correct
8 Correct 56 ms 70124 KB Output is correct
9 Correct 53 ms 70124 KB Output is correct
10 Correct 58 ms 70252 KB Output is correct
11 Correct 56 ms 70252 KB Output is correct
12 Correct 54 ms 70380 KB Output is correct
13 Correct 54 ms 70252 KB Output is correct
14 Correct 58 ms 70272 KB Output is correct
15 Correct 54 ms 70252 KB Output is correct
16 Correct 55 ms 70252 KB Output is correct
17 Correct 56 ms 70252 KB Output is correct
18 Correct 54 ms 70252 KB Output is correct
19 Correct 68 ms 70252 KB Output is correct
20 Correct 59 ms 70252 KB Output is correct
21 Correct 59 ms 70252 KB Output is correct
22 Correct 58 ms 70252 KB Output is correct
23 Correct 55 ms 70252 KB Output is correct
24 Correct 54 ms 70252 KB Output is correct
25 Correct 54 ms 70252 KB Output is correct
26 Correct 63 ms 70124 KB Output is correct
27 Correct 55 ms 70252 KB Output is correct
28 Correct 54 ms 70252 KB Output is correct
29 Correct 59 ms 70380 KB Output is correct
30 Correct 55 ms 70252 KB Output is correct
31 Correct 57 ms 70252 KB Output is correct
32 Correct 58 ms 70252 KB Output is correct
33 Correct 54 ms 70252 KB Output is correct
34 Correct 61 ms 70252 KB Output is correct
35 Correct 67 ms 70252 KB Output is correct
36 Correct 54 ms 70252 KB Output is correct
37 Correct 60 ms 70252 KB Output is correct
38 Correct 58 ms 70252 KB Output is correct
39 Correct 57 ms 70252 KB Output is correct
40 Correct 55 ms 70252 KB Output is correct
41 Correct 54 ms 70252 KB Output is correct
42 Correct 59 ms 70380 KB Output is correct
43 Correct 54 ms 70252 KB Output is correct
44 Correct 58 ms 70256 KB Output is correct
45 Correct 61 ms 70380 KB Output is correct
46 Correct 73 ms 71404 KB Output is correct
47 Correct 66 ms 71404 KB Output is correct
48 Correct 66 ms 71404 KB Output is correct
49 Correct 58 ms 70764 KB Output is correct
50 Correct 57 ms 70636 KB Output is correct
51 Correct 58 ms 70636 KB Output is correct
52 Correct 58 ms 71532 KB Output is correct
53 Correct 60 ms 71532 KB Output is correct
54 Correct 59 ms 71532 KB Output is correct
55 Correct 58 ms 71692 KB Output is correct
56 Correct 63 ms 71532 KB Output is correct
57 Correct 70 ms 71276 KB Output is correct
58 Correct 70 ms 71276 KB Output is correct
59 Correct 70 ms 71276 KB Output is correct
60 Correct 70 ms 71404 KB Output is correct
61 Correct 66 ms 71532 KB Output is correct
62 Correct 65 ms 71532 KB Output is correct
63 Correct 64 ms 71532 KB Output is correct
64 Correct 64 ms 71404 KB Output is correct
65 Correct 65 ms 71404 KB Output is correct
66 Correct 64 ms 71276 KB Output is correct
67 Correct 66 ms 71276 KB Output is correct
68 Correct 64 ms 70636 KB Output is correct
69 Correct 62 ms 71532 KB Output is correct
70 Correct 66 ms 71532 KB Output is correct
71 Correct 75 ms 71404 KB Output is correct
72 Correct 67 ms 71276 KB Output is correct
73 Correct 70 ms 71276 KB Output is correct
74 Correct 64 ms 71404 KB Output is correct
75 Correct 64 ms 71404 KB Output is correct
76 Correct 63 ms 71404 KB Output is correct
77 Correct 64 ms 71404 KB Output is correct
78 Correct 64 ms 71404 KB Output is correct
79 Correct 63 ms 71404 KB Output is correct
80 Correct 65 ms 71404 KB Output is correct
81 Correct 63 ms 71404 KB Output is correct
82 Correct 65 ms 71404 KB Output is correct
83 Correct 73 ms 71404 KB Output is correct
84 Correct 65 ms 71404 KB Output is correct
85 Correct 63 ms 71404 KB Output is correct
86 Correct 63 ms 71404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 69996 KB Output is correct
2 Correct 54 ms 69996 KB Output is correct
3 Correct 54 ms 70124 KB Output is correct
4 Correct 59 ms 70124 KB Output is correct
5 Correct 66 ms 70252 KB Output is correct
6 Correct 55 ms 70252 KB Output is correct
7 Correct 58 ms 70252 KB Output is correct
8 Correct 56 ms 70124 KB Output is correct
9 Correct 53 ms 70124 KB Output is correct
10 Correct 58 ms 70252 KB Output is correct
11 Correct 56 ms 70252 KB Output is correct
12 Correct 54 ms 70380 KB Output is correct
13 Correct 54 ms 70252 KB Output is correct
14 Correct 58 ms 70272 KB Output is correct
15 Correct 54 ms 70252 KB Output is correct
16 Correct 55 ms 70252 KB Output is correct
17 Correct 56 ms 70252 KB Output is correct
18 Correct 54 ms 70252 KB Output is correct
19 Correct 68 ms 70252 KB Output is correct
20 Correct 59 ms 70252 KB Output is correct
21 Correct 59 ms 70252 KB Output is correct
22 Correct 58 ms 70252 KB Output is correct
23 Correct 55 ms 70252 KB Output is correct
24 Correct 54 ms 70252 KB Output is correct
25 Correct 54 ms 70252 KB Output is correct
26 Correct 63 ms 70124 KB Output is correct
27 Correct 55 ms 70252 KB Output is correct
28 Correct 54 ms 70252 KB Output is correct
29 Correct 59 ms 70380 KB Output is correct
30 Correct 55 ms 70252 KB Output is correct
31 Correct 57 ms 70252 KB Output is correct
32 Correct 58 ms 70252 KB Output is correct
33 Correct 54 ms 70252 KB Output is correct
34 Correct 61 ms 70252 KB Output is correct
35 Correct 67 ms 70252 KB Output is correct
36 Correct 54 ms 70252 KB Output is correct
37 Correct 60 ms 70252 KB Output is correct
38 Correct 58 ms 70252 KB Output is correct
39 Correct 57 ms 70252 KB Output is correct
40 Correct 55 ms 70252 KB Output is correct
41 Correct 54 ms 70252 KB Output is correct
42 Correct 59 ms 70380 KB Output is correct
43 Correct 54 ms 70252 KB Output is correct
44 Correct 58 ms 70256 KB Output is correct
45 Correct 61 ms 70380 KB Output is correct
46 Correct 73 ms 71404 KB Output is correct
47 Correct 66 ms 71404 KB Output is correct
48 Correct 66 ms 71404 KB Output is correct
49 Correct 58 ms 70764 KB Output is correct
50 Correct 57 ms 70636 KB Output is correct
51 Correct 58 ms 70636 KB Output is correct
52 Correct 58 ms 71532 KB Output is correct
53 Correct 60 ms 71532 KB Output is correct
54 Correct 59 ms 71532 KB Output is correct
55 Correct 58 ms 71692 KB Output is correct
56 Correct 63 ms 71532 KB Output is correct
57 Correct 70 ms 71276 KB Output is correct
58 Correct 70 ms 71276 KB Output is correct
59 Correct 70 ms 71276 KB Output is correct
60 Correct 70 ms 71404 KB Output is correct
61 Correct 66 ms 71532 KB Output is correct
62 Correct 65 ms 71532 KB Output is correct
63 Correct 64 ms 71532 KB Output is correct
64 Correct 64 ms 71404 KB Output is correct
65 Correct 65 ms 71404 KB Output is correct
66 Correct 64 ms 71276 KB Output is correct
67 Correct 66 ms 71276 KB Output is correct
68 Correct 64 ms 70636 KB Output is correct
69 Correct 62 ms 71532 KB Output is correct
70 Correct 66 ms 71532 KB Output is correct
71 Correct 75 ms 71404 KB Output is correct
72 Correct 67 ms 71276 KB Output is correct
73 Correct 70 ms 71276 KB Output is correct
74 Correct 64 ms 71404 KB Output is correct
75 Correct 64 ms 71404 KB Output is correct
76 Correct 63 ms 71404 KB Output is correct
77 Correct 64 ms 71404 KB Output is correct
78 Correct 64 ms 71404 KB Output is correct
79 Correct 63 ms 71404 KB Output is correct
80 Correct 65 ms 71404 KB Output is correct
81 Correct 63 ms 71404 KB Output is correct
82 Correct 65 ms 71404 KB Output is correct
83 Correct 73 ms 71404 KB Output is correct
84 Correct 65 ms 71404 KB Output is correct
85 Correct 63 ms 71404 KB Output is correct
86 Correct 63 ms 71404 KB Output is correct
87 Correct 80 ms 73324 KB Output is correct
88 Correct 142 ms 79828 KB Output is correct
89 Correct 381 ms 102660 KB Output is correct
90 Correct 386 ms 102636 KB Output is correct
91 Correct 369 ms 102636 KB Output is correct
92 Correct 148 ms 84460 KB Output is correct
93 Correct 150 ms 84460 KB Output is correct
94 Correct 153 ms 84460 KB Output is correct
95 Correct 219 ms 108396 KB Output is correct
96 Correct 236 ms 108780 KB Output is correct
97 Correct 241 ms 108780 KB Output is correct
98 Correct 272 ms 108780 KB Output is correct
99 Correct 216 ms 105820 KB Output is correct
100 Correct 451 ms 94700 KB Output is correct
101 Correct 588 ms 95084 KB Output is correct
102 Correct 620 ms 95212 KB Output is correct
103 Correct 614 ms 95224 KB Output is correct
104 Correct 226 ms 105708 KB Output is correct
105 Correct 229 ms 105836 KB Output is correct
106 Correct 208 ms 105708 KB Output is correct
107 Correct 344 ms 102028 KB Output is correct
108 Correct 358 ms 102252 KB Output is correct
109 Correct 374 ms 102380 KB Output is correct
110 Correct 147 ms 83820 KB Output is correct
111 Correct 223 ms 108396 KB Output is correct
112 Correct 211 ms 105836 KB Output is correct
113 Correct 197 ms 105288 KB Output is correct
114 Correct 497 ms 94828 KB Output is correct
115 Correct 600 ms 94572 KB Output is correct
116 Correct 218 ms 105068 KB Output is correct
117 Correct 211 ms 103916 KB Output is correct
118 Correct 214 ms 103404 KB Output is correct
119 Correct 214 ms 103148 KB Output is correct
120 Correct 258 ms 103388 KB Output is correct
121 Correct 228 ms 103020 KB Output is correct
122 Correct 212 ms 102764 KB Output is correct
123 Correct 226 ms 103916 KB Output is correct
124 Correct 225 ms 103404 KB Output is correct
125 Correct 242 ms 103148 KB Output is correct
126 Correct 232 ms 103532 KB Output is correct
127 Correct 216 ms 103020 KB Output is correct
128 Correct 238 ms 102764 KB Output is correct