Submission #970861

# Submission time Handle Problem Language Result Execution time Memory
970861 2024-04-27T11:44:41 Z vjudge1 Factories (JOI14_factories) C++17
100 / 100
6620 ms 283776 KB
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
using ll = long long;

//#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)

constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

//void setupIO(){
//    #define name "Whisper"
//    //Phu Trong from Nguyen Tat Thanh High School for gifted student
//    srand(time(NULL));
//    cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//    freopen(name".inp", "r", stdin);
//    freopen(name".out", "w", stdout);
//    cout << fixed << setprecision(10);
//}

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
const int MAX = 5e5 + 5;
int numNode, numQuery;


vector<int> to, from;

//Centroid Tree is used to solve this problem
//The max depth of the tree not exceed log(N)


struct Edge{
    int u, v, w;

    Edge(){}
    Edge(int _u, int _v, int _w){
        u = _u, v = _v, w = _w;
    }
    int other(int x){return (u ^ v ^ x);}
} edge[MAX];

vector<int> G[MAX];
long long sz[MAX], del[MAX], par[MAX], dist[MAX];
int reSubsize(int u, int p){
    sz[u] = 1;
    for (int&i : G[u]) {
        int v = edge[i].other(u);
        if(del[v]) continue;
        if(v ^ p){
            sz[u] += reSubsize(v, u);
        }
    }
    return sz[u];
}
int getCentroid(int u, int p, int siz){
    for (int& i : G[u]){
        int v = edge[i].other(u);
        if(v ^ p){
            if(!del[v] && 2 * sz[v] > siz) return getCentroid(v, u, siz);
        }
    }
    return u;
}
#define MIN_HIGH(u, v) (dep[u] < dep[v] ? (u) : (v))

long long dep[MAX], node[MAX << 1], st[MAX << 1][21], pos[MAX], D[MAX];
int cnt = 0;
void dfs(int u, int p = -1){
    node[++cnt] = u;
    pos[u] = cnt;
    for (int &i : G[u]){
        int v = edge[i].other(u);
        if(v ^ p){
            D[v] = D[u] + edge[i].w;
            dep[v] = dep[u] + 1;
            dfs(v, u);
            node[++cnt] = u;
        }
    }
}
int lca(int u, int v){
    int l = pos[u], r = pos[v];
    if (l > r) swap(l, r);

    int k = 31 - __builtin_clz(r - l + 1);
    return MIN_HIGH(st[l][k], st[r - (1 << k) + 1][k]);
}

long long F(int u, int v){
    return D[u] + D[v] - 2 * D[lca(u, v)];
}
void buildCT(int root, int p){
    root = getCentroid(root, p, reSubsize(root, p));
    del[root] = 1;
    par[root] = p;
    dist[root] = LINF;
    for (int&i : G[root]){
        int v = edge[i].other(root);
        if(del[v]) continue;
        if(v ^ p){
            buildCT(v, root);
        }
    }
}
void Init(int N, int A[], int B[], int D[]){
    numNode = N;
    for (int i = 0; i < numNode - 1; ++i){
        ++A[i], ++B[i];
        edge[i + 1] = Edge(A[i], B[i], D[i]);
        G[A[i]].emplace_back(i + 1);
        G[B[i]].emplace_back(i + 1);
    }
    dfs(1);

    for (int i = 1; i <= cnt; ++i) st[i][0] = node[i];
    for (int k = 1; MASK(k) <= cnt; ++k){
        for (int i = 1; i + MASK(k) - 1 <= cnt; ++i){
            st[i][k] = MIN_HIGH(st[i][k - 1], st[i + MASK(k - 1)][k - 1]);
        }
    }
    buildCT(1, -1);
}



long long Solve(){
    long long res = LINF;
    for (int i = 0; i < (int)from.size(); ++i){
        for (int cur = from[i] + 1; cur != -1; cur = par[cur]){
            minimize(dist[cur], F(cur, from[i] + 1));
        }
    }
    for (int i = 0; i < (int)to.size(); ++i){
        for (int cur = to[i] + 1; cur != -1; cur = par[cur]){
            minimize(res, dist[cur] + F(cur, to[i] + 1));
        }
    }
    for (int i = 0; i < (int)from.size(); ++i){
        for (int cur = from[i] + 1; cur != -1; cur = par[cur]){
            dist[cur] = LINF;
        }
    }
    return res;
}
long long Query(int S, int X[], int T, int Y[]){
    from.clear(); to.clear();
    FOR(i, 0, T - 1) from.push_back(Y[i]);
    FOR(i, 0, S - 1) to.push_back(X[i]);

    return Solve();
}

