# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
533177 | cig32 | Factories (JOI14_factories) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
static int N, Q;
static int A[MAX_N], B[MAX_N], D[MAX_N];
static int S[MAX_N];
static int T[MAX_N];
static int X[MAX_SUM_ST];
static int Y[MAX_SUM_ST];
static int Qx[MAX_N];
static int Qy[MAX_N];
//#include "factories.h"
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
void Init(int32_t N, int32_t A[], int32_t B[], int32_t D[]);
long long Query(int32_t S, int32_t X[], int32_t T, int32_t Y[]);
const int MAXN = 500003;
vector<pair<int, int> > adj[MAXN];
pair<signed, signed> st[20][2*MAXN];
int tin[MAXN], tout[MAXN], cur = 0;
int dep[MAXN];
int l[MAXN], r[MAXN];
vector<int> e;
void dfs(int node, int prv, int ohno) {
tin[node] = ++cur;
dep[node] = ohno;
e.push_back(node);
for(auto x: adj[node]) {
if(x.first != prv) {
dfs(x.first, node, ohno + x.second);
e.push_back(node);
}
}
tout[node] = ++cur;
}
int lca_dep(int x, int y) {
int m1 = min(l[x], l[y]), m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
return min(st[k][m1], st[k][m2 - (1<<k) + 1]).first;
}
int dist(int x, int y) {
return dep[x] + dep[y] - 2 * lca_dep(x, y);
}
int lca_idx(int x, int y) {
int m1 = min(l[x], l[y]), m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
return min(st[k][m1], st[k][m2 - (1<<k) + 1]).second;
}
void Init(int32_t N, int32_t A[], int32_t B[], int32_t D[]) {
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, -1, 0);
for(int i=0; i<e.size(); i++) {
r[e[i]] = i;
}
for(int i=e.size()-1; i>=0; i--) {
l[e[i]] = i;
}
for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
for(int i=1; i<19; i++) {
for(int j=0; j<e.size(); j++) {
if(j + (1<<i) - 1 < e.size()) {
st[i][j] = min(st[i-1][j], st[i-1][j + (1<<(i-1))]);
}
}
}
}
long long Query(int32_t S, int32_t X[], int32_t T, int32_t Y[]) {
int ans = 1e18;
for(int i=0; i<S; i++) {
for(int j=0; j<T; j++) {
ans = min(ans, dist(X[i], Y[j]));
}
}
return ans;
}
#ifndef ONLINE_JUDGE
int32_t main() {
int32_t i, j, k;
int32_t STop, TTop;
if (2 != scanf("%d%d", &N, &Q)) {
fprintf(stderr, "error: cannot read N and Q.\n");
exit(1);
}
if (!(2 <= N && N <= MAX_N)) {
fprintf(stderr, "error: N is out of bounds.\n");
exit(1);
}
if (!(1 <= Q && Q <= MAX_Q)) {
fprintf(stderr, "error: Q is out of bounds.\n");
exit(1);
}
for (i = 0; i < N - 1; ++i) {
if (1 != scanf("%d", &A[i])) {
fprintf(stderr, "error: cannot read A[%d].\n", i);
exit(1);
}
if (!(0 <= A[i] && A[i] <= N - 1)) {
fprintf(stderr, "error: A[%d] is out of bounds.\n", i);
exit(1);
}
if (1 != scanf("%d", &B[i])) {
fprintf(stderr, "error: cannot read B[%d].\n", i);
exit(1);
}
if (!(0 <= B[i] && B[i] <= N - 1)) {
fprintf(stderr, "error: B[%d] is out of bounds.\n", i);
exit(1);
}
if (A[i] == B[i]) {
fprintf(stderr, "error: B[%d] is equal to A[%d].\n", i, i);
exit(1);
}
if (1 != scanf("%d", &D[i])) {
fprintf(stderr, "error: cannot read D[%d].\n", i);
exit(1);
}
if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
fprintf(stderr, "error: D[%d] is out of bounds.\n", i);
exit(1);
}
}
STop = 0;
TTop = 0;
for (j = 0; j < Q; ++j) {
if (2 != scanf("%d%d", &S[j], &T[j])) {
fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
exit(1);
}
if(STop + S[j] > MAX_SUM_ST) {
fprintf(stderr, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
exit(1);
}
if(TTop + T[j] > MAX_SUM_ST) {
fprintf(stderr, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
exit(1);
}
for (k = 0; k < S[j]; ++k, ++STop) {
if (1 != scanf("%d", &X[STop])) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= X[STop] && X[STop] <= N - 1)) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
}
for (k = 0; k < T[j]; ++k, ++TTop) {
if (1 != scanf("%d", &Y[TTop])) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
}
}
STop = 0;
TTop = 0;
Init(N, A, B, D);
for (j = 0; j < Q; ++j) {
for (k = 0; k < S[j]; k++) {
Qx[k] = X[STop++];
}
for (k = 0; k < T[j]; k++) {
Qy[k] = Y[TTop++];
}
printf("%lld\n", Query(S[j], Qx, T[j], Qy));
}
return 0;
}
#endif