Submission #799962

# Submission time Handle Problem Language Result Execution time Memory
799962 2023-08-01T08:52:01 Z phoenix Construction of Highway (JOI18_construction) C++17
100 / 100
185 ms 20656 KB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 10;

struct object {
    int color, len;
};
ll numberOfInverse(object *a, int n) {
    static object b[N];

    if(n == 1) return 0;
    ll ans = numberOfInverse(a, n / 2) + numberOfInverse(a + n / 2, (n + 1) / 2);
    int lb = 0, rb = 0, sum = 0;
    for(int i = n / 2; i < n; i++) {
        while(lb < n / 2 && a[lb].color < a[i].color) sum += a[lb++].len;
        ans += 1ll * a[i].len * sum;
    }    
    lb = 0, rb = n / 2;
    for(int i = 0; i < n; i++) {
        if(lb == n / 2) {
            b[i] = a[rb++];
            continue;
        }
        if(rb == n) {
            b[i] = a[lb++];
            continue;
        }
        b[i] = (a[lb].color <= a[rb].color ? a[lb++] : a[rb++]);
    }
    for(int i = 0; i < n; i++) 
        a[i] = b[i];
    return ans;
}

int n;
int C[N];
int sz[N];
int head[N];
int depth[N];
int heavy[N];
int parent[N];

vector<object> v[N];
vector<pair<int, int>> edges;
vector<int> g[N];

void precalc(int s, int p) {
    parent[s] = p; sz[s] = 1;
    for(int to : g[s]) {
        depth[to] = depth[s] + 1;
        precalc(to, s);
        sz[s] += sz[to];
        if(sz[heavy[s]] < sz[to]) heavy[s] = to;
    }
}
void decompose(int s, int h) {
    head[s] = h;
    if(heavy[s]) decompose(heavy[s], h);
    for(int to : g[s]) {
        if(to == heavy[s]) continue;
        decompose(to, to);
    }
    v[h].push_back({C[s], 1});
}

int len = 0;
object t[N]; 