//int N, A[MAX], B[MAX], Di[MAX], Q;
//int X[MAX], Y[MAX];
//void Whisper(){
//    cin >> N >> Q;
//    for (int i = 0; i < N - 1; ++i){
//        cin >> A[i] >> B[i] >> Di[i];
//    }
//    Init(N, A, B, Di);
//    for (int i = 1; i <= Q; ++i){
//        int S, T; cin >> S >> T;
//        FOR(v, 0, S - 1) cin >> X[v];
//        FOR(v, 0, T - 1) cin >> Y[v];
//        cout << Query(S, X, T, Y) << '\n';
//    }
//}
//
//
//signed main(){
//    setupIO();
//    int Test = 1;
////    cin >> Test;
//    for ( int i = 1 ; i <= Test ; i++ ){
//        Whisper();
//        if (i < Test) cout << '\n';
//    }
//}


# Verdict Execution time Memory Grader output
1 Correct 19 ms 47708 KB Output is correct
2 Correct 347 ms 49992 KB Output is correct
3 Correct 410 ms 50004 KB Output is correct
4 Correct 457 ms 50032 KB Output is correct
5 Correct 505 ms 50400 KB Output is correct
6 Correct 227 ms 49888 KB Output is correct
7 Correct 404 ms 49988 KB Output is correct
8 Correct 407 ms 50004 KB Output is correct
9 Correct 501 ms 50260 KB Output is correct
10 Correct 223 ms 50004 KB Output is correct
11 Correct 431 ms 49988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45404 KB Output is correct
2 Correct 2332 ms 253240 KB Output is correct
3 Correct 3144 ms 259788 KB Output is correct
4 Correct 820 ms 258528 KB Output is correct
5 Correct 4860 ms 283776 KB Output is correct
6 Correct 3478 ms 260188 KB Output is correct
7 Correct 1588 ms 96720 KB Output is correct
8 Correct 417 ms 96684 KB Output is correct
9 Correct 1890 ms 99788 KB Output is correct
10 Correct 1564 ms 97344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 47708 KB Output is correct
2 Correct 347 ms 49992 KB Output is correct
3 Correct 410 ms 50004 KB Output is correct
4 Correct 457 ms 50032 KB Output is correct
5 Correct 505 ms 50400 KB Output is correct
6 Correct 227 ms 49888 KB Output is correct
7 Correct 404 ms 49988 KB Output is correct
8 Correct 407 ms 50004 KB Output is correct
9 Correct 501 ms 50260 KB Output is correct
10 Correct 223 ms 50004 KB Output is correct
11 Correct 431 ms 49988 KB Output is correct
12 Correct 8 ms 45404 KB Output is correct
13 Correct 2332 ms 253240 KB Output is correct
14 Correct 3144 ms 259788 KB Output is correct
15 Correct 820 ms 258528 KB Output is correct
16 Correct 4860 ms 283776 KB Output is correct
17 Correct 3478 ms 260188 KB Output is correct
18 Correct 1588 ms 96720 KB Output is correct
19 Correct 417 ms 96684 KB Output is correct
20 Correct 1890 ms 99788 KB Output is correct
21 Correct 1564 ms 97344 KB Output is correct
22 Correct 3402 ms 253312 KB Output is correct
23 Correct 3597 ms 254196 KB Output is correct
24 Correct 4636 ms 257272 KB Output is correct
25 Correct 4664 ms 261504 KB Output is correct
26 Correct 4841 ms 259032 KB Output is correct
27 Correct 6620 ms 278516 KB Output is correct
28 Correct 1133 ms 259260 KB Output is correct
29 Correct 4384 ms 258452 KB Output is correct
30 Correct 4673 ms 257744 KB Output is correct
31 Correct 4502 ms 258772 KB Output is correct
32 Correct 1933 ms 101288 KB Output is correct
33 Correct 428 ms 97480 KB Output is correct
34 Correct 1286 ms 95712 KB Output is correct
35 Correct 1305 ms 95572 KB Output is correct
36 Correct 1471 ms 96128 KB Output is correct
37 Correct 1620 ms 95984 KB Output is correct