답안 #783224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
783224 2023-07-14T18:22:47 Z Kahou Construction of Highway (JOI18_construction) C++14
100 / 100
1308 ms 24940 KB
/* In the name of God, aka Allah */
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e5 + 50, LG = 17;
int n, a[N], f[N], h[N], st[N], ft[N], par[LG][N], fen[2][N], tim;
pii P[N];
pii E[N];
vector<int> adj[N];

void dfs(int u) {
        st[u] = ++tim;
        for (int v:adj[u]) {
                h[v] = h[u]+1;
                par[0][v] = u;
                dfs(v);
        }
        ft[u] = tim;
}

int get(int c, int i) {
        int out = 0;
        for (; i > 0; i -= i&-i) out += fen[c][i];
        return out;
}
void upd(int c, int i, int x) {
        for (; i <= n; i += i&-i) fen[c][i] += x;
}

int getpar(int u) {
        int tmp = get(0, st[u]);
        int v = u;
        for (int k = LG-1; k >= 0; k--) {
                if (tmp - get(0, st[par[k][v]]) == 0) v = par[k][v];
        }
        return v;
}
void solve() {
        cin >> n;
        for (int u = 1; u <= n; u++) {
                cin >> a[u];
                P[u] = {a[u], u};
        }
        sort(P+1, P+n+1);
        int t = 0;
        for (int i = 1; i <= n; i++) {
                if (P[i].F != P[i-1].F) t++;
                a[P[i].S] = t;
        }
        for (int i = 1; i <= n-1; i++) {
                int u, v;
                cin >> u >> v;
                E[i] = {u, v};
                adj[u].push_back(v);
        }
        h[1] = 1;
        dfs(1);
        for (int k = 1; k < LG; k++) {
                for (int u = 1; u <= n; u++) {
                        par[k][u] = par[k-1][par[k-1][u]];
                }
        }
        upd(0, st[1], +1);
        upd(0, ft[1]+1, -1);
        for (int i = 1; i <= n-1; i++) {
                int u = E[i].F;
                ll out = 0;
                while (u) {
                        int v = getpar(u);
                        upd(1, a[v], h[u]-h[v]+1);
                        out += 1ll*(h[u]-h[v]+1)*get(1, a[v]-1);
                        u = par[0][v];
                }
                cout << out << endl;
 
                u = E[i].F;
                if (f[u]) {
                        upd(0, st[f[u]], +1);
                        upd(0, ft[f[u]]+1, -1);
                        a[f[u]] = a[getpar(u)];
                }
                f[u] = E[i].S;
                while (u) {
                        int v = getpar(u);
                        upd(1, a[v], -(h[u]-h[v]+1));
                        a[v] = a[E[i].S];
                        if (v != 1) {
                                upd(0, st[v], -1);
                                upd(0, ft[v]+1, +1);
                                upd(0, st[f[par[0][v]]], +1);
                                upd(0, ft[f[par[0][v]]]+1, -1);
                                a[f[par[0][v]]] = a[getpar(v)];
                                f[par[0][v]] = v;
                        }
                        u = par[0][v];
                }
        }
}
int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        solve();
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2868 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2756 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2856 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2820 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 3 ms 2748 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 3 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2868 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2756 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2856 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2820 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 3 ms 2748 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 3 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 10 ms 3284 KB Output is correct
47 Correct 10 ms 3284 KB Output is correct
48 Correct 10 ms 3296 KB Output is correct
49 Correct 4 ms 3540 KB Output is correct
50 Correct 4 ms 3540 KB Output is correct
51 Correct 4 ms 3540 KB Output is correct
52 Correct 6 ms 3412 KB Output is correct
53 Correct 6 ms 3308 KB Output is correct
54 Correct 6 ms 3412 KB Output is correct
55 Correct 6 ms 3408 KB Output is correct
56 Correct 6 ms 3412 KB Output is correct
57 Correct 19 ms 3292 KB Output is correct
58 Correct 19 ms 3284 KB Output is correct
59 Correct 22 ms 3300 KB Output is correct
60 Correct 19 ms 3308 KB Output is correct
61 Correct 6 ms 3412 KB Output is correct
62 Correct 6 ms 3412 KB Output is correct
63 Correct 6 ms 3412 KB Output is correct
64 Correct 14 ms 3420 KB Output is correct
65 Correct 10 ms 3284 KB Output is correct
66 Correct 10 ms 3300 KB Output is correct
67 Correct 10 ms 3296 KB Output is correct
68 Correct 4 ms 3580 KB Output is correct
69 Correct 6 ms 3412 KB Output is correct
70 Correct 6 ms 3412 KB Output is correct
71 Correct 6 ms 3412 KB Output is correct
72 Correct 19 ms 3292 KB Output is correct
73 Correct 19 ms 3284 KB Output is correct
74 Correct 6 ms 3412 KB Output is correct
75 Correct 6 ms 3284 KB Output is correct
76 Correct 6 ms 3284 KB Output is correct
77 Correct 6 ms 3284 KB Output is correct
78 Correct 6 ms 3284 KB Output is correct
79 Correct 6 ms 3284 KB Output is correct
80 Correct 6 ms 3284 KB Output is correct
81 Correct 7 ms 3284 KB Output is correct
82 Correct 7 ms 3348 KB Output is correct
83 Correct 9 ms 3284 KB Output is correct
84 Correct 7 ms 3284 KB Output is correct
85 Correct 7 ms 3296 KB Output is correct
86 Correct 7 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2868 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2756 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2856 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2820 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 3 ms 2748 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 3 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 3 ms 2900 KB Output is correct
46 Correct 10 ms 3284 KB Output is correct
47 Correct 10 ms 3284 KB Output is correct
48 Correct 10 ms 3296 KB Output is correct
49 Correct 4 ms 3540 KB Output is correct
50 Correct 4 ms 3540 KB Output is correct
51 Correct 4 ms 3540 KB Output is correct
52 Correct 6 ms 3412 KB Output is correct
53 Correct 6 ms 3308 KB Output is correct
54 Correct 6 ms 3412 KB Output is correct
55 Correct 6 ms 3408 KB Output is correct
56 Correct 6 ms 3412 KB Output is correct
57 Correct 19 ms 3292 KB Output is correct
58 Correct 19 ms 3284 KB Output is correct
59 Correct 22 ms 3300 KB Output is correct
60 Correct 19 ms 3308 KB Output is correct
61 Correct 6 ms 3412 KB Output is correct
62 Correct 6 ms 3412 KB Output is correct
63 Correct 6 ms 3412 KB Output is correct
64 Correct 14 ms 3420 KB Output is correct
65 Correct 10 ms 3284 KB Output is correct
66 Correct 10 ms 3300 KB Output is correct
67 Correct 10 ms 3296 KB Output is correct
68 Correct 4 ms 3580 KB Output is correct
69 Correct 6 ms 3412 KB Output is correct
70 Correct 6 ms 3412 KB Output is correct
71 Correct 6 ms 3412 KB Output is correct
72 Correct 19 ms 3292 KB Output is correct
73 Correct 19 ms 3284 KB Output is correct
74 Correct 6 ms 3412 KB Output is correct
75 Correct 6 ms 3284 KB Output is correct
76 Correct 6 ms 3284 KB Output is correct
77 Correct 6 ms 3284 KB Output is correct
78 Correct 6 ms 3284 KB Output is correct
79 Correct 6 ms 3284 KB Output is correct
80 Correct 6 ms 3284 KB Output is correct
81 Correct 7 ms 3284 KB Output is correct
82 Correct 7 ms 3348 KB Output is correct
83 Correct 9 ms 3284 KB Output is correct
84 Correct 7 ms 3284 KB Output is correct
85 Correct 7 ms 3296 KB Output is correct
86 Correct 7 ms 3284 KB Output is correct
87 Correct 30 ms 4052 KB Output is correct
88 Correct 107 ms 7180 KB Output is correct
89 Correct 622 ms 17716 KB Output is correct
90 Correct 569 ms 17512 KB Output is correct
91 Correct 631 ms 17628 KB Output is correct
92 Correct 114 ms 24892 KB Output is correct
93 Correct 123 ms 24940 KB Output is correct
94 Correct 109 ms 24868 KB Output is correct
95 Correct 189 ms 20172 KB Output is correct
96 Correct 194 ms 20584 KB Output is correct
97 Correct 209 ms 20620 KB Output is correct
98 Correct 259 ms 20620 KB Output is correct
99 Correct 182 ms 20728 KB Output is correct
100 Correct 1308 ms 17860 KB Output is correct
101 Correct 1245 ms 18168 KB Output is correct
102 Correct 1295 ms 17984 KB Output is correct
103 Correct 1270 ms 18064 KB Output is correct
104 Correct 237 ms 20732 KB Output is correct
105 Correct 282 ms 20680 KB Output is correct
106 Correct 269 ms 20652 KB Output is correct
107 Correct 624 ms 16428 KB Output is correct
108 Correct 500 ms 16540 KB Output is correct
109 Correct 564 ms 16964 KB Output is correct
110 Correct 108 ms 23928 KB Output is correct
111 Correct 193 ms 20160 KB Output is correct
112 Correct 233 ms 19684 KB Output is correct
113 Correct 179 ms 19704 KB Output is correct
114 Correct 1302 ms 17772 KB Output is correct
115 Correct 1253 ms 17036 KB Output is correct
116 Correct 226 ms 19668 KB Output is correct
117 Correct 206 ms 18808 KB Output is correct
118 Correct 177 ms 18292 KB Output is correct
119 Correct 186 ms 18052 KB Output is correct
120 Correct 190 ms 18064 KB Output is correct
121 Correct 215 ms 17636 KB Output is correct
122 Correct 190 ms 17340 KB Output is correct
123 Correct 224 ms 18816 KB Output is correct
124 Correct 230 ms 18296 KB Output is correct
125 Correct 237 ms 18124 KB Output is correct
126 Correct 271 ms 18180 KB Output is correct
127 Correct 221 ms 17664 KB Output is correct
128 Correct 281 ms 17568 KB Output is correct