#include "factories.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define ll long long
#define sz(x) (int)x.size()
template<class T> bool umin(T& a, const T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool umax(T& a, const T b) { if(a < b) { a = b; return 1; } return 0; }
const int MAXN = 5e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
vector<pii> edges[MAXN], ss;
int n, tin[MAXN], tout[MAXN], t, lg[MAXN];
pii st[MAXN*2][20];
ll dep[MAXN];
void dfs(int u, int p = -1){
ss.pb({dep[u], u});
tin[u] = sz(ss)-1;
for(auto x : edges[u]){
if(x.ff == p) continue;
dep[x.ff] = dep[u] + (ll)x.ss;
dfs(x.ff, u);
ss.pb({dep[u], u});
} tout[u] = sz(ss)-1;
}
void Init(int n, int A[], int B[], int D[]) {
for(int i=0;i<N-1;i++){
edges[A[i]].pb({B[i], D[i]}), edges[B[i]].pb({A[i], D[i]});
} dfs(1); lg[1] = 0;
for(int i=2;i<=n;i++) lg[i] = lg[i/2]+1;
for(int i=0;i<sz(ss);i++) st[i][0] = ss[i];
for(int j=1;j<20;j++){
for(int i=0;i<sz(ss);i++){
if(i + (1<<(j - 1)) < sz(ss)) st[i][j] = min(st[i][j-1], st[i+(1<<(j-1))][j-1]);
else st[i][j] = st[i][j-1];
}
}
}
bool par(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); }
int find_lca(int a, int b){
if(par(a, b)) return a;
if(par(b, a)) return b;
if(tin[a] > tout[b]) swap(a, b);
int l = tout[a], r = tin[b], llg = lg[r-l+1];
pii mn = min(st[l][llg], st[r-(1<<llg)][llg]);
return mn.ss;
}
ll Query(int s, int x[], int t, int y[]) {
ll res = inf;
for(int i=0;i<s;i++){
for(int j=0;j<t;j++){
int a = x[i], b = y[j];
int lca = find_lca(a, b);
umin(res, dep[a] + dep[b] - dep[lca] * 2);
}
} return res;
}
Compilation message
factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:41:16: error: 'N' was not declared in this scope
41 | for(int i=0;i<N-1;i++){
| ^