답안 #157159

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
157159 2019-10-09T18:28:17 Z fedoseevtimofey Construction of Highway (JOI18_construction) C++14
100 / 100
683 ms 111204 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e5 + 7;
vector <int> g[N];
int sz[N], st[N], en[N], par[N], in[N], out[N], h[N];

int A[N], B[N], C[N];
deque <pair <int, int>> col[N];

void dfs(int u = 0, int p = -1) {
    par[u] = p;
    for (int i = 0; i < g[u].size(); ++i) {
        if (g[u][i] == p) {
            g[u].erase(g[u].begin() + i);
            break;
        }
    }
    sz[u] = 1;
    for (int v : g[u]) {
        dfs(v, u);
        sz[u] += sz[v];
    }
    for (int i = 1; i < (int)g[u].size(); ++i) {
        if (sz[g[u][i]] > sz[g[u][0]]) swap(g[u][i], g[u][0]);
    }
}

vector <int> e;

int dfs_hld(int u = 0, int head = 0) {
    e.push_back(u);
    in[u] = (int)e.size() - 1;
    st[u] = head;
    if (par[u] != -1) h[u] = h[par[u]] + 1;
    else h[u] = 0;
    if (!g[u].empty()) {
        en[u] = dfs_hld(g[u][0], head);
        for (int i = 1; i < (int)g[u].size(); ++i) {
            dfs_hld(g[u][i], g[u][i]);
        }
    } else {
        en[u] = u;
    }
    out[u] = (int)e.size() - 1;
    return en[u];
}

struct Fenwick {
    vector <int> f;
    int n;
    void modify(int i, int val) {
        i = n - i - 1;
        for (; i < n; i |= i + 1) f[i] += val;
    }
    int get(int r) {
        int res = 0;
        r = n - r - 1;
        --r;
        for (; r >= 0; r &= r + 1, --r) res += f[r];
        return res;
    }
    Fenwick(int nn) {
        n = nn;
        f.resize(n);
    }
};

bool anc(int a, int b) {
    return in[a] <= in[b] && out[b] <= out[a];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> C[i];
    for (int i = 0; i + 1 < n; ++i) {
        cin >> A[i] >> B[i];
        --A[i], --B[i];
        g[A[i]].push_back(B[i]);
        g[B[i]].push_back(A[i]);
    }
    dfs();
    dfs_hld();
    col[0].push_back({C[0], 0});
    for (int id = 0; id + 1 < n; ++id) {
        int ans = 0;
        int u = B[id];
        vector <pair <int, int>> hv;
        while (u != -1) {
            int pth = st[u];
            vector <pair <int, int>> cur;
            for (int i = 0; i < (int)col[pth].size(); ++i) {
                if (anc(col[pth][i].second, u)) {
                    cur.push_back({col[pth][i].first, h[col[pth][i].second] - (i == 0 ? h[pth] - 1: h[col[pth][i - 1].second])});
                } else {
                    cur.push_back({col[pth][i].first, h[u] - (i == 0 ? h[pth] - 1: h[col[pth][i - 1].second])});
                    break;
                }
            }
            reverse(cur.begin(), cur.end());
            for (auto p : cur) hv.push_back(p);
            while (!col[pth].empty() && anc(col[pth][0].second, B[id])) col[pth].pop_front();
            col[pth].push_front({C[B[id]], u});
            u = par[st[u]];
        }   
        reverse(hv.begin(), hv.end());
        vector <int> comp;
        for (auto p : hv) comp.push_back(p.first);
        sort(comp.begin(), comp.end());
        comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
        for (auto &p : hv) p.first = lower_bound(comp.begin(), comp.end(), p.first) - comp.begin();
        Fenwick f(comp.size());
        for (auto p : hv) {
            ans += p.second * f.get(p.first);
            f.modify(p.first, p.second);
        }
        cout << ans << '\n';
    }
}   

Compilation message

