Submission #131927

# Submission time Handle Problem Language Result Execution time Memory
131927 2019-07-18T04:34:14 Z imeimi2000 Construction of Highway (JOI18_construction) C++17
100 / 100
830 ms 19940 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#define fs first
#define se second

using namespace std;
typedef long long llong;
typedef pair<int, int> pii;

int n;
int C[100001];
int A[100001];
int B[100001];
vector<int> child[100001];
int sz[100001];

int dep[100001];
int par[100001];
void dfs1(int x) {
    sz[x] = 1;
    for (int i : child[x]) {
        dep[i] = dep[x] + 1;
        dfs1(i);
        sz[i] += sz[x];
    }
    sort(child[x].begin(), child[x].end(), [&](int a, int b) {
        return sz[a] > sz[b];
    });
}

int hidx[100001];
int st[100001];
int re[100001];
void dfs2(int x) {
    static int ord = 0;
    if (hidx[x] == 0) hidx[x] = x;
    re[st[x] = ++ord] = x;
    if (child[x].empty()) return;
    hidx[child[x][0]] = hidx[x];
    for (int i : child[x]) {
        dfs2(i);
    }
}

set<pii> sp;
vector<pii> gs;

void hld_up(int x, int c) {
    if (x == 0) return;
    int idx = hidx[x];
    auto it = sp.lower_bound(pii(st[x] + 1, 0));
    if (hidx[re[it->fs]] == idx) {
        gs.emplace_back(it->se, st[x] - max(st[idx] - 1, prev(it)->fs));
    }
    while (1) {
        it = --sp.lower_bound(pii(st[x] + 1, 0));
        if (it->fs < st[idx]) break;
        gs.emplace_back(it->se, it->fs - max(st[idx] - 1, prev(it)->fs));
        sp.erase(it);
    }
    sp.emplace(st[x], c);
    hld_up(par[idx], c);
}

vector<int> cp;
llong fen[100001];

void init(int i) {
    while (i <= 100000) {
        fen[i] = 0;
        i += i & -i;
    }
}

void init() {
    for (int i : cp) init(i);
    cp.clear();
}

void update(int i, int x) {
    cp.push_back(i);
    while (i <= 100000) {
        fen[i] += x;
        i += i & -i;
    }
}

llong query(int i) {
    llong ret = 0;
    while (i) {
        ret += fen[i];
        i -= i & -i;
    }
    return ret;
}

