답안 #749182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
749182 2023-05-27T13:10:12 Z stevancv Construction of Highway (JOI18_construction) C++14
100 / 100
749 ms 24172 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int a[N], b[N], c[N], inv[N], n;
vector<int> g[N];
int par[N][17], in[N], out[N], dep[N], tsz;
int st[4 * N];
void Add(int node, int l, int r, int p, int o) {
    if (l == r) {
        st[node] = o;
        return;
    }
    int mid = l + r >> 1;
    if (p <= mid) Add(2 * node, l, mid, p, o);
    else Add(2 * node + 1, mid + 1, r, p, o);
    st[node] = max(st[2 * node], st[2 * node + 1]);
}
int Get(int node, int l, int r, int ql, int qr) {
    if (r < ql || qr < l) return 0;
    if (ql <= l && r <= qr) return st[node];
    int mid = l + r >> 1;
    return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
void Dfs(int s, int e) {
    in[s] = ++tsz;
    par[s][0] = e;
    for (int i = 1; i < 17; i++) par[s][i] = par[par[s][i - 1]][i - 1];
    Add(1, 1, n, in[s], inv[s]);
    for (int u : g[s]) {
        dep[u] = dep[s] + 1;
        Dfs(u, s);
    }
    out[s] = tsz;
}
bool Ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
    if (Ancestor(u, v)) return u;
    if (Ancestor(v, u)) return v;
    for (int i = 16; i >= 0; i--) {
        if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i];
    }
    return par[u][0];
}
ll sol;
struct Fenwick {
    ll bit[N];
    void Add(int x, int y) {
        for (; x < N; x += x & (-x)) bit[x] += y;
    }
    ll Get(int x) {
        ll ans = 0;
        for (; x > 0; x -= x & (-x)) ans += bit[x];
        return ans;
    }
}f;
void Resi(int s, int x) {
    int o = Get(1, 1, n, in[s], out[s]);
    int y = b[o];
    int lca = Lca(x, y);
    ll k = dep[lca] - dep[s] + 1;
    sol += (ll)(k * (f.Get(N - 1) - f.Get(c[y])));
    if (lca == x) return;
    f.Add(c[y], k);
    int l = 0, r = g[lca].size() - 1, gde = -1;
    while (l <= r) {
        int mid = l + r >> 1;
        if (in[g[lca][mid]] <= in[x]) {
            l = mid + 1;
            gde = mid;
        }
        else r = mid - 1;
    }
    Resi(g[lca][gde], x);
    f.Add(c[y], -k);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    vector<int> all;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        all.push_back(c[i]);
    }
    sort(all.begin(), all.end());
    for (int i = 1; i <= n; i++) {
        c[i] = (int)(lower_bound(all.begin(), all.end(), c[i]) - all.begin()) + 1;
    }
    for (int i = 1; i < n; i++) {
        cin >> a[i] >> b[i];
        inv[b[i]] = i;
        g[a[i]].push_back(b[i]);
    }
    Dfs(1, 0);
    vector<int> ans;
    for (int i = n - 1; i >= 1; i--) {
        Add(1, 1, n, in[b[i]], 0);
        sol = 0;
        if (a[i] == 1) {
            ans.push_back(0);
            continue;
        }
        Resi(1, a[i]);
        ans.push_back(sol);
    }
    for (int i = n - 2; i >= 0; i--) cout << ans[i] << en;
    return 0;
}

Compilation message

construction.cpp: In function 'void Add(int, int, int, int, int)':
construction.cpp:20:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   20 |     int mid = l + r >> 1;
      |               ~~^~~
construction.cpp: In function 'int Get(int, int, int, int, int)':
construction.cpp:28:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |     int mid = l + r >> 1;
      |               ~~^~~
construction.cpp: In function 'void Resi(int, int)':
construction.cpp:75:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |         int mid = l + r >> 1;
      |                   ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2684 KB Output is correct
