Submission #478323

# Submission time Handle Problem Language Result Execution time Memory
478323 2021-10-07T02:54:59 Z Mike4235 Construction of Highway (JOI18_construction) C++17
100 / 100
481 ms 93464 KB
/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")*/
// only when really needed

/* GNU G++17 7.3.0: No long long for faster code
   GNU G++17 9.2.0 (64 bit, msys 2): Long long only for faster code */
 
#include <bits/stdc++.h>
  
#define for1(i,a,b) for (int i = a; i <= b; i++)
#define for2(i,a,b) for (int i = a; i >= b; i--)
// #define int long long

#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pb push_back
 
/*
__builtin_popcountll(x) : Number of 1-bit
__builtin_ctzll(x) : Number of trailing 0
*/
 
#define PI 3.1415926535897932384626433832795
#define INF 1000000000000000000
#define MOD 1000000007
#define MOD2 1000000009
#define EPS 1e-6

using namespace std;

int n, now;
int a[100005], b[100005], c[100005], fentree[100005];
int sz[100005], par[100005], nxt[100005], tin[100005];
deque<pii> ele[100005];
vector<int> g[100005];
map<int, int> mp;

void dfs_sz(int u) {
    sz[u] = 1;
    for (auto &v : g[u]) {
        par[v] = u;
        dfs_sz(v);
        if (sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
        sz[u] += sz[v];
    }
}

void dfs_hld(int u) {
    tin[u] = ++now;
    for (auto v : g[u]) {
        nxt[v] = (v == g[u][0] ? nxt[u] : v);
        if (sz(ele[nxt[v]]) && ele[nxt[v]].back().first == c[v]) ele[nxt[v]].back().second++;
        else ele[nxt[v]].push_back({c[v], 1});
        dfs_hld(v);
    }
}

void update(int i, int val) {
    for (; i <= n; i += i & -i) fentree[i] += val;
}

int get(int i) {
    int res = 0;
    for (; i; i -= i & -i) res += fentree[i];
    return res;
}

int solve(int u) {
    vector<pii> q;

    while (u != 0) {
        vector<pii> tmp;
        int cur = 0, sz = tin[u] - tin[nxt[u]] + 1;
        for (auto p : ele[nxt[u]]) {
            if (cur + p.second <= sz) {
                tmp.push_back(p);
                cur += p.second;
            }
            else {
                if (cur < sz) tmp.push_back({p.first, sz - cur});
                break;
            }
        }

        reverse(tmp.begin(), tmp.end());
        for (auto p : tmp) q.push_back(p);

        u = par[nxt[u]];
    }

    int res = 0;
    for (auto p : q) {
        res += p.second * get(p.first - 1);
        update(p.first, p.second);
    }
    for (auto p : q) update(p.first, -p.second);

    return res;
}

void assign(int u, int val) {
    while (u != 0) {
        int cur = 0, sz = tin[u] - tin[nxt[u]] + 1;
        while (cur < sz) {
            auto p = ele[nxt[u]].front();
            if (cur + p.second <= sz) {
                cur += p.second;
                ele[nxt[u]].pop_front();
            }
            else {
                ele[nxt[u]].front().second -= sz - cur;
                break;
            }
        }
        if (sz(ele[nxt[u]]) && ele[nxt[u]].front().first == val) ele[nxt[u]].front().second += sz; 
        else ele[nxt[u]].push_front({val, sz});
        u = par[nxt[u]];
    }
}

signed main() {
    
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    // freopen("cf.inp", "r", stdin);
    // freopen("cf.out", "w", stdout);

    cin >> n;
    for1(i,1,n) {
        cin >> c[i];
        mp[c[i]] = 0;
    }

    int cur = 0;
    for (auto &p : mp) p.second = ++cur;
    for1(i,1,n) c[i] = mp[c[i]];

    for1(i,1,n - 1) {
        cin >> a[i] >> b[i];
        g[a[i]].push_back(b[i]);
    }

    nxt[1] = 1;
    ele[1].push_back({c[1], 1});
    dfs_sz(1);
    dfs_hld(1);

    for1(i,1,n - 1) {
        cout << solve(a[i]) << "\n";
        assign(a[i], c[b[i]]);
    }
    
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 69956 KB Output is correct
2 Correct 47 ms 69996 KB Output is correct
3 Correct 50 ms 69904 KB Output is correct
4 Correct 47 ms 69956 KB Output is correct
5 Correct 47 ms 69964 KB Output is correct
6 Correct 61 ms 70088 KB Output is correct
7 Correct 47 ms 70016 KB Output is correct
8 Correct 55 ms 70076 KB Output is correct
9 Correct 48 ms 70112 KB Output is correct
10 Correct 51 ms 70024 KB Output is correct
11 Correct 54 ms 70084 KB Output is correct
12 Correct 47 ms 70020 KB Output is correct
13 Correct 47 ms 70092 KB Output is correct
14 Correct 48 ms 70080 KB Output is correct
15 Correct 51 ms 70048 KB Output is correct
16 Correct 52 ms 70024 KB Output is correct
17 Correct 47 ms 69972 KB Output is correct
18 Correct 48 ms 69964 KB Output is correct
19 Correct 48 ms 70060 KB Output is correct
20 Correct 47 ms 70080 KB Output is correct
21 Correct 45 ms 70044 KB Output is correct
22 Correct 52 ms 69964 KB Output is correct
23 Correct 55 ms 70004 KB Output is correct
24 Correct 49 ms 69972 KB Output is correct
25 Correct 52 ms 69948 KB Output is correct
26 Correct 45 ms 70020 KB Output is correct
27 Correct 50 ms 70124 KB Output is correct
28 Correct 46 ms 70080 KB Output is correct
29 Correct 48 ms 70016 KB Output is correct
30 Correct 47 ms 69964 KB Output is correct
31 Correct 45 ms 69932 KB Output is correct
32 Correct 47 ms 70060 KB Output is correct
33 Correct 47 ms 70012 KB Output is correct
34 Correct 47 ms 69992 KB Output is correct
35 Correct 49 ms 70092 KB Output is correct
36 Correct 48 ms 69940 KB Output is correct
37 Correct 47 ms 70028 KB Output is correct
38 Correct 48 ms 70048 KB Output is correct
39 Correct 49 ms 70072 KB Output is correct
40 Correct 48 ms 69992 KB Output is correct
41 Correct 47 ms 70088 KB Output is correct
42 Correct 46 ms 70040 KB Output is correct
43 Correct 45 ms 70016 KB Output is correct
44 Correct 47 ms 69956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 69956 KB Output is correct
2 Correct 47 ms 69996 KB Output is correct
3 Correct 50 ms 69904 KB Output is correct
4 Correct 47 ms 69956 KB Output is correct
5 Correct 47 ms 69964 KB Output is correct
6 Correct 61 ms 70088 KB Output is correct
7 Correct 47 ms 70016 KB Output is correct
8 Correct 55 ms 70076 KB Output is correct
9 Correct 48 ms 70112 KB Output is correct
10 Correct 51 ms 70024 KB Output is correct
11 Correct 54 ms 70084 KB Output is correct
12 Correct 47 ms 70020 KB Output is correct
13 Correct 47 ms 70092 KB Output is correct
14 Correct 48 ms 70080 KB Output is correct
15 Correct 51 ms 70048 KB Output is correct
16 Correct 52 ms 70024 KB Output is correct
17 Correct 47 ms 69972 KB Output is correct
18 Correct 48 ms 69964 KB Output is correct
19 Correct 48 ms 70060 KB Output is correct
20 Correct 47 ms 70080 KB Output is correct
21 Correct 45 ms 70044 KB Output is correct
22 Correct 52 ms 69964 KB Output is correct
23 Correct 55 ms 70004 KB Output is correct
24 Correct 49 ms 69972 KB Output is correct
25 Correct 52 ms 69948 KB Output is correct
26 Correct 45 ms 70020 KB Output is correct
27 Correct 50 ms 70124 KB Output is correct
28 Correct 46 ms 70080 KB Output is correct
29 Correct 48 ms 70016 KB Output is correct
30 Correct 47 ms 69964 KB Output is correct
31 Correct 45 ms 69932 KB Output is correct
32 Correct 47 ms 70060 KB Output is correct
33 Correct 47 ms 70012 KB Output is correct
34 Correct 47 ms 69992 KB Output is correct
35 Correct 49 ms 70092 KB Output is correct
36 Correct 48 ms 69940 KB Output is correct
37 Correct 47 ms 70028 KB Output is correct
38 Correct 48 ms 70048 KB Output is correct
39 Correct 49 ms 70072 KB Output is correct
40 Correct 48 ms 69992 KB Output is correct
41 Correct 47 ms 70088 KB Output is correct
42 Correct 46 ms 70040 KB Output is correct
43 Correct 45 ms 70016 KB Output is correct
44 Correct 47 ms 69956 KB Output is correct
45 Correct 47 ms 70104 KB Output is correct
46 Correct 54 ms 70332 KB Output is correct
47 Correct 51 ms 70420 KB Output is correct
48 Correct 52 ms 70376 KB Output is correct
49 Correct 53 ms 70820 KB Output is correct
50 Correct 50 ms 70852 KB Output is correct
51 Correct 51 ms 70856 KB Output is correct
52 Correct 52 ms 70596 KB Output is correct
53 Correct 53 ms 70596 KB Output is correct
54 Correct 49 ms 70596 KB Output is correct
55 Correct 50 ms 70584 KB Output is correct
56 Correct 51 ms 70576 KB Output is correct
57 Correct 53 ms 70420 KB Output is correct
58 Correct 55 ms 70332 KB Output is correct
59 Correct 54 ms 70404 KB Output is correct
60 Correct 53 ms 70380 KB Output is correct
61 Correct 52 ms 70468 KB Output is correct
62 Correct 50 ms 70604 KB Output is correct
63 Correct 49 ms 70600 KB Output is correct
64 Correct 50 ms 70208 KB Output is correct
65 Correct 59 ms 70256 KB Output is correct
66 Correct 55 ms 70212 KB Output is correct
67 Correct 63 ms 70252 KB Output is correct
68 Correct 48 ms 70584 KB Output is correct
69 Correct 52 ms 70620 KB Output is correct
70 Correct 50 ms 70348 KB Output is correct
71 Correct 52 ms 70360 KB Output is correct
72 Correct 61 ms 70360 KB Output is correct
73 Correct 63 ms 70212 KB Output is correct
74 Correct 51 ms 70312 KB Output is correct
75 Correct 53 ms 70496 KB Output is correct
76 Correct 52 ms 70372 KB Output is correct
77 Correct 52 ms 70456 KB Output is correct
78 Correct 51 ms 70196 KB Output is correct
79 Correct 50 ms 70212 KB Output is correct
80 Correct 58 ms 70268 KB Output is correct
81 Correct 52 ms 70620 KB Output is correct
82 Correct 52 ms 70364 KB Output is correct
83 Correct 54 ms 70392 KB Output is correct
84 Correct 53 ms 70212 KB Output is correct
85 Correct 51 ms 70244 KB Output is correct
86 Correct 50 ms 70152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 69956 KB Output is correct
2 Correct 47 ms 69996 KB Output is correct
3 Correct 50 ms 69904 KB Output is correct
4 Correct 47 ms 69956 KB Output is correct
5 Correct 47 ms 69964 KB Output is correct
6 Correct 61 ms 70088 KB Output is correct
7 Correct 47 ms 70016 KB Output is correct
8 Correct 55 ms 70076 KB Output is correct
9 Correct 48 ms 70112 KB Output is correct
10 Correct 51 ms 70024 KB Output is correct
11 Correct 54 ms 70084 KB Output is correct
12 Correct 47 ms 70020 KB Output is correct
13 Correct 47 ms 70092 KB Output is correct
14 Correct 48 ms 70080 KB Output is correct
15 Correct 51 ms 70048 KB Output is correct
16 Correct 52 ms 70024 KB Output is correct
17 Correct 47 ms 69972 KB Output is correct
18 Correct 48 ms 69964 KB Output is correct
19 Correct 48 ms 70060 KB Output is correct
20 Correct 47 ms 70080 KB Output is correct
21 Correct 45 ms 70044 KB Output is correct
22 Correct 52 ms 69964 KB Output is correct
23 Correct 55 ms 70004 KB Output is correct
24 Correct 49 ms 69972 KB Output is correct
25 Correct 52 ms 69948 KB Output is correct
26 Correct 45 ms 70020 KB Output is correct
27 Correct 50 ms 70124 KB Output is correct
28 Correct 46 ms 70080 KB Output is correct
29 Correct 48 ms 70016 KB Output is correct
30 Correct 47 ms 69964 KB Output is correct
31 Correct 45 ms 69932 KB Output is correct
32 Correct 47 ms 70060 KB Output is correct
33 Correct 47 ms 70012 KB Output is correct
34 Correct 47 ms 69992 KB Output is correct
35 Correct 49 ms 70092 KB Output is correct
36 Correct 48 ms 69940 KB Output is correct
37 Correct 47 ms 70028 KB Output is correct
38 Correct 48 ms 70048 KB Output is correct
39 Correct 49 ms 70072 KB Output is correct
40 Correct 48 ms 69992 KB Output is correct
41 Correct 47 ms 70088 KB Output is correct
42 Correct 46 ms 70040 KB Output is correct
43 Correct 45 ms 70016 KB Output is correct
44 Correct 47 ms 69956 KB Output is correct
45 Correct 47 ms 70104 KB Output is correct
46 Correct 54 ms 70332 KB Output is correct
47 Correct 51 ms 70420 KB Output is correct
48 Correct 52 ms 70376 KB Output is correct
49 Correct 53 ms 70820 KB Output is correct
50 Correct 50 ms 70852 KB Output is correct
51 Correct 51 ms 70856 KB Output is correct
52 Correct 52 ms 70596 KB Output is correct
53 Correct 53 ms 70596 KB Output is correct
54 Correct 49 ms 70596 KB Output is correct
55 Correct 50 ms 70584 KB Output is correct
56 Correct 51 ms 70576 KB Output is correct
57 Correct 53 ms 70420 KB Output is correct
58 Correct 55 ms 70332 KB Output is correct
59 Correct 54 ms 70404 KB Output is correct
60 Correct 53 ms 70380 KB Output is correct
61 Correct 52 ms 70468 KB Output is correct
62 Correct 50 ms 70604 KB Output is correct
63 Correct 49 ms 70600 KB Output is correct
64 Correct 50 ms 70208 KB Output is correct
65 Correct 59 ms 70256 KB Output is correct
66 Correct 55 ms 70212 KB Output is correct
67 Correct 63 ms 70252 KB Output is correct
68 Correct 48 ms 70584 KB Output is correct
69 Correct 52 ms 70620 KB Output is correct
70 Correct 50 ms 70348 KB Output is correct
71 Correct 52 ms 70360 KB Output is correct
72 Correct 61 ms 70360 KB Output is correct
73 Correct 63 ms 70212 KB Output is correct
74 Correct 51 ms 70312 KB Output is correct
75 Correct 53 ms 70496 KB Output is correct
76 Correct 52 ms 70372 KB Output is correct
77 Correct 52 ms 70456 KB Output is correct
78 Correct 51 ms 70196 KB Output is correct
79 Correct 50 ms 70212 KB Output is correct
80 Correct 58 ms 70268 KB Output is correct
81 Correct 52 ms 70620 KB Output is correct
82 Correct 52 ms 70364 KB Output is correct
83 Correct 54 ms 70392 KB Output is correct
84 Correct 53 ms 70212 KB Output is correct
85 Correct 51 ms 70244 KB Output is correct
86 Correct 50 ms 70152 KB Output is correct
87 Correct 74 ms 70936 KB Output is correct
88 Correct 129 ms 72916 KB Output is correct
89 Correct 337 ms 79704 KB Output is correct
90 Correct 326 ms 79808 KB Output is correct
91 Correct 328 ms 79752 KB Output is correct
92 Correct 210 ms 91316 KB Output is correct
93 Correct 234 ms 93464 KB Output is correct
94 Correct 203 ms 93384 KB Output is correct
95 Correct 208 ms 87496 KB Output is correct
96 Correct 205 ms 87900 KB Output is correct
97 Correct 203 ms 87956 KB Output is correct
98 Correct 202 ms 87880 KB Output is correct
99 Correct 207 ms 86976 KB Output is correct
100 Correct 456 ms 81916 KB Output is correct
101 Correct 481 ms 82356 KB Output is correct
102 Correct 472 ms 82364 KB Output is correct
103 Correct 450 ms 82240 KB Output is correct
104 Correct 232 ms 86852 KB Output is correct
105 Correct 227 ms 86924 KB Output is correct
106 Correct 229 ms 86928 KB Output is correct
107 Correct 259 ms 76584 KB Output is correct
108 Correct 261 ms 76008 KB Output is correct
109 Correct 287 ms 77744 KB Output is correct
110 Correct 139 ms 87752 KB Output is correct
111 Correct 215 ms 87496 KB Output is correct
112 Correct 139 ms 82248 KB Output is correct
113 Correct 150 ms 81288 KB Output is correct
114 Correct 450 ms 81884 KB Output is correct
115 Correct 384 ms 76580 KB Output is correct
116 Correct 193 ms 81212 KB Output is correct
117 Correct 210 ms 83916 KB Output is correct
118 Correct 212 ms 83268 KB Output is correct
119 Correct 212 ms 82760 KB Output is correct
120 Correct 144 ms 78804 KB Output is correct
121 Correct 161 ms 78120 KB Output is correct
122 Correct 158 ms 77624 KB Output is correct
123 Correct 248 ms 84028 KB Output is correct
124 Correct 289 ms 83276 KB Output is correct
125 Correct 235 ms 82840 KB Output is correct
126 Correct 170 ms 78880 KB Output is correct
127 Correct 168 ms 78168 KB Output is correct
128 Correct 177 ms 77748 KB Output is correct