답안 #985592

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985592 2024-05-18T09:22:25 Z alextodoran Construction of Highway (JOI18_construction) C++17
100 / 100
216 ms 29700 KB
/**
 _  _   __  _ _ _  _  _ _
 |a  ||t  ||o    d | |o  |
| __    _| | _ | __|  _ |
| __ |/_  | __  /__\ / _\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100000;

int N;
int C[N_MAX + 2];
int A[N_MAX + 2], B[N_MAX + 2];
vector <int> adj[N_MAX + 2];

int order[N_MAX + 2];
void compress () {
    iota(order + 1, order + N + 1, 1);
    sort(order + 1, order + N + 1, [&] (const int &u, const int &v) {
        return C[u] < C[v];
    });
    int curr = 0, last = 0;
    for (int i = 1; i <= N; i++) {
        if (C[order[i]] > last) {
            last = C[order[i]];
            curr++;
        }
        C[order[i]] = curr;
    }
}

int parent[N_MAX + 2];
int sub[N_MAX + 2];

void dfs (int u) {
    sub[u] = 1;
    for (int v : adj[u]) {
        if (v != parent[u]) {
            parent[v] = u;
            dfs(v);
            sub[u] += sub[v];
        }
    }
}

int cnt_chains;
vector <int> chain[N_MAX + 2];
int chain_id[N_MAX + 2];
int chain_pos[N_MAX + 2];

void hld (int u) {
    chain_pos[u] = (int) chain[cnt_chains].size();
    chain[cnt_chains].push_back(u);
    chain_id[u] = cnt_chains;
    int heavy = 0;
    for (int v : adj[u]) {
        if (v != parent[u] && sub[v] > sub[heavy]) {
            heavy = v;
        }
    }
    if (heavy != 0) {
        hld(heavy);
        for (int v : adj[u]) {
            if (v != parent[u] && v != heavy) {
                cnt_chains++;
                hld(v);
            }
        }
    }
}

struct SegInfo {
    int mn, mx;
    int lazy;
    void update (int val) {
        mn = mx = val;
        lazy = val;
    }
};
SegInfo operator + (const SegInfo &A, const SegInfo &B) {
    return SegInfo{min(A.mn, B.mn), max(A.mx, B.mx), 0};
}

vector <SegInfo> seg_tree[N_MAX + 2];

void build () {
    for (int c = 1; c <= cnt_chains; c++) {
        seg_tree[c].resize((int) chain[c].size() * 4 + 2);
    }
}

void update (int c, int node, int l, int r, int i, int val) {
    if (r <= i) {
        seg_tree[c][node].update(val);
        return;
    }
    int mid = (l + r) / 2;
    int left = node * 2, right = node * 2 + 1;
    if (seg_tree[c][node].lazy != 0) {
        seg_tree[c][left].update(seg_tree[c][node].lazy);
        seg_tree[c][right].update(seg_tree[c][node].lazy);
        seg_tree[c][node].lazy = 0;
    }
    update(c, left, l, mid, i, val);
    if (mid + 1 <= i) {
        update(c, right, mid + 1, r, i, val);
    }
    seg_tree[c][node] = seg_tree[c][left] + seg_tree[c][right];
}
void update (int c, int i, int val) {
    update(c, 1, 0, (int) chain[c].size() - 1, i, val);
}

vector <pair <int, int>> blocks;
void add_block (pair <int, int> block) {
    if (blocks.empty() == false && blocks.back().first == block.first) {
        blocks.back().second += block.second;
    } else {
        blocks.push_back(block);
    }
}
void query (int c, int node, int l, int r, int i) {
    if (r <= i && seg_tree[c][node].mn == seg_tree[c][node].mx) {
        add_block(make_pair(seg_tree[c][node].mn, r - l + 1));
        return;
    }
    int mid = (l + r) / 2;
    int left = node * 2, right = node * 2 + 1;
    if (seg_tree[c][node].lazy != 0) {
        seg_tree[c][left].update(seg_tree[c][node].lazy);
        seg_tree[c][right].update(seg_tree[c][node].lazy);
        seg_tree[c][node].lazy = 0;
    }
    if (mid + 1 <= i) {
        query(c, right, mid + 1, r, i);
    }
    query(c, left, l, mid, i);
}
void query (int c, int i) {
    query(c, 1, 0, (int) chain[c].size() - 1, i);
}

int Fen[N_MAX + 2];
void update_Fen (int pos, int val) {
    for (int i = pos; i <= N; i += i & -i) {
        Fen[i] += val;
    }
}
int query_Fen (int pos) {
    int sum = 0;
    for (int i = pos; i >= 1; i -= i & -i) {
        sum += Fen[i];
    }
    return sum;
}

ll solve (int start) {
//    cout << "solve(" << start << "): ";
    int u = start;
    while (u != 0) {
        int c = chain_id[u];
//        cout << "Chain #" << c << " (" << chain[c][0] << ".." << u << ") [" << chain_pos[u] << "] ";
//        cout << "C = " << C[start] << "\n";
        if (chain_pos[u] - (u == start) >= 0) {
            query(c, chain_pos[u] - (u == start));
        }
        update(c, chain_pos[u], C[start]);
        u = parent[chain[c][0]];
    }
    ll answer = 0;
    for (pair <int, int> block : blocks) {
//        cout << "Block " << block.first << " of size " << block.second << "\n";
        answer += (ll) block.second * query_Fen(block.first - 1);
        update_Fen(block.first, block.second);
    }
    for (pair <int, int> block : blocks) {
        update_Fen(block.first, -block.second);
    }
    blocks.clear();
    return answer;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N;
    for (int u = 1; u <= N; u++) {
        cin >> C[u];
    }
    for (int i = 1; i <= N - 1; i++) {
        cin >> A[i] >> B[i];
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    compress();
    dfs(1);
    cnt_chains = 1; hld(1);
    build();
    solve(1);
    for (int i = 1; i <= N - 1; i++) {
        cout << solve(B[i]) << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10672 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10796 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10680 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 3 ms 10588 KB Output is correct
29 Correct 2 ms 10584 KB Output is correct
30 Correct 3 ms 10588 KB Output is correct
31 Correct 3 ms 10588 KB Output is correct
32 Correct 2 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10588 KB Output is correct
36 Correct 3 ms 10588 KB Output is correct
37 Correct 2 ms 10588 KB Output is correct
38 Correct 2 ms 10588 KB Output is correct
39 Correct 2 ms 10588 KB Output is correct
40 Correct 3 ms 10588 KB Output is correct
41 Correct 3 ms 10588 KB Output is correct
42 Correct 2 ms 10588 KB Output is correct
43 Correct 5 ms 10584 KB Output is correct
44 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10672 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10796 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10680 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 3 ms 10588 KB Output is correct
29 Correct 2 ms 10584 KB Output is correct
30 Correct 3 ms 10588 KB Output is correct
31 Correct 3 ms 10588 KB Output is correct
32 Correct 2 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10588 KB Output is correct
36 Correct 3 ms 10588 KB Output is correct
37 Correct 2 ms 10588 KB Output is correct
38 Correct 2 ms 10588 KB Output is correct
39 Correct 2 ms 10588 KB Output is correct
40 Correct 3 ms 10588 KB Output is correct
41 Correct 3 ms 10588 KB Output is correct
42 Correct 2 ms 10588 KB Output is correct
43 Correct 5 ms 10584 KB Output is correct
44 Correct 2 ms 10588 KB Output is correct
45 Correct 3 ms 10844 KB Output is correct
46 Correct 6 ms 11248 KB Output is correct
47 Correct 6 ms 11100 KB Output is correct
48 Correct 6 ms 11096 KB Output is correct
49 Correct 5 ms 11356 KB Output is correct
50 Correct 6 ms 11312 KB Output is correct
51 Correct 5 ms 11356 KB Output is correct
52 Correct 5 ms 11352 KB Output is correct
53 Correct 5 ms 11352 KB Output is correct
54 Correct 6 ms 11308 KB Output is correct
55 Correct 5 ms 11248 KB Output is correct
56 Correct 5 ms 11356 KB Output is correct
57 Correct 6 ms 11100 KB Output is correct
58 Correct 6 ms 11100 KB Output is correct
59 Correct 7 ms 11260 KB Output is correct
60 Correct 7 ms 11096 KB Output is correct
61 Correct 5 ms 11352 KB Output is correct
62 Correct 5 ms 11356 KB Output is correct
63 Correct 5 ms 11352 KB Output is correct
64 Correct 5 ms 11100 KB Output is correct
65 Correct 6 ms 11356 KB Output is correct
66 Correct 5 ms 11100 KB Output is correct
67 Correct 5 ms 11100 KB Output is correct
68 Correct 5 ms 11416 KB Output is correct
69 Correct 5 ms 11356 KB Output is correct
70 Correct 5 ms 11336 KB Output is correct
71 Correct 5 ms 11356 KB Output is correct
72 Correct 6 ms 11100 KB Output is correct
73 Correct 6 ms 11100 KB Output is correct
74 Correct 5 ms 11356 KB Output is correct
75 Correct 5 ms 11100 KB Output is correct
76 Correct 5 ms 11100 KB Output is correct
77 Correct 5 ms 11096 KB Output is correct
78 Correct 5 ms 11100 KB Output is correct
79 Correct 5 ms 11100 KB Output is correct
80 Correct 5 ms 11100 KB Output is correct
81 Correct 5 ms 11100 KB Output is correct
82 Correct 6 ms 11312 KB Output is correct
83 Correct 5 ms 11100 KB Output is correct
84 Correct 5 ms 11100 KB Output is correct
85 Correct 5 ms 11284 KB Output is correct
86 Correct 5 ms 11100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10672 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10796 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10680 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 3 ms 10588 KB Output is correct
29 Correct 2 ms 10584 KB Output is correct
30 Correct 3 ms 10588 KB Output is correct
31 Correct 3 ms 10588 KB Output is correct
32 Correct 2 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10588 KB Output is correct
36 Correct 3 ms 10588 KB Output is correct
37 Correct 2 ms 10588 KB Output is correct
38 Correct 2 ms 10588 KB Output is correct
39 Correct 2 ms 10588 KB Output is correct
40 Correct 3 ms 10588 KB Output is correct
41 Correct 3 ms 10588 KB Output is correct
42 Correct 2 ms 10588 KB Output is correct
43 Correct 5 ms 10584 KB Output is correct
44 Correct 2 ms 10588 KB Output is correct
45 Correct 3 ms 10844 KB Output is correct
46 Correct 6 ms 11248 KB Output is correct
47 Correct 6 ms 11100 KB Output is correct
48 Correct 6 ms 11096 KB Output is correct
49 Correct 5 ms 11356 KB Output is correct
50 Correct 6 ms 11312 KB Output is correct
51 Correct 5 ms 11356 KB Output is correct
52 Correct 5 ms 11352 KB Output is correct
53 Correct 5 ms 11352 KB Output is correct
54 Correct 6 ms 11308 KB Output is correct
55 Correct 5 ms 11248 KB Output is correct
56 Correct 5 ms 11356 KB Output is correct
57 Correct 6 ms 11100 KB Output is correct
58 Correct 6 ms 11100 KB Output is correct
59 Correct 7 ms 11260 KB Output is correct
60 Correct 7 ms 11096 KB Output is correct
61 Correct 5 ms 11352 KB Output is correct
62 Correct 5 ms 11356 KB Output is correct
63 Correct 5 ms 11352 KB Output is correct
64 Correct 5 ms 11100 KB Output is correct
65 Correct 6 ms 11356 KB Output is correct
66 Correct 5 ms 11100 KB Output is correct
67 Correct 5 ms 11100 KB Output is correct
68 Correct 5 ms 11416 KB Output is correct
69 Correct 5 ms 11356 KB Output is correct
70 Correct 5 ms 11336 KB Output is correct
71 Correct 5 ms 11356 KB Output is correct
72 Correct 6 ms 11100 KB Output is correct
73 Correct 6 ms 11100 KB Output is correct
74 Correct 5 ms 11356 KB Output is correct
75 Correct 5 ms 11100 KB Output is correct
76 Correct 5 ms 11100 KB Output is correct
77 Correct 5 ms 11096 KB Output is correct
78 Correct 5 ms 11100 KB Output is correct
79 Correct 5 ms 11100 KB Output is correct
80 Correct 5 ms 11100 KB Output is correct
81 Correct 5 ms 11100 KB Output is correct
82 Correct 6 ms 11312 KB Output is correct
83 Correct 5 ms 11100 KB Output is correct
84 Correct 5 ms 11100 KB Output is correct
85 Correct 5 ms 11284 KB Output is correct
86 Correct 5 ms 11100 KB Output is correct
87 Correct 12 ms 11868 KB Output is correct
88 Correct 37 ms 14872 KB Output is correct
89 Correct 141 ms 24392 KB Output is correct
90 Correct 147 ms 24428 KB Output is correct
91 Correct 140 ms 24376 KB Output is correct
92 Correct 83 ms 29700 KB Output is correct
93 Correct 81 ms 29388 KB Output is correct
94 Correct 83 ms 29272 KB Output is correct
95 Correct 85 ms 28620 KB Output is correct
96 Correct 90 ms 29032 KB Output is correct
97 Correct 91 ms 29104 KB Output is correct
98 Correct 89 ms 29136 KB Output is correct
99 Correct 94 ms 28368 KB Output is correct
100 Correct 168 ms 23100 KB Output is correct
101 Correct 189 ms 23376 KB Output is correct
102 Correct 200 ms 23272 KB Output is correct
103 Correct 216 ms 23436 KB Output is correct
104 Correct 107 ms 28392 KB Output is correct
105 Correct 129 ms 28276 KB Output is correct
106 Correct 114 ms 28620 KB Output is correct
107 Correct 111 ms 23376 KB Output is correct
108 Correct 128 ms 23540 KB Output is correct
109 Correct 138 ms 24044 KB Output is correct
110 Correct 88 ms 28860 KB Output is correct
111 Correct 83 ms 28700 KB Output is correct
112 Correct 84 ms 28288 KB Output is correct
113 Correct 88 ms 27724 KB Output is correct
114 Correct 188 ms 23332 KB Output is correct
115 Correct 179 ms 22656 KB Output is correct
116 Correct 101 ms 27600 KB Output is correct
117 Correct 93 ms 26196 KB Output is correct
118 Correct 100 ms 25520 KB Output is correct
119 Correct 95 ms 25100 KB Output is correct
120 Correct 86 ms 25652 KB Output is correct
121 Correct 88 ms 25160 KB Output is correct
122 Correct 94 ms 24860 KB Output is correct
123 Correct 115 ms 26300 KB Output is correct
124 Correct 118 ms 25472 KB Output is correct
125 Correct 117 ms 25112 KB Output is correct
126 Correct 120 ms 25848 KB Output is correct
127 Correct 110 ms 25172 KB Output is correct
128 Correct 109 ms 24656 KB Output is correct