Submission #1263894

#TimeUsernameProblemLanguageResultExecution timeMemory
1263894wedonttalkanymoreFactories (JOI14_factories)C++20
100 / 100
3983 ms465892 KiB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

//#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
#define MAX_N          500000
#define MAX_Q          100000
#define MAX_SUM_ST    1000000
#define MAX_VALUE  1000000000

const ll MAXN = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 20;

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];

int in[2 * MAXN], out[2 * MAXN], timedfs, h[MAXN], lg[MAXN];
pii st[2 * MAXN][lim + 1];
int n;
vector <pii> adj[MAXN], mini_adj[MAXN];
ll dist[MAXN];

void predfs(int u, int par) {
	in[u] = out[u] = ++timedfs;
	st[timedfs][0] = make_pair(h[u], u);
	for (auto [v, w] : adj[u]) {
		if (v == par) continue;
		dist[v] = dist[u] + w;
		h[v] = h[u] + 1;
		predfs(v, u);
		st[++timedfs][0] = make_pair(h[u], u);
	}
	out[u] = timedfs;
}
void build() {
	predfs(0, -1);
	for (int i = 2; i <= timedfs; i++) lg[i] = lg[i / 2] + 1;
	for (int i = 1; i <= lim; i++) {
		for (int j = 1; j + (1 << i) - 1 <= timedfs; j++) {
			st[j][i] = min(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
		}
	}
}

int lca(int u, int v) {
	int l = in[u], r = in[v];
	if (l > r) swap(l, r);
	int k = lg[r - l + 1];
	return min(st[l][k], st[r - (1 << k) + 1][k]).se;
}
ll distance(int u, int v) {
	return dist[u] + dist[v] - 2 * dist[lca(u, v)];
}

bool cmp(int a, int b) {
	return in[a] < in[b];
}

void Init(int N, int A[], int B[], int D[]) {
	n = N;
	for (int i = 0; i < n - 1; i++) {
		adj[A[i]].emplace_back(B[i], D[i]);
		adj[B[i]].emplace_back(A[i], D[i]);
	}
	build();
}

bool check(int x, int y) {
	return in[x] <= in[y] && out[y] <= out[x];
}

ll length[MAXN];
ll minn = inf;
vector <int> comp;
vector <int> val1, val2;
int ownX[MAXN], ownY[MAXN];

void dijk(int id) {
	for (int i = 0; i < (int)comp.size(); i++) length[comp[i]] = inf;
	priority_queue <pii, vector <pii>, greater <pii> > q;
	if (!id) {
		for (int i = 0; i < (int)val1.size(); i++) {
			length[val1[i]] = 0;
			q.push({0, val1[i]});
		}
	}
	else {
		for (int i = 0; i < (int)val2.size(); i++) {
			length[val2[i]] = 0;
			q.push({0, val2[i]});
		}
	}
	while(q.size()) {
		auto [c, u] = q.top();
		q.pop();
		if (c != length[u]) continue;
		for (auto [v, w] : mini_adj[u]) {
			if (length[v] > length[u] + w) {
				length[v] = length[u] + w;
				if ((!id && ownY[v]) || (id && ownX[v])) minn = min(minn, length[v]);
				q.push({length[v], v});
			}
		}
	}
}

void reset() {
	comp.clear();
	val1.clear();
	val2.clear();
	minn = inf;
}

long long Query(int S, int X[], int T, int Y[]) {
	reset();
	for (int i = 0; i < S; i++) {
		comp.push_back(X[i]);
		val1.push_back(X[i]);
		ownX[X[i]]++;
	}
	for (int i = 0; i < T; i++) {
		comp.push_back(Y[i]);
		val2.push_back(Y[i]);
		ownY[Y[i]]++;
	}
	sort(comp.begin(), comp.end(), cmp);
	int sz = comp.size();
	for (int i = 0; i < sz - 1; i++) {
		comp.push_back(lca(comp[i], comp[i + 1]));
	}
	sort(comp.begin(), comp.end(), cmp);
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	sz = comp.size();
	stack <int> st;
	st.push(comp[0]);
	for (int i = 1; i < sz; i++) {
		while(!st.empty() && !check(st.top(), comp[i])) st.pop();
		if (st.size()) {
			mini_adj[st.top()].emplace_back(comp[i], distance(comp[i], st.top()));
			mini_adj[comp[i]].emplace_back(st.top(), distance(comp[i], st.top()));
		}
		st.push(comp[i]);
	}
	dijk(0);
	dijk(1);
	sz = comp.size();
	for (int i = 0; i < sz; i++) mini_adj[comp[i]].clear();
	for (int i = 0; i < S; i++) ownX[X[i]]--;
	for (int i = 0; i < T; i++) ownY[Y[i]]--;
	return minn;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...