Submission #969554

# Submission time Handle Problem Language Result Execution time Memory
969554 2024-04-25T09:53:59 Z Turkhuu Factories (JOI14_factories) C++17
100 / 100
4112 ms 188880 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int I = 5e5;
int N;
ll len[I], best[I];
int p[I], sz[I], in[I], lg[2 * I], vis[I], dep[I], st[2 * I][20];
vector<int> a;
vector<array<int, 2>> adj[I];
void dfs(int x, int p) {
    in[x] = a.size();
    a.push_back(x);
    for (auto [y, z] : adj[x]) {
        if (y == p) continue;
        dep[y] = dep[x] + 1;
        len[y] = len[x] + z;
        dfs(y, x);
        a.push_back(x);
    }
}
void init(int x, int p) {
    sz[x] = 1;
    for (auto [y, z] : adj[x]) {
        if (vis[y] || y == p) continue;
        init(y, x);
        sz[x] += sz[y];
    }
}
int find(int x, int p, int s) {
    for (auto [y, z] : adj[x]) {
        if (vis[y] || y == p) continue;
        if (sz[y] * 2 > s) return find(y, x, s);
    }
    return x;
}
int cd(int x) {
    init(x, -1);
    x = find(x, -1, sz[x]);
    vis[x] = 1;
    for (auto [y, z] : adj[x]) {
        if (!vis[y]) {
            p[cd(y)] = x;
        }
    }
    return x;
}
int f(int x, int y) {
    return dep[x] < dep[y] ? x : y;
}
void build() {
    for (int i = 0; i < a.size(); i++) {
        st[i][0] = a[i];
    }
    for (int j = 0; j < 19; j++) {
        for (int i = 0; i + (2 << j) <= a.size(); i++) {
            st[i][j + 1] = f(st[i][j], st[i + (1 << j)][j]);
        }
    }
}
int query(int l, int r) {
    int k = lg[r - l];
    return f(st[l][k], st[r - (1 << k)][k]);
}
int lca(int x, int y) {
    return query(min(in[x], in[y]), max(in[x], in[y]) + 1);
}
ll dis(int x, int y) {
    return len[x] + len[y] - 2 * len[lca(x, y)];
}
void Init(int n, int A[], int B[], int D[]) {
    N = n;
    for (int i = 0; i < N - 1; i++) {
        adj[A[i]].push_back({B[i], D[i]});
        adj[B[i]].push_back({A[i], D[i]});
    }
    for (int i = 2; i < 2 * N; i++) {
        lg[i] = lg[i / 2] + 1;
    }
    for (int i = 0; i < N; i++) {
        best[i] = 1e18;
    }
    dfs(0, -1);
    build();
    p[cd(0)] = -1;
}
ll Query(int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; i++) {
        for (int j = X[i]; j != -1; j = p[j]) {
            best[j] = min(best[j], dis(j, X[i]));
        }
    }
    ll ans = 1e18;
    for (int i = 0; i < T; i++) {
        for (int j = Y[i]; j != -1; j = p[j]) {
            ans = min(ans, best[j] + dis(j, Y[i]));
        }
    }
    for (int i = 0; i < S; i++) {
        for (int j = X[i]; j != -1; j = p[j]) {
            best[j] = 1e18;
        }
    }
    return ans;
}

Compilation message

factories.cpp: In function 'void build()':
factories.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 0; i < a.size(); i++) {
      |                     ~~^~~~~~~~~~
factories.cpp:56:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for (int i = 0; i + (2 << j) <= a.size(); i++) {
      |                         ~~~~~~~~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 305 ms 46184 KB Output is correct
3 Correct 417 ms 46224 KB Output is correct
4 Correct 410 ms 46208 KB Output is correct
5 Correct 421 ms 46676 KB Output is correct
6 Correct 166 ms 46160 KB Output is correct
7 Correct 406 ms 46220 KB Output is correct
8 Correct 424 ms 46224 KB Output is correct
9 Correct 416 ms 46484 KB Output is correct
10 Correct 172 ms 46164 KB Output is correct
11 Correct 400 ms 46212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39512 KB Output is correct
2 Correct 1609 ms 157976 KB Output is correct
3 Correct 2207 ms 161620 KB Output is correct
4 Correct 649 ms 158760 KB Output is correct
5 Correct 3157 ms 188880 KB Output is correct
6 Correct 2461 ms 162052 KB Output is correct
7 Correct 996 ms 70300 KB Output is correct
8 Correct 337 ms 70220 KB Output is correct
9 Correct 1026 ms 73728 KB Output is correct
10 Correct 1008 ms 70856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 39516 KB Output is correct
2 Correct 305 ms 46184 KB Output is correct
3 Correct 417 ms 46224 KB Output is correct
4 Correct 410 ms 46208 KB Output is correct
5 Correct 421 ms 46676 KB Output is correct
6 Correct 166 ms 46160 KB Output is correct
7 Correct 406 ms 46220 KB Output is correct
8 Correct 424 ms 46224 KB Output is correct
9 Correct 416 ms 46484 KB Output is correct
10 Correct 172 ms 46164 KB Output is correct
11 Correct 400 ms 46212 KB Output is correct
12 Correct 7 ms 39512 KB Output is correct
13 Correct 1609 ms 157976 KB Output is correct
14 Correct 2207 ms 161620 KB Output is correct
15 Correct 649 ms 158760 KB Output is correct
16 Correct 3157 ms 188880 KB Output is correct
17 Correct 2461 ms 162052 KB Output is correct
18 Correct 996 ms 70300 KB Output is correct
19 Correct 337 ms 70220 KB Output is correct
20 Correct 1026 ms 73728 KB Output is correct
21 Correct 1008 ms 70856 KB Output is correct
22 Correct 2224 ms 157044 KB Output is correct
23 Correct 2353 ms 158056 KB Output is correct
24 Correct 3153 ms 159868 KB Output is correct
25 Correct 3411 ms 162124 KB Output is correct
26 Correct 3374 ms 160420 KB Output is correct
27 Correct 4112 ms 181684 KB Output is correct
28 Correct 851 ms 158880 KB Output is correct
29 Correct 3140 ms 159952 KB Output is correct
30 Correct 3327 ms 159560 KB Output is correct
31 Correct 3329 ms 160408 KB Output is correct
32 Correct 973 ms 74512 KB Output is correct
33 Correct 357 ms 70336 KB Output is correct
34 Correct 793 ms 69040 KB Output is correct
35 Correct 788 ms 69048 KB Output is correct
36 Correct 987 ms 69832 KB Output is correct
37 Correct 976 ms 69828 KB Output is correct