Submission #684030

# Submission time Handle Problem Language Result Execution time Memory
684030 2023-01-20T04:39:49 Z nguyentunglam Factories (JOI14_factories) C++17
100 / 100
2245 ms 197128 KB
#include "factories.h"
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 5e5 + 10;
int st[N], ed[N], h[N], vtx[N << 1], f[21][N << 1], num[N], a[N << 1];
long long d[N], nearest[3][N], res;
int cnt, timer;
bool mark1[N], mark2[N];
vector<pair<int, int> > adj[N];
vector<int> ke[N];
void dfs(int u) {
    st[u] = ++timer;
    vtx[++cnt] = u;
    num[u] = cnt;
    for(auto [v, w] : adj[u]) if (!st[v]) {
        h[v] = h[u] + 1; d[v] = d[u] + w;
        dfs(v);
        vtx[++cnt] = u;
    }
    ed[u] = timer;
}
int lca(int u, int v) {
    int l = num[u], r = num[v];
    if (l > r) swap(l, r);
    int k = log2(r - l + 1);
    int x = f[k][l], y = f[k][r - (1 << k) + 1];
    return h[x] < h[y] ? x : y;
}
void solve(int u) {
    nearest[1][u] = nearest[2][u] = 1e18;
    if (mark1[u]) nearest[1][u] = d[u];
    if (mark2[u]) nearest[2][u] = d[u];
    for(int v : ke[u]) {
        solve(v);
        for(int j = 1; j <= 2; j++) nearest[j][u] = min(nearest[j][u], nearest[j][v]);
    }
    res = min(res, nearest[1][u] + nearest[2][u] - 2LL * d[u]);
}
void Init (int n, int a[], int b[], int d[]) {
    for(int i = 0; i <= n - 2; i++) {
        adj[a[i]].emplace_back(b[i], d[i]);
        adj[b[i]].emplace_back(a[i], d[i]);
    }
    dfs(0);
    for(int i = 1; i <= cnt; i++) f[0][i] = vtx[i];
    for(int j = 1; j <= log2(cnt); j++) for(int i = 1; i + (1 << j) - 1 <= cnt; i++) {
        int x = f[j - 1][i], y = f[j - 1][i + (1 << (j - 1))];
        f[j][i] = h[x] < h[y] ? x : y;
    }
}
long long Query(int s, int x[], int t, int y[]) {
    res = 1e18;
    int k = 0;
    for(int i = 0; i < s; i++) a[++k] = x[i], mark1[x[i]] = 1;
    for(int i = 0; i < t; i++) a[++k] = y[i], mark2[y[i]] = 1;
    sort(a + 1, a + k + 1, [] (int &x, int &y) { return st[x] < st[y]; });
    stack<int> S;
    a[0] = a[k];
    for(int i = 1; i <= k; i++) a[i + k] = lca(a[i], a[i - 1]);
    k <<= 1;
    sort(a + 1, a + k + 1, [] (int &x, int &y) { return st[x] < st[y]; });
    S.push(a[1]);
    for(int i = 2; i <= k; i++) if (a[i] != a[i - 1]) {
        while (ed[S.top()] < st[a[i]]) S.pop();
        ke[S.top()].push_back(a[i]);
        S.push(a[i]);
    }
    solve(a[1]);
    for(int i = 1; i <= k; i++) mark1[a[i]] = mark2[a[i]] = 0, ke[a[i]].clear();
    return res;
}
//int main() {
//    #define task ""
//    cin.tie(0) -> sync_with_stdio(0);
//    if (fopen ("task.inp", "r")) {
//        freopen ("task.inp", "r", stdin);
//        freopen ("task.out", "w", stdout);
//    }
//    if (fopen (task".inp", "r")) {
//        freopen (task".inp", "r", stdin);
//        freopen (task".out", "w", stdout);
//    }
//    int n, q; cin >> n >> q;
//    for(int i = 1; i < n; i++) {
//        int u, v, d; cin >> u >> v >> d;
//        adj[u].emplace_back(v, d);
//        adj[v].emplace_back(u, d);
//    }
//    dfs(0);
//    for(int i = 1; i <= cnt; i++) f[0][i] = vtx[i];
//    for(int j = 1; j <= log2(cnt); j++) for(int i = 1; i + (1 << j) - 1 <= cnt; i++) {
//        int x = f[j - 1][i], y = f[j - 1][i + (1 << (j - 1))];
//        f[j][i] = h[x] < h[y] ? x : y;
//    }
//    while (q--) {
//        res = 1e18;
//        int s, t; cin >> s >> t;
//        for(int i = 1; i <= s; i++) cin >> a[i], mark1[a[i]] = 1;
//        for(int i = s + 1; i <= s + t; i++) cin >> a[i], mark2[a[i]] = 2;
//        int k = s + t;
//        sort(a + 1, a + k + 1, [] (int &x, int &y) { return st[x] < st[y]; });
//        stack<int> S;
//        a[0] = a[k];
//        for(int i = 1; i <= k; i++) a[i + k] = lca(a[i], a[i - 1]);
//        k <<= 1;
//        sort(a + 1, a + k + 1, [] (int &x, int &y) { return st[x] < st[y]; });
//        S.push(a[1]);
//        for(int i = 2; i <= k; i++) if (a[i] != a[i - 1]) {
//            while (ed[S.top()] < st[a[i]]) S.pop();
//            ke[S.top()].push_back(a[i]);
//            S.push(a[i]);
//        }
//        solve(a[1]);
//        cout << res << endl;
//        for(int i = 1; i <= k; i++) mark1[a[i]] = mark2[a[i]] = 0, ke[a[i]].clear();
//    }
//}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24276 KB Output is correct
2 Correct 657 ms 32984 KB Output is correct
3 Correct 668 ms 42472 KB Output is correct
4 Correct 682 ms 42548 KB Output is correct
5 Correct 667 ms 42748 KB Output is correct
6 Correct 433 ms 42412 KB Output is correct
7 Correct 628 ms 42524 KB Output is correct
8 Correct 661 ms 42544 KB Output is correct
9 Correct 598 ms 42824 KB Output is correct
10 Correct 427 ms 42384 KB Output is correct
11 Correct 654 ms 42512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24148 KB Output is correct
2 Correct 977 ms 154076 KB Output is correct
3 Correct 1122 ms 157340 KB Output is correct
4 Correct 761 ms 154744 KB Output is correct
5 Correct 1212 ms 190792 KB Output is correct
6 Correct 1267 ms 158904 KB Output is correct
7 Correct 1071 ms 57348 KB Output is correct
8 Correct 731 ms 57584 KB Output is correct
9 Correct 845 ms 63556 KB Output is correct
10 Correct 939 ms 58696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24276 KB Output is correct
2 Correct 657 ms 32984 KB Output is correct
3 Correct 668 ms 42472 KB Output is correct
4 Correct 682 ms 42548 KB Output is correct
5 Correct 667 ms 42748 KB Output is correct
6 Correct 433 ms 42412 KB Output is correct
7 Correct 628 ms 42524 KB Output is correct
8 Correct 661 ms 42544 KB Output is correct
9 Correct 598 ms 42824 KB Output is correct
10 Correct 427 ms 42384 KB Output is correct
11 Correct 654 ms 42512 KB Output is correct
12 Correct 17 ms 24148 KB Output is correct
13 Correct 977 ms 154076 KB Output is correct
14 Correct 1122 ms 157340 KB Output is correct
15 Correct 761 ms 154744 KB Output is correct
16 Correct 1212 ms 190792 KB Output is correct
17 Correct 1267 ms 158904 KB Output is correct
18 Correct 1071 ms 57348 KB Output is correct
19 Correct 731 ms 57584 KB Output is correct
20 Correct 845 ms 63556 KB Output is correct
21 Correct 939 ms 58696 KB Output is correct
22 Correct 2025 ms 166488 KB Output is correct
23 Correct 1807 ms 170388 KB Output is correct
24 Correct 2245 ms 174944 KB Output is correct
25 Correct 2101 ms 173924 KB Output is correct
26 Correct 2005 ms 165836 KB Output is correct
27 Correct 2098 ms 197128 KB Output is correct
28 Correct 1099 ms 165064 KB Output is correct
29 Correct 1923 ms 164584 KB Output is correct
30 Correct 1926 ms 166680 KB Output is correct
31 Correct 1911 ms 167348 KB Output is correct
32 Correct 1064 ms 79032 KB Output is correct
33 Correct 629 ms 73264 KB Output is correct
34 Correct 993 ms 69628 KB Output is correct
35 Correct 1027 ms 69316 KB Output is correct
36 Correct 1043 ms 70380 KB Output is correct
37 Correct 1048 ms 70272 KB Output is correct