답안 #799957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799957 2023-08-01T08:51:03 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
158 ms 22756 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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5056 KB Output is correct
5 Correct 4 ms 5040 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5120 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 5040 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5032 KB Output is correct
16 Correct 4 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 4 ms 5032 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5032 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5036 KB Output is correct
30 Correct 4 ms 5036 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5036 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5036 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5044 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5056 KB Output is correct
5 Correct 4 ms 5040 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5120 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 5040 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5032 KB Output is correct
16 Correct 4 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 4 ms 5032 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5032 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5036 KB Output is correct
30 Correct 4 ms 5036 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5036 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5036 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5044 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5064 KB Output is correct
46 Correct 7 ms 5380 KB Output is correct
47 Correct 5 ms 5332 KB Output is correct
48 Correct 5 ms 5332 KB Output is correct
49 Correct 4 ms 5684 KB Output is correct
50 Correct 4 ms 5680 KB Output is correct
51 Correct 5 ms 5716 KB Output is correct
52 Correct 6 ms 5580 KB Output is correct
53 Correct 5 ms 5576 KB Output is correct
54 Correct 5 ms 5540 KB Output is correct
55 Correct 4 ms 5556 KB Output is correct
56 Correct 5 ms 5460 KB Output is correct
57 Correct 7 ms 5300 KB Output is correct
58 Correct 6 ms 5332 KB Output is correct
59 Correct 6 ms 5296 KB Output is correct
60 Correct 10 ms 5404 KB Output is correct
61 Correct 6 ms 5552 KB Output is correct
62 Correct 6 ms 5508 KB Output is correct
63 Correct 4 ms 5556 KB Output is correct
64 Correct 5 ms 5296 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 5 ms 5296 KB Output is correct
67 Correct 6 ms 5332 KB Output is correct
68 Correct 5 ms 5684 KB Output is correct
69 Correct 5 ms 5588 KB Output is correct
70 Correct 4 ms 5588 KB Output is correct
71 Correct 4 ms 5460 KB Output is correct
72 Correct 6 ms 5356 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 4 ms 5460 KB Output is correct
75 Correct 5 ms 5460 KB Output is correct
76 Correct 4 ms 5424 KB Output is correct
77 Correct 4 ms 5332 KB Output is correct
78 Correct 4 ms 5332 KB Output is correct
79 Correct 6 ms 5332 KB Output is correct
80 Correct 5 ms 5332 KB Output is correct
81 Correct 4 ms 5428 KB Output is correct
82 Correct 4 ms 5332 KB Output is correct
83 Correct 4 ms 5332 KB Output is correct
84 Correct 4 ms 5460 KB Output is correct
85 Correct 4 ms 5432 KB Output is correct
86 Correct 5 ms 5332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5056 KB Output is correct
5 Correct 4 ms 5040 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5036 KB Output is correct
10 Correct 3 ms 5120 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 5040 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5032 KB Output is correct
16 Correct 4 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 4 ms 5032 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5032 KB Output is correct
25 Correct 4 ms 5076 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 3 ms 5036 KB Output is correct
30 Correct 4 ms 5036 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5036 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5036 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 4 ms 5044 KB Output is correct
42 Correct 3 ms 5032 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5064 KB Output is correct
46 Correct 7 ms 5380 KB Output is correct
47 Correct 5 ms 5332 KB Output is correct
48 Correct 5 ms 5332 KB Output is correct
49 Correct 4 ms 5684 KB Output is correct
50 Correct 4 ms 5680 KB Output is correct
51 Correct 5 ms 5716 KB Output is correct
52 Correct 6 ms 5580 KB Output is correct
53 Correct 5 ms 5576 KB Output is correct
54 Correct 5 ms 5540 KB Output is correct
55 Correct 4 ms 5556 KB Output is correct
56 Correct 5 ms 5460 KB Output is correct
57 Correct 7 ms 5300 KB Output is correct
58 Correct 6 ms 5332 KB Output is correct
59 Correct 6 ms 5296 KB Output is correct
60 Correct 10 ms 5404 KB Output is correct
61 Correct 6 ms 5552 KB Output is correct
62 Correct 6 ms 5508 KB Output is correct
63 Correct 4 ms 5556 KB Output is correct
64 Correct 5 ms 5296 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 5 ms 5296 KB Output is correct
67 Correct 6 ms 5332 KB Output is correct
68 Correct 5 ms 5684 KB Output is correct
69 Correct 5 ms 5588 KB Output is correct
70 Correct 4 ms 5588 KB Output is correct
71 Correct 4 ms 5460 KB Output is correct
72 Correct 6 ms 5356 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 4 ms 5460 KB Output is correct
75 Correct 5 ms 5460 KB Output is correct
76 Correct 4 ms 5424 KB Output is correct
77 Correct 4 ms 5332 KB Output is correct
78 Correct 4 ms 5332 KB Output is correct
79 Correct 6 ms 5332 KB Output is correct
80 Correct 5 ms 5332 KB Output is correct
81 Correct 4 ms 5428 KB Output is correct
82 Correct 4 ms 5332 KB Output is correct
83 Correct 4 ms 5332 KB Output is correct
84 Correct 4 ms 5460 KB Output is correct
85 Correct 4 ms 5432 KB Output is correct
86 Correct 5 ms 5332 KB Output is correct
87 Correct 10 ms 5844 KB Output is correct
88 Correct 33 ms 7660 KB Output is correct
89 Correct 97 ms 14076 KB Output is correct
90 Correct 131 ms 14072 KB Output is correct
91 Correct 93 ms 14020 KB Output is correct
92 Correct 54 ms 22756 KB Output is correct
93 Correct 51 ms 22680 KB Output is correct
94 Correct 63 ms 22684 KB Output is correct
95 Correct 52 ms 18796 KB Output is correct
96 Correct 64 ms 19100 KB Output is correct
97 Correct 59 ms 19172 KB Output is correct
98 Correct 72 ms 19176 KB Output is correct
99 Correct 54 ms 18016 KB Output is correct
100 Correct 125 ms 13756 KB Output is correct
101 Correct 154 ms 13992 KB Output is correct
102 Correct 147 ms 14060 KB Output is correct
103 Correct 154 ms 13996 KB Output is correct
104 Correct 58 ms 18032 KB Output is correct
105 Correct 57 ms 17940 KB Output is correct
106 Correct 56 ms 17948 KB Output is correct
107 Correct 116 ms 13280 KB Output is correct
108 Correct 90 ms 13316 KB Output is correct
109 Correct 98 ms 13576 KB Output is correct
110 Correct 50 ms 22036 KB Output is correct
111 Correct 50 ms 18708 KB Output is correct
112 Correct 55 ms 18604 KB Output is correct
113 Correct 50 ms 17324 KB Output is correct
114 Correct 130 ms 13720 KB Output is correct
115 Correct 158 ms 13404 KB Output is correct
116 Correct 60 ms 17388 KB Output is correct
117 Correct 51 ms 15712 KB Output is correct
118 Correct 56 ms 15204 KB Output is correct
119 Correct 55 ms 14728 KB Output is correct
120 Correct 60 ms 15248 KB Output is correct
121 Correct 50 ms 14880 KB Output is correct
122 Correct 51 ms 14392 KB Output is correct
123 Correct 58 ms 15744 KB Output is correct
124 Correct 69 ms 15268 KB Output is correct
125 Correct 59 ms 14828 KB Output is correct
126 Correct 61 ms 15384 KB Output is correct
127 Correct 57 ms 14920 KB Output is correct
128 Correct 59 ms 14448 KB Output is correct