Submission #971113

# Submission time Handle Problem Language Result Execution time Memory
971113 2024-04-28T01:27:13 Z Whisper Factories (JOI14_factories) C++17
100 / 100
6974 ms 299800 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 does 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 25 ms 47708 KB Output is correct
2 Correct 346 ms 59340 KB Output is correct
3 Correct 404 ms 59472 KB Output is correct
4 Correct 413 ms 59356 KB Output is correct
5 Correct 478 ms 59740 KB Output is correct
6 Correct 210 ms 59452 KB Output is correct
7 Correct 415 ms 59452 KB Output is correct
8 Correct 412 ms 59492 KB Output is correct
9 Correct 458 ms 59712 KB Output is correct
10 Correct 205 ms 59464 KB Output is correct
11 Correct 413 ms 59472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45660 KB Output is correct
2 Correct 2379 ms 272060 KB Output is correct
3 Correct 3350 ms 273660 KB Output is correct
4 Correct 923 ms 272832 KB Output is correct
5 Correct 4988 ms 298588 KB Output is correct
6 Correct 3446 ms 275028 KB Output is correct
7 Correct 1475 ms 110684 KB Output is correct
8 Correct 468 ms 110796 KB Output is correct
9 Correct 1783 ms 113808 KB Output is correct
10 Correct 1510 ms 111148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47708 KB Output is correct
2 Correct 346 ms 59340 KB Output is correct
3 Correct 404 ms 59472 KB Output is correct
4 Correct 413 ms 59356 KB Output is correct
5 Correct 478 ms 59740 KB Output is correct
6 Correct 210 ms 59452 KB Output is correct
7 Correct 415 ms 59452 KB Output is correct
8 Correct 412 ms 59492 KB Output is correct
9 Correct 458 ms 59712 KB Output is correct
10 Correct 205 ms 59464 KB Output is correct
11 Correct 413 ms 59472 KB Output is correct
12 Correct 8 ms 45660 KB Output is correct
13 Correct 2379 ms 272060 KB Output is correct
14 Correct 3350 ms 273660 KB Output is correct
15 Correct 923 ms 272832 KB Output is correct
16 Correct 4988 ms 298588 KB Output is correct
17 Correct 3446 ms 275028 KB Output is correct
18 Correct 1475 ms 110684 KB Output is correct
19 Correct 468 ms 110796 KB Output is correct
20 Correct 1783 ms 113808 KB Output is correct
21 Correct 1510 ms 111148 KB Output is correct
22 Correct 3240 ms 277332 KB Output is correct
23 Correct 3419 ms 278836 KB Output is correct
24 Correct 4808 ms 279516 KB Output is correct
25 Correct 4793 ms 282152 KB Output is correct
26 Correct 4938 ms 278920 KB Output is correct
27 Correct 6974 ms 299800 KB Output is correct
28 Correct 1256 ms 280400 KB Output is correct
29 Correct 4868 ms 278592 KB Output is correct
30 Correct 4541 ms 278100 KB Output is correct
31 Correct 4502 ms 278872 KB Output is correct
32 Correct 1755 ms 115152 KB Output is correct
33 Correct 481 ms 111304 KB Output is correct
34 Correct 1215 ms 109180 KB Output is correct
35 Correct 1152 ms 109168 KB Output is correct
36 Correct 1461 ms 109908 KB Output is correct
37 Correct 1448 ms 109652 KB Output is correct