4 Correct 3 ms 2688 KB Output is correct
5 Correct 3 ms 2820 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 2820 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2828 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 3 ms 2820 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2824 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 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 2820 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2820 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2816 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 3 ms 2772 KB Output is correct
38 Correct 3 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 2824 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2684 KB Output is correct
4 Correct 3 ms 2688 KB Output is correct
5 Correct 3 ms 2820 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 2820 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2828 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 3 ms 2820 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2824 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 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 2820 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2820 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2816 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 3 ms 2772 KB Output is correct
38 Correct 3 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 2824 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 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 9 ms 3328 KB Output is correct
47 Correct 9 ms 3284 KB Output is correct
48 Correct 11 ms 3284 KB Output is correct
49 Correct 5 ms 3480 KB Output is correct
50 Correct 5 ms 3540 KB Output is correct
51 Correct 6 ms 3540 KB Output is correct
52 Correct 6 ms 3412 KB Output is correct
53 Correct 6 ms 3412 KB Output is correct
54 Correct 7 ms 3412 KB Output is correct
55 Correct 6 ms 3412 KB Output is correct
56 Correct 5 ms 3412 KB Output is correct
57 Correct 13 ms 3300 KB Output is correct
58 Correct 14 ms 3372 KB Output is correct
59 Correct 14 ms 3284 KB Output is correct
60 Correct 14 ms 3284 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 8 ms 3284 KB Output is correct
65 Correct 8 ms 3336 KB Output is correct
66 Correct 8 ms 3284 KB Output is correct
67 Correct 10 ms 3260 KB Output is correct
68 Correct 5 ms 3540 KB Output is correct
69 Correct 6 ms 3412 KB Output is correct
70 Correct 5 ms 3412 KB Output is correct
71 Correct 6 ms 3412 KB Output is correct
72 Correct 14 ms 3284 KB Output is correct
73 Correct 15 ms 3340 KB Output is correct
74 Correct 7 ms 3412 KB Output is correct
75 Correct 6 ms 3392 KB Output is correct
76 Correct 7 ms 3276 KB Output is correct
77 Correct 6 ms 3284 KB Output is correct
78 Correct 5 ms 3284 KB Output is correct
79 Correct 5 ms 3332 KB Output is correct
80 Correct 5 ms 3332 KB Output is correct
81 Correct 6 ms 3416 KB Output is correct
82 Correct 6 ms 3284 KB Output is correct
83 Correct 6 ms 3284 KB Output is correct
84 Correct 6 ms 3284 KB Output is correct
85 Correct 6 ms 3384 KB Output is correct
86 Correct 6 ms 3332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2684 KB Output is correct
4 Correct 3 ms 2688 KB Output is correct
5 Correct 3 ms 2820 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 2820 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2828 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 3 ms 2820 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2824 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 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 2820 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2820 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2816 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 3 ms 2772 KB Output is correct
38 Correct 3 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 2824 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 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 9 ms 3328 KB Output is correct
47 Correct 9 ms 3284 KB Output is correct
48 Correct 11 ms 3284 KB Output is correct
49 Correct 5 ms 3480 KB Output is correct
50 Correct 5 ms 3540 KB Output is correct
51 Correct 6 ms 3540 KB Output is correct
52 Correct 6 ms 3412 KB Output is correct
53 Correct 6 ms 3412 KB Output is correct
54 Correct 7 ms 3412 KB Output is correct
55 Correct 6 ms 3412 KB Output is correct
56 Correct 5 ms 3412 KB Output is correct
57 Correct 13 ms 3300 KB Output is correct
58 Correct 14 ms 3372 KB Output is correct
59 Correct 14 ms 3284 KB Output is correct
60 Correct 14 ms 3284 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 8 ms 3284 KB Output is correct
65 Correct 8 ms 3336 KB Output is correct
66 Correct 8 ms 3284 KB Output is correct
67 Correct 10 ms 3260 KB Output is correct
68 Correct 5 ms 3540 KB Output is correct
69 Correct 6 ms 3412 KB Output is correct
70 Correct 5 ms 3412 KB Output is correct
71 Correct 6 ms 3412 KB Output is correct
72 Correct 14 ms 3284 KB Output is correct
73 Correct 15 ms 3340 KB Output is correct
74 Correct 7 ms 3412 KB Output is correct
75 Correct 6 ms 3392 KB Output is correct
76 Correct 7 ms 3276 KB Output is correct
77 Correct 6 ms 3284 KB Output is correct
78 Correct 5 ms 3284 KB Output is correct
79 Correct 5 ms 3332 KB Output is correct
80 Correct 5 ms 3332 KB Output is correct
81 Correct 6 ms 3416 KB Output is correct
82 Correct 6 ms 3284 KB Output is correct
83 Correct 6 ms 3284 KB Output is correct
84 Correct 6 ms 3284 KB Output is correct
85 Correct 6 ms 3384 KB Output is correct
86 Correct 6 ms 3332 KB Output is correct
87 Correct 22 ms 4288 KB Output is correct
88 Correct 79 ms 7496 KB Output is correct
89 Correct 408 ms 18696 KB Output is correct
90 Correct 387 ms 18768 KB Output is correct
91 Correct 360 ms 18816 KB Output is correct
92 Correct 159 ms 24124 KB Output is correct
93 Correct 140 ms 24172 KB Output is correct
94 Correct 137 ms 24144 KB Output is correct
95 Correct 163 ms 21064 KB Output is correct
96 Correct 168 ms 21832 KB Output is correct
97 Correct 170 ms 21772 KB Output is correct
98 Correct 170 ms 21812 KB Output is correct
99 Correct 173 ms 21112 KB Output is correct
100 Correct 667 ms 18944 KB Output is correct
101 Correct 749 ms 19232 KB Output is correct
102 Correct 670 ms 19392 KB Output is correct
103 Correct 712 ms 19344 KB Output is correct
104 Correct 224 ms 21188 KB Output is correct
105 Correct 240 ms 21132 KB Output is correct
106 Correct 223 ms 21064 KB Output is correct
107 Correct 327 ms 17304 KB Output is correct
108 Correct 344 ms 17984 KB Output is correct
109 Correct 365 ms 18480 KB Output is correct
110 Correct 124 ms 23584 KB Output is correct
111 Correct 172 ms 20956 KB Output is correct
112 Correct 162 ms 21188 KB Output is correct
113 Correct 153 ms 20544 KB Output is correct
114 Correct 650 ms 19108 KB Output is correct
115 Correct 667 ms 18724 KB Output is correct
116 Correct 201 ms 20620 KB Output is correct
117 Correct 172 ms 19640 KB Output is correct
118 Correct 163 ms 19232 KB Output is correct
119 Correct 144 ms 18924 KB Output is correct
120 Correct 154 ms 19228 KB Output is correct
121 Correct 142 ms 18840 KB Output is correct
122 Correct 153 ms 18684 KB Output is correct
123 Correct 237 ms 19716 KB Output is correct
124 Correct 245 ms 19292 KB Output is correct
125 Correct 223 ms 19084 KB Output is correct
126 Correct 231 ms 19340 KB Output is correct
127 Correct 224 ms 18972 KB Output is correct
128 Correct 219 ms 18744 KB Output is correct