ll query(int a, int x) {
    for(int ver = a; ver; ver = parent[head[ver]]) {
        int u = head[ver];
        int k = depth[ver] - depth[u] + 1;
        vector<object> t1;
        while(v[u].back().len <= k) {
            k -= v[u].back().len;
            t1.push_back(v[u].back());
            v[u].pop_back();
        }
        if(k) {
            object z = {v[u].back().color, k};
            v[u].back().len -= k;
            t1.push_back(z);
            k = 0;
        }
        v[u].push_back({x, depth[ver] - depth[u] + 1});
        while(!t1.empty()) {
            t[len++] = t1.back();
            t1.pop_back();
        }
    }
    // for(int i = 0; i < len; i++) 
    //     cerr << '(' << t[i].color << ' ' << t[i].len << ')' << ' ';
    // cout << '\n';
    ll res = numberOfInverse(t, len);
    len = 0;
    return res;
} 

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> C[i];
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        edges.push_back({a, b});
        g[a].push_back(b);
    }
    precalc(1, 0);
    decompose(1, 1);
    for(auto [a, b] : edges) {
        cout << query(a, C[b]) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 2 ms 5076 KB Output is correct
27 Correct 2 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 2 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 2 ms 5076 KB Output is correct
33 Correct 3 ms 5120 KB Output is correct
34 Correct 2 ms 5076 KB Output is correct
35 Correct 2 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 2 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 3 ms 5000 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 2 ms 5076 KB Output is correct
27 Correct 2 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 2 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 2 ms 5076 KB Output is correct
33 Correct 3 ms 5120 KB Output is correct
34 Correct 2 ms 5076 KB Output is correct
35 Correct 2 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 2 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 3 ms 5000 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 5 ms 5336 KB Output is correct
47 Correct 5 ms 5332 KB Output is correct
48 Correct 6 ms 5332 KB Output is correct
49 Correct 4 ms 5588 KB Output is correct
50 Correct 4 ms 5676 KB Output is correct
51 Correct 5 ms 5588 KB Output is correct
52 Correct 4 ms 5460 KB Output is correct
53 Correct 4 ms 5512 KB Output is correct
54 Correct 4 ms 5460 KB Output is correct
55 Correct 5 ms 5460 KB Output is correct
56 Correct 4 ms 5468 KB Output is correct
57 Correct 7 ms 5240 KB Output is correct
58 Correct 6 ms 5280 KB Output is correct
59 Correct 6 ms 5332 KB Output is correct
60 Correct 6 ms 5324 KB Output is correct
61 Correct 4 ms 5460 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 4 ms 5460 KB Output is correct
64 Correct 5 ms 5332 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 5 ms 5332 KB Output is correct
67 Correct 6 ms 5332 KB Output is correct
68 Correct 4 ms 5680 KB Output is correct
69 Correct 4 ms 5460 KB Output is correct
70 Correct 4 ms 5460 KB Output is correct
71 Correct 4 ms 5460 KB Output is correct
72 Correct 5 ms 5332 KB Output is correct
73 Correct 6 ms 5204 KB Output is correct
74 Correct 4 ms 5420 KB Output is correct
75 Correct 4 ms 5332 KB Output is correct
76 Correct 4 ms 5332 KB Output is correct
77 Correct 4 ms 5332 KB Output is correct
78 Correct 4 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 4 ms 5332 KB Output is correct
81 Correct 4 ms 5332 KB Output is correct
82 Correct 4 ms 5376 KB Output is correct
83 Correct 5 ms 5332 KB Output is correct
84 Correct 4 ms 5332 KB Output is correct
85 Correct 4 ms 5332 KB Output is correct
86 Correct 5 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 2 ms 5076 KB Output is correct
27 Correct 2 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 2 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 2 ms 5076 KB Output is correct
33 Correct 3 ms 5120 KB Output is correct
34 Correct 2 ms 5076 KB Output is correct
35 Correct 2 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 2 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 3 ms 5000 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 5 ms 5336 KB Output is correct
47 Correct 5 ms 5332 KB Output is correct
48 Correct 6 ms 5332 KB Output is correct
49 Correct 4 ms 5588 KB Output is correct
50 Correct 4 ms 5676 KB Output is correct
51 Correct 5 ms 5588 KB Output is correct
52 Correct 4 ms 5460 KB Output is correct
53 Correct 4 ms 5512 KB Output is correct
54 Correct 4 ms 5460 KB Output is correct
55 Correct 5 ms 5460 KB Output is correct
56 Correct 4 ms 5468 KB Output is correct
57 Correct 7 ms 5240 KB Output is correct
58 Correct 6 ms 5280 KB Output is correct
59 Correct 6 ms 5332 KB Output is correct
60 Correct 6 ms 5324 KB Output is correct
61 Correct 4 ms 5460 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 4 ms 5460 KB Output is correct
64 Correct 5 ms 5332 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 5 ms 5332 KB Output is correct
67 Correct 6 ms 5332 KB Output is correct
68 Correct 4 ms 5680 KB Output is correct
69 Correct 4 ms 5460 KB Output is correct
70 Correct 4 ms 5460 KB Output is correct
71 Correct 4 ms 5460 KB Output is correct
72 Correct 5 ms 5332 KB Output is correct
73 Correct 6 ms 5204 KB Output is correct
74 Correct 4 ms 5420 KB Output is correct
75 Correct 4 ms 5332 KB Output is correct
76 Correct 4 ms 5332 KB Output is correct
77 Correct 4 ms 5332 KB Output is correct
78 Correct 4 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 4 ms 5332 KB Output is correct
81 Correct 4 ms 5332 KB Output is correct
82 Correct 4 ms 5376 KB Output is correct
83 Correct 5 ms 5332 KB Output is correct
84 Correct 4 ms 5332 KB Output is correct
85 Correct 4 ms 5332 KB Output is correct
86 Correct 5 ms 5332 KB Output is correct
87 Correct 11 ms 5700 KB Output is correct
88 Correct 27 ms 7120 KB Output is correct
89 Correct 98 ms 11960 KB Output is correct
90 Correct 97 ms 11960 KB Output is correct
91 Correct 93 ms 11932 KB Output is correct
92 Correct 60 ms 20616 KB Output is correct
93 Correct 51 ms 20600 KB Output is correct
94 Correct 55 ms 20636 KB Output is correct
95 Correct 52 ms 17080 KB Output is correct
96 Correct 64 ms 17024 KB Output is correct
97 Correct 66 ms 16964 KB Output is correct
98 Correct 66 ms 16992 KB Output is correct
99 Correct 53 ms 15888 KB Output is correct
100 Correct 113 ms 11984 KB Output is correct
101 Correct 154 ms 11996 KB Output is correct
102 Correct 171 ms 11872 KB Output is correct
103 Correct 148 ms 11884 KB Output is correct
104 Correct 57 ms 15816 KB Output is correct
105 Correct 56 ms 15820 KB Output is correct
106 Correct 55 ms 15844 KB Output is correct
107 Correct 105 ms 11816 KB Output is correct
108 Correct 90 ms 11848 KB Output is correct
109 Correct 93 ms 11952 KB Output is correct
110 Correct 67 ms 20656 KB Output is correct
111 Correct 52 ms 17084 KB Output is correct
112 Correct 56 ms 16992 KB Output is correct
113 Correct 51 ms 15900 KB Output is correct
114 Correct 117 ms 11956 KB Output is correct
115 Correct 185 ms 11964 KB Output is correct
116 Correct 67 ms 15844 KB Output is correct
117 Correct 61 ms 13888 KB Output is correct
118 Correct 51 ms 13332 KB Output is correct
119 Correct 51 ms 12928 KB Output is correct
120 Correct 49 ms 13776 KB Output is correct
121 Correct 51 ms 13320 KB Output is correct
122 Correct 50 ms 12864 KB Output is correct
123 Correct 58 ms 13916 KB Output is correct
124 Correct 58 ms 13380 KB Output is correct
125 Correct 65 ms 12972 KB Output is correct
126 Correct 60 ms 13936 KB Output is correct
127 Correct 64 ms 13448 KB Output is correct
128 Correct 57 ms 12988 KB Output is correct