Submission #1082917

# Submission time Handle Problem Language Result Execution time Memory
1082917 2024-09-02T05:48:45 Z pemguimn Factories (JOI14_factories) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
//#include "factories.h"
using namespace std;

const int maxN = 5e5 + 5, LOG = 20;
const long long INF = (long long) 1e18 + 5;
vector<pair<int, int>> adj[maxN];

int h[maxN], par[LOG][maxN];
long long w[maxN];
void dfs(int i, int pre){
    for(pair<int, int> e : adj[i]){
        int x = e.first, c = e.second;
        if(x == pre) continue;
        h[x] = h[i] + 1, w[x] = w[i] + c;
        par[0][x] = i;
        for(int j = 1; j < LOG; j++) par[j][x] = par[j - 1][par[j - 1][x]];
        dfs(x, i);
    }
}

int lca(int x, int y){
    if(h[x] < h[y]) swap(x, y);
    int d = h[x] - h[y];
    for(int i = 0; i < LOG; i++){
        if(d & (1 << i)) x = par[i][x];
    }
    if(x == y) return x;
    for(int i = LOG - 1; i >= 0; i--){
        if(par[i][x] != par[i][y]) x = par[i][x], y = par[i][y];
    }
    return par[0][x];
}

map<pair<int, int>, long long> mp;
int c[5005][5005];
void Init(int N, int A[], int B[], int D[]){
    mp.clear();
    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]});
    }
    dfs(0, 0);
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            c[i][j] = w[i] + w[j] - 2 * w[lca(i, j)];
        }
    }
}

long long Query(int S, int X[], int T, int Y[]){
    long long ans = INF;
    for(int i = 0; i < S; i++){
        for(int j = 0; j < T; j++){
            pair<int, int> key = {X[i], Y[j]};
            ans = min(ans, c[X[i][Y[j]]);
//            if(mp.find(key) != mp.end()){
//                ans = min(ans, mp[key]);
//            } else{
//                mp[key] = w[X[i]] + w[Y[j]] - 2 * w[lca(X[i], Y[j])];
//                ans = min(ans, mp[{X[i], Y[j]}]);
//            }
        }
    }
    return ans;
}

#ifdef LOCAL
const int MAX = 1e3 + 5;
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int n, q; cin >> n >> q;
    int x[MAX], y[MAX], d[MAX];
    for(int i = 0; i < n - 1; i++){
        cin >> x[i] >> y[i] >> d[i];
    }
    Init(n, x, y, d);
    for(int i = 1; i <= q; i++){
        int s, t; cin >> s >> t;
        int a[MAX], b[MAX];
        for(int i = 0; i < s; i++) cin >> a[i];
        for(int i = 0; i < t; i++) cin >> b[i];
        cout << Query(s, a, t, b) << '\n';
    }
    return 0;
}
#endif

Compilation message

factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:56:34: error: invalid types 'int[int]' for array subscript
   56 |             ans = min(ans, c[X[i][Y[j]]);
      |                                  ^
factories.cpp:56:40: error: expected ']' before ')' token
   56 |             ans = min(ans, c[X[i][Y[j]]);
      |                                        ^
      |                                        ]