Submission #130242

# Submission time Handle Problem Language Result Execution time Memory
130242 2019-07-14T11:15:06 Z darkkcyan Construction of Highway (JOI18_construction) C++14
100 / 100
542 ms 113520 KB
#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;

#define llong long long 
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
// #define rand __rand
// mt19937 rand(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64

#define maxn 101010
int n;
int c[maxn];
vector<int> gr[maxn];
int parent[maxn];
int query[maxn];

int subtree_size[maxn];
shared_ptr<int> chain_head[maxn];
int chain_id[maxn];
void build_hld(int u) {
    subtree_size[u] = 1;
    for (auto v: gr[u]) {
        build_hld(v);
        subtree_size[u] += subtree_size[v];
        if (!chain_head[u] or subtree_size[*chain_head[u]] < subtree_size[*chain_head[v]])
            chain_head[u] = chain_head[v];
    }
    if (!chain_head[u]) chain_head[u].reset(new int());
    chain_id[u] = chain_id[*chain_head[u]] + 1;
    *chain_head[u] = u;
}

deque<pair<int, int>> chain_data[maxn];
vector<pair<int, int>> do_operation(int u, int liveliness) {
    if (u == 0) return {};
    int head = *chain_head[u];
    auto ans = do_operation(parent[head], liveliness);
    int n_remove = chain_id[head] - chain_id[u] + 1;
    int s = n_remove;

    while (s > 0 and chain_data[head].size()) {   // check the size just for safety.
        int n_removing = min(s, chain_data[head].front().yy);
        s -= n_removing;
        chain_data[head].front().yy -= n_removing;
        ans.emplace_back(chain_data[head].front().xx, n_removing);
        if (chain_data[head].front().yy == 0) chain_data[head].pop_front();
    }

    if (s > 0) {
        assert(false);
    }

    chain_data[head].emplace_front(liveliness, n_remove);
    return move(ans);
}

struct Bit {
    vector<llong> data;
    Bit(int size) : data(size + 10) {}
    void upd(int i, llong val) { for (++i; i < len(data); i += i & -i) data[i] += val; }
    llong get(int i) { llong ans = 0; for (++i; i > 0; i -= i & -i) ans += data[i]; return ans; }
};