construction.cpp: In function 'void dfs(int, int)':
construction.cpp:17:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[u].size(); ++i) {
                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 70008 KB Output is correct
2 Correct 75 ms 70084 KB Output is correct
3 Correct 78 ms 70136 KB Output is correct
4 Correct 101 ms 70108 KB Output is correct
5 Correct 99 ms 70264 KB Output is correct
6 Correct 101 ms 70268 KB Output is correct
7 Correct 98 ms 70264 KB Output is correct
8 Correct 77 ms 70136 KB Output is correct
9 Correct 74 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 86 ms 70180 KB Output is correct
12 Correct 80 ms 70184 KB Output is correct
13 Correct 79 ms 70264 KB Output is correct
14 Correct 79 ms 70392 KB Output is correct
15 Correct 79 ms 70264 KB Output is correct
16 Correct 79 ms 70236 KB Output is correct
17 Correct 80 ms 70264 KB Output is correct
18 Correct 79 ms 70236 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 80 ms 70288 KB Output is correct
21 Correct 99 ms 70268 KB Output is correct
22 Correct 79 ms 70260 KB Output is correct
23 Correct 98 ms 70264 KB Output is correct
24 Correct 80 ms 70264 KB Output is correct
25 Correct 79 ms 70392 KB Output is correct
26 Correct 73 ms 70264 KB Output is correct
27 Correct 83 ms 70360 KB Output is correct
28 Correct 80 ms 70392 KB Output is correct
29 Correct 99 ms 70268 KB Output is correct
30 Correct 98 ms 70264 KB Output is correct
31 Correct 81 ms 70236 KB Output is correct
32 Correct 97 ms 70184 KB Output is correct
33 Correct 87 ms 70264 KB Output is correct
34 Correct 90 ms 70280 KB Output is correct
35 Correct 91 ms 70280 KB Output is correct
36 Correct 77 ms 70264 KB Output is correct
37 Correct 100 ms 70236 KB Output is correct
38 Correct 79 ms 70264 KB Output is correct
39 Correct 82 ms 70264 KB Output is correct
40 Correct 81 ms 70392 KB Output is correct
41 Correct 79 ms 70264 KB Output is correct
42 Correct 80 ms 70200 KB Output is correct
43 Correct 80 ms 70312 KB Output is correct
44 Correct 78 ms 70272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 70008 KB Output is correct
2 Correct 75 ms 70084 KB Output is correct
3 Correct 78 ms 70136 KB Output is correct
4 Correct 101 ms 70108 KB Output is correct
5 Correct 99 ms 70264 KB Output is correct
6 Correct 101 ms 70268 KB Output is correct
7 Correct 98 ms 70264 KB Output is correct
8 Correct 77 ms 70136 KB Output is correct
9 Correct 74 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 86 ms 70180 KB Output is correct
12 Correct 80 ms 70184 KB Output is correct
13 Correct 79 ms 70264 KB Output is correct
14 Correct 79 ms 70392 KB Output is correct
15 Correct 79 ms 70264 KB Output is correct
16 Correct 79 ms 70236 KB Output is correct
17 Correct 80 ms 70264 KB Output is correct
18 Correct 79 ms 70236 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 80 ms 70288 KB Output is correct
21 Correct 99 ms 70268 KB Output is correct
22 Correct 79 ms 70260 KB Output is correct
23 Correct 98 ms 70264 KB Output is correct
24 Correct 80 ms 70264 KB Output is correct
25 Correct 79 ms 70392 KB Output is correct
26 Correct 73 ms 70264 KB Output is correct
27 Correct 83 ms 70360 KB Output is correct
28 Correct 80 ms 70392 KB Output is correct
29 Correct 99 ms 70268 KB Output is correct
30 Correct 98 ms 70264 KB Output is correct
31 Correct 81 ms 70236 KB Output is correct
32 Correct 97 ms 70184 KB Output is correct
33 Correct 87 ms 70264 KB Output is correct
34 Correct 90 ms 70280 KB Output is correct
35 Correct 91 ms 70280 KB Output is correct
36 Correct 77 ms 70264 KB Output is correct
37 Correct 100 ms 70236 KB Output is correct
38 Correct 79 ms 70264 KB Output is correct
39 Correct 82 ms 70264 KB Output is correct
40 Correct 81 ms 70392 KB Output is correct
41 Correct 79 ms 70264 KB Output is correct
42 Correct 80 ms 70200 KB Output is correct
43 Correct 80 ms 70312 KB Output is correct
44 Correct 78 ms 70272 KB Output is correct
45 Correct 81 ms 70404 KB Output is correct
46 Correct 89 ms 71528 KB Output is correct
47 Correct 89 ms 71524 KB Output is correct
48 Correct 89 ms 71520 KB Output is correct
49 Correct 77 ms 70752 KB Output is correct
50 Correct 72 ms 70804 KB Output is correct
51 Correct 76 ms 70748 KB Output is correct
52 Correct 85 ms 71712 KB Output is correct
53 Correct 86 ms 71640 KB Output is correct
54 Correct 85 ms 71672 KB Output is correct
55 Correct 86 ms 71644 KB Output is correct
56 Correct 85 ms 71672 KB Output is correct
57 Correct 89 ms 71416 KB Output is correct
58 Correct 90 ms 71416 KB Output is correct
59 Correct 90 ms 71416 KB Output is correct
60 Correct 90 ms 71376 KB Output is correct
61 Correct 87 ms 71544 KB Output is correct
62 Correct 85 ms 71672 KB Output is correct
63 Correct 86 ms 71544 KB Output is correct
64 Correct 91 ms 71428 KB Output is correct
65 Correct 114 ms 71416 KB Output is correct
66 Correct 98 ms 71544 KB Output is correct
67 Correct 110 ms 71416 KB Output is correct
68 Correct 102 ms 70648 KB Output is correct
69 Correct 86 ms 71672 KB Output is correct
70 Correct 84 ms 71800 KB Output is correct
71 Correct 86 ms 71696 KB Output is correct
72 Correct 89 ms 71544 KB Output is correct
73 Correct 90 ms 71416 KB Output is correct
74 Correct 105 ms 71544 KB Output is correct
75 Correct 101 ms 71512 KB Output is correct
76 Correct 104 ms 71544 KB Output is correct
77 Correct 111 ms 71544 KB Output is correct
78 Correct 102 ms 71672 KB Output is correct
79 Correct 107 ms 71516 KB Output is correct
80 Correct 88 ms 71416 KB Output is correct
81 Correct 102 ms 71704 KB Output is correct
82 Correct 85 ms 71544 KB Output is correct
83 Correct 96 ms 71672 KB Output is correct
84 Correct 105 ms 71544 KB Output is correct
85 Correct 85 ms 71508 KB Output is correct
86 Correct 87 ms 71544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 70008 KB Output is correct
2 Correct 75 ms 70084 KB Output is correct
3 Correct 78 ms 70136 KB Output is correct
4 Correct 101 ms 70108 KB Output is correct
5 Correct 99 ms 70264 KB Output is correct
6 Correct 101 ms 70268 KB Output is correct
7 Correct 98 ms 70264 KB Output is correct
8 Correct 77 ms 70136 KB Output is correct
9 Correct 74 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 86 ms 70180 KB Output is correct
12 Correct 80 ms 70184 KB Output is correct
13 Correct 79 ms 70264 KB Output is correct
14 Correct 79 ms 70392 KB Output is correct
15 Correct 79 ms 70264 KB Output is correct
16 Correct 79 ms 70236 KB Output is correct
17 Correct 80 ms 70264 KB Output is correct
18 Correct 79 ms 70236 KB Output is correct
19 Correct 79 ms 70264 KB Output is correct
20 Correct 80 ms 70288 KB Output is correct
21 Correct 99 ms 70268 KB Output is correct
22 Correct 79 ms 70260 KB Output is correct
23 Correct 98 ms 70264 KB Output is correct
24 Correct 80 ms 70264 KB Output is correct
25 Correct 79 ms 70392 KB Output is correct
26 Correct 73 ms 70264 KB Output is correct
27 Correct 83 ms 70360 KB Output is correct
28 Correct 80 ms 70392 KB Output is correct
29 Correct 99 ms 70268 KB Output is correct
30 Correct 98 ms 70264 KB Output is correct
31 Correct 81 ms 70236 KB Output is correct
32 Correct 97 ms 70184 KB Output is correct
33 Correct 87 ms 70264 KB Output is correct
34 Correct 90 ms 70280 KB Output is correct
35 Correct 91 ms 70280 KB Output is correct
36 Correct 77 ms 70264 KB Output is correct
37 Correct 100 ms 70236 KB Output is correct
38 Correct 79 ms 70264 KB Output is correct
39 Correct 82 ms 70264 KB Output is correct
40 Correct 81 ms 70392 KB Output is correct
41 Correct 79 ms 70264 KB Output is correct
42 Correct 80 ms 70200 KB Output is correct
43 Correct 80 ms 70312 KB Output is correct
44 Correct 78 ms 70272 KB Output is correct
45 Correct 81 ms 70404 KB Output is correct
46 Correct 89 ms 71528 KB Output is correct
47 Correct 89 ms 71524 KB Output is correct
48 Correct 89 ms 71520 KB Output is correct
49 Correct 77 ms 70752 KB Output is correct
50 Correct 72 ms 70804 KB Output is correct
51 Correct 76 ms 70748 KB Output is correct
52 Correct 85 ms 71712 KB Output is correct
53 Correct 86 ms 71640 KB Output is correct
54 Correct 85 ms 71672 KB Output is correct
55 Correct 86 ms 71644 KB Output is correct
56 Correct 85 ms 71672 KB Output is correct
57 Correct 89 ms 71416 KB Output is correct
58 Correct 90 ms 71416 KB Output is correct
59 Correct 90 ms 71416 KB Output is correct
60 Correct 90 ms 71376 KB Output is correct
61 Correct 87 ms 71544 KB Output is correct
62 Correct 85 ms 71672 KB Output is correct
63 Correct 86 ms 71544 KB Output is correct
64 Correct 91 ms 71428 KB Output is correct
65 Correct 114 ms 71416 KB Output is correct
66 Correct 98 ms 71544 KB Output is correct
67 Correct 110 ms 71416 KB Output is correct
68 Correct 102 ms 70648 KB Output is correct
69 Correct 86 ms 71672 KB Output is correct
70 Correct 84 ms 71800 KB Output is correct
71 Correct 86 ms 71696 KB Output is correct
72 Correct 89 ms 71544 KB Output is correct
73 Correct 90 ms 71416 KB Output is correct
74 Correct 105 ms 71544 KB Output is correct
75 Correct 101 ms 71512 KB Output is correct
76 Correct 104 ms 71544 KB Output is correct
77 Correct 111 ms 71544 KB Output is correct
78 Correct 102 ms 71672 KB Output is correct
79 Correct 107 ms 71516 KB Output is correct
80 Correct 88 ms 71416 KB Output is correct
81 Correct 102 ms 71704 KB Output is correct
82 Correct 85 ms 71544 KB Output is correct
83 Correct 96 ms 71672 KB Output is correct
84 Correct 105 ms 71544 KB Output is correct
85 Correct 85 ms 71508 KB Output is correct
86 Correct 87 ms 71544 KB Output is correct
87 Correct 117 ms 73780 KB Output is correct
88 Correct 175 ms 80860 KB Output is correct
89 Correct 479 ms 105588 KB Output is correct
90 Correct 469 ms 105440 KB Output is correct
91 Correct 495 ms 105552 KB Output is correct
92 Correct 307 ms 87392 KB Output is correct
93 Correct 206 ms 87460 KB Output is correct
94 Correct 226 ms 87528 KB Output is correct
95 Correct 332 ms 109676 KB Output is correct
96 Correct 328 ms 109120 KB Output is correct
97 Correct 267 ms 111204 KB Output is correct
98 Correct 267 ms 111060 KB Output is correct
99 Correct 271 ms 109756 KB Output is correct
100 Correct 496 ms 97360 KB Output is correct
101 Correct 572 ms 97684 KB Output is correct
102 Correct 683 ms 97904 KB Output is correct
103 Correct 569 ms 97776 KB Output is correct
104 Correct 277 ms 109788 KB Output is correct
105 Correct 321 ms 109600 KB Output is correct
106 Correct 282 ms 109660 KB Output is correct
107 Correct 551 ms 105036 KB Output is correct
108 Correct 509 ms 105248 KB Output is correct
109 Correct 583 ms 105460 KB Output is correct
110 Correct 197 ms 87116 KB Output is correct
111 Correct 254 ms 110704 KB Output is correct
112 Correct 252 ms 110576 KB Output is correct
113 Correct 276 ms 109116 KB Output is correct
114 Correct 568 ms 97380 KB Output is correct
115 Correct 605 ms 97068 KB Output is correct
116 Correct 271 ms 109128 KB Output is correct
117 Correct 287 ms 107404 KB Output is correct
118 Correct 280 ms 106708 KB Output is correct
119 Correct 281 ms 106352 KB Output is correct
120 Correct 333 ms 107004 KB Output is correct
121 Correct 347 ms 106540 KB Output is correct
122 Correct 298 ms 106112 KB Output is correct
123 Correct 296 ms 107456 KB Output is correct
124 Correct 311 ms 106764 KB Output is correct
125 Correct 311 ms 106480 KB Output is correct
126 Correct 328 ms 107220 KB Output is correct
127 Correct 356 ms 106480 KB Output is correct
128 Correct 290 ms 106220 KB Output is correct