llong get_ans() {
    llong ret = 0;
    init();
    for (pii i : gs) {
        ret += query(i.fs - 1) * i.se;
        update(i.fs, i.se);
    }
    gs.clear();
    return ret;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> C[i];
        cp.push_back(C[i]);
    }
    sort(cp.begin(), cp.end());
    cp.erase(unique(cp.begin(), cp.end()), cp.end());
    for (int i = 1; i <= n; ++i) {
        C[i] = lower_bound(cp.begin(), cp.end(), C[i]) - cp.begin() + 1;
    }
    cp.clear();
    for (int i = 1; i < n; ++i) {
        cin >> A[i] >> B[i];
        par[B[i]] = A[i];
        child[A[i]].push_back(B[i]);
    }
    dfs1(1);
    dfs2(1);
    sp.emplace(0, 0);
    sp.emplace(n + 1, 0);
    sp.emplace(st[1], C[1]);
    for (int i = 1; i < n; ++i) {
        hld_up(B[i], C[B[i]]);
        printf("%lld\n", get_ans());
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 5 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 4 ms 2812 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 5 ms 2812 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
13 Correct 4 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 5 ms 2808 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 4 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 4 ms 2808 KB Output is correct
27 Correct 4 ms 2808 KB Output is correct
28 Correct 4 ms 2808 KB Output is correct
29 Correct 4 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2812 KB Output is correct
32 Correct 4 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 5 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2780 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 5 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 4 ms 2812 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 5 ms 2812 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
13 Correct 4 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 5 ms 2808 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 4 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 4 ms 2808 KB Output is correct
27 Correct 4 ms 2808 KB Output is correct
28 Correct 4 ms 2808 KB Output is correct
29 Correct 4 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2812 KB Output is correct
32 Correct 4 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 5 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2780 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 7 ms 2808 KB Output is correct
46 Correct 16 ms 3192 KB Output is correct
47 Correct 17 ms 3152 KB Output is correct
48 Correct 17 ms 3192 KB Output is correct
49 Correct 8 ms 3292 KB Output is correct
50 Correct 8 ms 3364 KB Output is correct
51 Correct 8 ms 3320 KB Output is correct
52 Correct 9 ms 3324 KB Output is correct
53 Correct 9 ms 3320 KB Output is correct
54 Correct 9 ms 3320 KB Output is correct
55 Correct 9 ms 3320 KB Output is correct
56 Correct 9 ms 3192 KB Output is correct
57 Correct 19 ms 3192 KB Output is correct
58 Correct 20 ms 3192 KB Output is correct
59 Correct 19 ms 3192 KB Output is correct
60 Correct 20 ms 3192 KB Output is correct
61 Correct 9 ms 3192 KB Output is correct
62 Correct 10 ms 3252 KB Output is correct
63 Correct 9 ms 3240 KB Output is correct
64 Correct 17 ms 3192 KB Output is correct
65 Correct 16 ms 3148 KB Output is correct
66 Correct 17 ms 3064 KB Output is correct
67 Correct 17 ms 3176 KB Output is correct
68 Correct 8 ms 3320 KB Output is correct
69 Correct 9 ms 3320 KB Output is correct
70 Correct 8 ms 3320 KB Output is correct
71 Correct 8 ms 3192 KB Output is correct
72 Correct 19 ms 3192 KB Output is correct
73 Correct 21 ms 3192 KB Output is correct
74 Correct 10 ms 3320 KB Output is correct
75 Correct 11 ms 3320 KB Output is correct
76 Correct 10 ms 3192 KB Output is correct
77 Correct 11 ms 3192 KB Output is correct
78 Correct 10 ms 3192 KB Output is correct
79 Correct 9 ms 3192 KB Output is correct
80 Correct 9 ms 3192 KB Output is correct
81 Correct 10 ms 3192 KB Output is correct
82 Correct 10 ms 3192 KB Output is correct
83 Correct 11 ms 3192 KB Output is correct
84 Correct 10 ms 3192 KB Output is correct
85 Correct 10 ms 3192 KB Output is correct
86 Correct 10 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 5 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 4 ms 2812 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 5 ms 2812 KB Output is correct
12 Correct 5 ms 2808 KB Output is correct
13 Correct 4 ms 2808 KB Output is correct
14 Correct 5 ms 2808 KB Output is correct
15 Correct 5 ms 2808 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 5 ms 2808 KB Output is correct
21 Correct 4 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 4 ms 2808 KB Output is correct
27 Correct 4 ms 2808 KB Output is correct
28 Correct 4 ms 2808 KB Output is correct
29 Correct 4 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2812 KB Output is correct
32 Correct 4 ms 2808 KB Output is correct
33 Correct 5 ms 2808 KB Output is correct
34 Correct 5 ms 2808 KB Output is correct
35 Correct 5 ms 2808 KB Output is correct
36 Correct 5 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 5 ms 2808 KB Output is correct
39 Correct 5 ms 2808 KB Output is correct
40 Correct 5 ms 2780 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 7 ms 2808 KB Output is correct
46 Correct 16 ms 3192 KB Output is correct
47 Correct 17 ms 3152 KB Output is correct
48 Correct 17 ms 3192 KB Output is correct
49 Correct 8 ms 3292 KB Output is correct
50 Correct 8 ms 3364 KB Output is correct
51 Correct 8 ms 3320 KB Output is correct
52 Correct 9 ms 3324 KB Output is correct
53 Correct 9 ms 3320 KB Output is correct
54 Correct 9 ms 3320 KB Output is correct
55 Correct 9 ms 3320 KB Output is correct
56 Correct 9 ms 3192 KB Output is correct
57 Correct 19 ms 3192 KB Output is correct
58 Correct 20 ms 3192 KB Output is correct
59 Correct 19 ms 3192 KB Output is correct
60 Correct 20 ms 3192 KB Output is correct
61 Correct 9 ms 3192 KB Output is correct
62 Correct 10 ms 3252 KB Output is correct
63 Correct 9 ms 3240 KB Output is correct
64 Correct 17 ms 3192 KB Output is correct
65 Correct 16 ms 3148 KB Output is correct
66 Correct 17 ms 3064 KB Output is correct
67 Correct 17 ms 3176 KB Output is correct
68 Correct 8 ms 3320 KB Output is correct
69 Correct 9 ms 3320 KB Output is correct
70 Correct 8 ms 3320 KB Output is correct
71 Correct 8 ms 3192 KB Output is correct
72 Correct 19 ms 3192 KB Output is correct
73 Correct 21 ms 3192 KB Output is correct
74 Correct 10 ms 3320 KB Output is correct
75 Correct 11 ms 3320 KB Output is correct
76 Correct 10 ms 3192 KB Output is correct
77 Correct 11 ms 3192 KB Output is correct
78 Correct 10 ms 3192 KB Output is correct
79 Correct 9 ms 3192 KB Output is correct
80 Correct 9 ms 3192 KB Output is correct
81 Correct 10 ms 3192 KB Output is correct
82 Correct 10 ms 3192 KB Output is correct
83 Correct 11 ms 3192 KB Output is correct
84 Correct 10 ms 3192 KB Output is correct
85 Correct 10 ms 3192 KB Output is correct
86 Correct 10 ms 3064 KB Output is correct
87 Correct 44 ms 3704 KB Output is correct
88 Correct 145 ms 6348 KB Output is correct
89 Correct 659 ms 14700 KB Output is correct
90 Correct 661 ms 14708 KB Output is correct
91 Correct 616 ms 14620 KB Output is correct
92 Correct 138 ms 19160 KB Output is correct
93 Correct 136 ms 19084 KB Output is correct
94 Correct 146 ms 19060 KB Output is correct
95 Correct 184 ms 19556 KB Output is correct
96 Correct 192 ms 19920 KB Output is correct
97 Correct 194 ms 19940 KB Output is correct
98 Correct 193 ms 19928 KB Output is correct
99 Correct 192 ms 16984 KB Output is correct
100 Correct 817 ms 14636 KB Output is correct
101 Correct 763 ms 15052 KB Output is correct
102 Correct 790 ms 14984 KB Output is correct
103 Correct 830 ms 15148 KB Output is correct
104 Correct 202 ms 16884 KB Output is correct
105 Correct 192 ms 16884 KB Output is correct
106 Correct 197 ms 16880 KB Output is correct
107 Correct 597 ms 13172 KB Output is correct
108 Correct 616 ms 13164 KB Output is correct
109 Correct 661 ms 13740 KB Output is correct
110 Correct 118 ms 17780 KB Output is correct
111 Correct 187 ms 19440 KB Output is correct
112 Correct 176 ms 18588 KB Output is correct
113 Correct 167 ms 15520 KB Output is correct
114 Correct 723 ms 14828 KB Output is correct
115 Correct 721 ms 13812 KB Output is correct
116 Correct 184 ms 15604 KB Output is correct
117 Correct 203 ms 14916 KB Output is correct
118 Correct 201 ms 14444 KB Output is correct
119 Correct 199 ms 14324 KB Output is correct
120 Correct 189 ms 13840 KB Output is correct
121 Correct 188 ms 13684 KB Output is correct
122 Correct 183 ms 13220 KB Output is correct
123 Correct 217 ms 14984 KB Output is correct
124 Correct 223 ms 14552 KB Output is correct
125 Correct 222 ms 14192 KB Output is correct
126 Correct 193 ms 13940 KB Output is correct
127 Correct 211 ms 13428 KB Output is correct
128 Correct 215 ms 13172 KB Output is correct