llong compute_cost(const vector<pair<int, int>>& chain) {
    vector<int> vals;
    for (auto i: chain) vals.push_back(i.xx);
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
    llong ans = 0;
    Bit bit(len(vals));
    for (int i = len(chain); i--; ) {
        int cur_val = (int)(lower_bound(vals.begin(), vals.end(), chain[i].xx) - vals.begin());
        llong cur_ans = 1ll * chain[i].yy * bit.get(cur_val - 1);
        ans += cur_ans;
        bit.upd(cur_val, chain[i].yy);
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    rep1(i, n) cin >> c[i];
    parent[1] = 0;
    rep(i, n - 1) {
        int p, u; cin >> p >> u;
        query[i] = u;
        parent[u] = p;
        gr[p].push_back(u);
    }

    build_hld(1);
    chain_data[1].emplace_back(c[1], 1);
    rep(i, n - 1) {
        int u = query[i];
        cout << compute_cost(do_operation(parent[u], c[u])) << '\n';
        int head = *chain_head[u];
        if (head == u) {
            chain_data[head].emplace_front(c[u], 1);
        } else {
            assert(len(chain_data[head]) == 1);
            chain_data[head].front().yy ++;
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70776 KB Output is correct
2 Correct 72 ms 70776 KB Output is correct
3 Correct 78 ms 70728 KB Output is correct
4 Correct 80 ms 70904 KB Output is correct
5 Correct 80 ms 71032 KB Output is correct
6 Correct 78 ms 70876 KB Output is correct
7 Correct 79 ms 70904 KB Output is correct
8 Correct 72 ms 70820 KB Output is correct
9 Correct 73 ms 70776 KB Output is correct
10 Correct 73 ms 70908 KB Output is correct
11 Correct 80 ms 70956 KB Output is correct
12 Correct 78 ms 70904 KB Output is correct
13 Correct 78 ms 70944 KB Output is correct
14 Correct 78 ms 70948 KB Output is correct
15 Correct 78 ms 70896 KB Output is correct
16 Correct 79 ms 70904 KB Output is correct
17 Correct 84 ms 71000 KB Output is correct
18 Correct 79 ms 70904 KB Output is correct
19 Correct 78 ms 70904 KB Output is correct
20 Correct 79 ms 70984 KB Output is correct
21 Correct 80 ms 70904 KB Output is correct
22 Correct 81 ms 70904 KB Output is correct
23 Correct 79 ms 70904 KB Output is correct
24 Correct 79 ms 71032 KB Output is correct
25 Correct 81 ms 70904 KB Output is correct
26 Correct 75 ms 70904 KB Output is correct
27 Correct 79 ms 70904 KB Output is correct
28 Correct 79 ms 70864 KB Output is correct
29 Correct 109 ms 70892 KB Output is correct
30 Correct 80 ms 70904 KB Output is correct
31 Correct 96 ms 70904 KB Output is correct
32 Correct 81 ms 70928 KB Output is correct
33 Correct 79 ms 70904 KB Output is correct
34 Correct 80 ms 71032 KB Output is correct
35 Correct 98 ms 70904 KB Output is correct
36 Correct 81 ms 70904 KB Output is correct
37 Correct 79 ms 70904 KB Output is correct
38 Correct 80 ms 70904 KB Output is correct
39 Correct 79 ms 70968 KB Output is correct
40 Correct 78 ms 70908 KB Output is correct
41 Correct 79 ms 70904 KB Output is correct
42 Correct 82 ms 70904 KB Output is correct
43 Correct 79 ms 70904 KB Output is correct
44 Correct 80 ms 71032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70776 KB Output is correct
2 Correct 72 ms 70776 KB Output is correct
3 Correct 78 ms 70728 KB Output is correct
4 Correct 80 ms 70904 KB Output is correct
5 Correct 80 ms 71032 KB Output is correct
6 Correct 78 ms 70876 KB Output is correct
7 Correct 79 ms 70904 KB Output is correct
8 Correct 72 ms 70820 KB Output is correct
9 Correct 73 ms 70776 KB Output is correct
10 Correct 73 ms 70908 KB Output is correct
11 Correct 80 ms 70956 KB Output is correct
12 Correct 78 ms 70904 KB Output is correct
13 Correct 78 ms 70944 KB Output is correct
14 Correct 78 ms 70948 KB Output is correct
15 Correct 78 ms 70896 KB Output is correct
16 Correct 79 ms 70904 KB Output is correct
17 Correct 84 ms 71000 KB Output is correct
18 Correct 79 ms 70904 KB Output is correct
19 Correct 78 ms 70904 KB Output is correct
20 Correct 79 ms 70984 KB Output is correct
21 Correct 80 ms 70904 KB Output is correct
22 Correct 81 ms 70904 KB Output is correct
23 Correct 79 ms 70904 KB Output is correct
24 Correct 79 ms 71032 KB Output is correct
25 Correct 81 ms 70904 KB Output is correct
26 Correct 75 ms 70904 KB Output is correct
27 Correct 79 ms 70904 KB Output is correct
28 Correct 79 ms 70864 KB Output is correct
29 Correct 109 ms 70892 KB Output is correct
30 Correct 80 ms 70904 KB Output is correct
31 Correct 96 ms 70904 KB Output is correct
32 Correct 81 ms 70928 KB Output is correct
33 Correct 79 ms 70904 KB Output is correct
34 Correct 80 ms 71032 KB Output is correct
35 Correct 98 ms 70904 KB Output is correct
36 Correct 81 ms 70904 KB Output is correct
37 Correct 79 ms 70904 KB Output is correct
38 Correct 80 ms 70904 KB Output is correct
39 Correct 79 ms 70968 KB Output is correct
40 Correct 78 ms 70908 KB Output is correct
41 Correct 79 ms 70904 KB Output is correct
42 Correct 82 ms 70904 KB Output is correct
43 Correct 79 ms 70904 KB Output is correct
44 Correct 80 ms 71032 KB Output is correct
45 Correct 81 ms 71104 KB Output is correct
46 Correct 89 ms 72312 KB Output is correct
47 Correct 88 ms 72184 KB Output is correct
48 Correct 87 ms 72184 KB Output is correct
49 Correct 77 ms 71416 KB Output is correct
50 Correct 77 ms 71608 KB Output is correct
51 Correct 77 ms 71416 KB Output is correct
52 Correct 83 ms 72440 KB Output is correct
53 Correct 85 ms 72568 KB Output is correct
54 Correct 85 ms 72440 KB Output is correct
55 Correct 87 ms 72440 KB Output is correct
56 Correct 85 ms 72412 KB Output is correct
57 Correct 87 ms 72184 KB Output is correct
58 Correct 89 ms 72156 KB Output is correct
59 Correct 89 ms 72184 KB Output is correct
60 Correct 89 ms 72312 KB Output is correct
61 Correct 84 ms 72440 KB Output is correct
62 Correct 84 ms 72412 KB Output is correct
63 Correct 97 ms 72540 KB Output is correct
64 Correct 85 ms 72184 KB Output is correct
65 Correct 87 ms 72156 KB Output is correct
66 Correct 102 ms 72156 KB Output is correct
67 Correct 88 ms 72208 KB Output is correct
68 Correct 76 ms 71388 KB Output is correct
69 Correct 84 ms 72440 KB Output is correct
70 Correct 83 ms 72312 KB Output is correct
71 Correct 85 ms 72308 KB Output is correct
72 Correct 88 ms 72132 KB Output is correct
73 Correct 90 ms 72056 KB Output is correct
74 Correct 85 ms 72440 KB Output is correct
75 Correct 84 ms 72312 KB Output is correct
76 Correct 108 ms 72312 KB Output is correct
77 Correct 103 ms 72252 KB Output is correct
78 Correct 84 ms 72312 KB Output is correct
79 Correct 103 ms 72184 KB Output is correct
80 Correct 85 ms 72276 KB Output is correct
81 Correct 86 ms 72312 KB Output is correct
82 Correct 84 ms 72212 KB Output is correct
83 Correct 86 ms 72184 KB Output is correct
84 Correct 85 ms 72312 KB Output is correct
85 Correct 102 ms 72184 KB Output is correct
86 Correct 85 ms 72184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 70776 KB Output is correct
2 Correct 72 ms 70776 KB Output is correct
3 Correct 78 ms 70728 KB Output is correct
4 Correct 80 ms 70904 KB Output is correct
5 Correct 80 ms 71032 KB Output is correct
6 Correct 78 ms 70876 KB Output is correct
7 Correct 79 ms 70904 KB Output is correct
8 Correct 72 ms 70820 KB Output is correct
9 Correct 73 ms 70776 KB Output is correct
10 Correct 73 ms 70908 KB Output is correct
11 Correct 80 ms 70956 KB Output is correct
12 Correct 78 ms 70904 KB Output is correct
13 Correct 78 ms 70944 KB Output is correct
14 Correct 78 ms 70948 KB Output is correct
15 Correct 78 ms 70896 KB Output is correct
16 Correct 79 ms 70904 KB Output is correct
17 Correct 84 ms 71000 KB Output is correct
18 Correct 79 ms 70904 KB Output is correct
19 Correct 78 ms 70904 KB Output is correct
20 Correct 79 ms 70984 KB Output is correct
21 Correct 80 ms 70904 KB Output is correct
22 Correct 81 ms 70904 KB Output is correct
23 Correct 79 ms 70904 KB Output is correct
24 Correct 79 ms 71032 KB Output is correct
25 Correct 81 ms 70904 KB Output is correct
26 Correct 75 ms 70904 KB Output is correct
27 Correct 79 ms 70904 KB Output is correct
28 Correct 79 ms 70864 KB Output is correct
29 Correct 109 ms 70892 KB Output is correct
30 Correct 80 ms 70904 KB Output is correct
31 Correct 96 ms 70904 KB Output is correct
32 Correct 81 ms 70928 KB Output is correct
33 Correct 79 ms 70904 KB Output is correct
34 Correct 80 ms 71032 KB Output is correct
35 Correct 98 ms 70904 KB Output is correct
36 Correct 81 ms 70904 KB Output is correct
37 Correct 79 ms 70904 KB Output is correct
38 Correct 80 ms 70904 KB Output is correct
39 Correct 79 ms 70968 KB Output is correct
40 Correct 78 ms 70908 KB Output is correct
41 Correct 79 ms 70904 KB Output is correct
42 Correct 82 ms 70904 KB Output is correct
43 Correct 79 ms 70904 KB Output is correct
44 Correct 80 ms 71032 KB Output is correct
45 Correct 81 ms 71104 KB Output is correct
46 Correct 89 ms 72312 KB Output is correct
47 Correct 88 ms 72184 KB Output is correct
48 Correct 87 ms 72184 KB Output is correct
49 Correct 77 ms 71416 KB Output is correct
50 Correct 77 ms 71608 KB Output is correct
51 Correct 77 ms 71416 KB Output is correct
52 Correct 83 ms 72440 KB Output is correct
53 Correct 85 ms 72568 KB Output is correct
54 Correct 85 ms 72440 KB Output is correct
55 Correct 87 ms 72440 KB Output is correct
56 Correct 85 ms 72412 KB Output is correct
57 Correct 87 ms 72184 KB Output is correct
58 Correct 89 ms 72156 KB Output is correct
59 Correct 89 ms 72184 KB Output is correct
60 Correct 89 ms 72312 KB Output is correct
61 Correct 84 ms 72440 KB Output is correct
62 Correct 84 ms 72412 KB Output is correct
63 Correct 97 ms 72540 KB Output is correct
64 Correct 85 ms 72184 KB Output is correct
65 Correct 87 ms 72156 KB Output is correct
66 Correct 102 ms 72156 KB Output is correct
67 Correct 88 ms 72208 KB Output is correct
68 Correct 76 ms 71388 KB Output is correct
69 Correct 84 ms 72440 KB Output is correct
70 Correct 83 ms 72312 KB Output is correct
71 Correct 85 ms 72308 KB Output is correct
72 Correct 88 ms 72132 KB Output is correct
73 Correct 90 ms 72056 KB Output is correct
74 Correct 85 ms 72440 KB Output is correct
75 Correct 84 ms 72312 KB Output is correct
76 Correct 108 ms 72312 KB Output is correct
77 Correct 103 ms 72252 KB Output is correct
78 Correct 84 ms 72312 KB Output is correct
79 Correct 103 ms 72184 KB Output is correct
80 Correct 85 ms 72276 KB Output is correct
81 Correct 86 ms 72312 KB Output is correct
82 Correct 84 ms 72212 KB Output is correct
83 Correct 86 ms 72184 KB Output is correct
84 Correct 85 ms 72312 KB Output is correct
85 Correct 102 ms 72184 KB Output is correct
86 Correct 85 ms 72184 KB Output is correct
87 Correct 101 ms 74460 KB Output is correct
88 Correct 159 ms 81656 KB Output is correct
89 Correct 465 ms 107448 KB Output is correct
90 Correct 415 ms 107300 KB Output is correct
91 Correct 431 ms 107324 KB Output is correct
92 Correct 187 ms 89080 KB Output is correct
93 Correct 188 ms 89080 KB Output is correct
94 Correct 181 ms 89208 KB Output is correct
95 Correct 249 ms 113104 KB Output is correct
96 Correct 271 ms 113520 KB Output is correct
97 Correct 283 ms 113464 KB Output is correct
98 Correct 259 ms 113520 KB Output is correct
99 Correct 258 ms 112024 KB Output is correct
100 Correct 460 ms 98544 KB Output is correct
101 Correct 538 ms 98676 KB Output is correct
102 Correct 521 ms 98788 KB Output is correct
103 Correct 542 ms 98884 KB Output is correct
104 Correct 270 ms 112080 KB Output is correct
105 Correct 262 ms 111920 KB Output is correct
106 Correct 269 ms 111992 KB Output is correct
107 Correct 370 ms 106488 KB Output is correct
108 Correct 409 ms 106860 KB Output is correct
109 Correct 417 ms 106872 KB Output is correct
110 Correct 181 ms 88568 KB Output is correct
111 Correct 252 ms 113236 KB Output is correct
112 Correct 247 ms 112496 KB Output is correct
113 Correct 257 ms 111352 KB Output is correct
114 Correct 460 ms 98336 KB Output is correct
115 Correct 513 ms 98192 KB Output is correct
116 Correct 279 ms 111352 KB Output is correct
117 Correct 252 ms 109280 KB Output is correct
118 Correct 286 ms 108636 KB Output is correct
119 Correct 274 ms 108024 KB Output is correct
120 Correct 270 ms 108892 KB Output is correct
121 Correct 286 ms 108196 KB Output is correct
122 Correct 283 ms 107768 KB Output is correct
123 Correct 276 ms 109468 KB Output is correct
124 Correct 277 ms 108664 KB Output is correct
125 Correct 274 ms 108244 KB Output is correct
126 Correct 281 ms 109176 KB Output is correct
127 Correct 279 ms 108280 KB Output is correct
128 Correct 304 ms 107976 KB Output is correct