답안 #656613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656613 2022-11-08T02:29:26 Z TranGiaHuy1508 공장들 (JOI14_factories) C++17
100 / 100
2521 ms 447936 KB
#include <bits/stdc++.h>
using namespace std;

#include "factories.h"

using ll = long long;

namespace {
	template <class T>
	struct RMQ{
		vector<vector<T>> table;
		ll _n, _lg;

		RMQ() = default;
		RMQ(vector<T> &v){
			_n = v.size(); _lg = log2(_n);
			table.assign(_lg + 1, vector<T>(_n));

			for (ll i = 0; i < _n; i++) table[0][i] = v[i];

			for (ll j = 1; j <= _lg; j++){
				for (ll i = 0; i + (1 << j) <= _n; i++){
					table[j][i] = min(table[j-1][i], table[j-1][i + (1 << (j-1))]);
				}
			}
		}

		T get(int l, int r){
			ll j = log2(r - l + 1);
			return min(table[j][l], table[j][r - (1 << j) + 1]);
		}
	};

	ll n;
	const ll inf = 1e18;
	vector<vector<pair<ll, ll>>> adj;
	vector<pair<ll, ll>> euler_tour;
	vector<ll> dist, d1, d2;
	vector<ll> tin, tout, firstocc, op;
	ll timer = 0;
	RMQ<pair<ll, ll>> rmq;

	void euler_dfs(ll x, ll p = -1, ll d = 0){
		firstocc[x] = euler_tour.size();
		euler_tour.emplace_back(d, x);
		tin[x] = timer++;
		for (auto [k, w]: adj[x]){
			if (k == p) continue;
			dist[k] = dist[x] + w;
			euler_dfs(k, x, d+1);
			euler_tour.emplace_back(d, x);
		}
		tout[x] = timer++;
	}

	ll LCA(ll x, ll y){
		if (firstocc[x] > firstocc[y]) swap(x, y);
		return rmq.get(firstocc[x], firstocc[y]).second;
	}

	ll distance(ll x, ll y){
		return dist[x] + dist[y] - dist[LCA(x, y)] * 2;
	}
}

void Init(int N, int A[], int B[], int D[]){
	n = N;
	adj.resize(n);
	euler_tour.clear();
	tin.resize(n);
	tout.resize(n);
	firstocc.resize(n);
	dist.assign(n, 0);
	d1.assign(n, inf);
	d2.assign(n, inf);
	op.assign(n, 0);

	for (ll i = 0; i < n-1; i++){
		adj[A[i]].emplace_back(B[i], D[i]);
		adj[B[i]].emplace_back(A[i], D[i]);
	}

	euler_dfs(0);
	rmq = RMQ<pair<ll, ll>>(euler_tour);
}

ll Query(int S, int X[], int T, int Y[]){
	for (ll i = 0; i < S; i++) op[X[i]] = 1;
	for (ll i = 0; i < T; i++) op[Y[i]] = 2;

	vector<ll> Z;
	for (ll i = 0; i < S; i++) Z.push_back(X[i]);
	for (ll i = 0; i < T; i++) Z.push_back(Y[i]);

	sort(Z.begin(), Z.end(), [&](ll a, ll b){
		return tin[a] < tin[b];
	});

	for (ll i = 1; i < S + T; i++){
		ll lca = LCA(Z[i-1], Z[i]);
		if (!op[lca]){
			op[lca] = 3;
			Z.push_back(lca);
		}
	}

	sort(Z.begin(), Z.end(), [&](ll a, ll b){
		return tin[a] < tin[b];
	});

	stack<ll> parent;
	ll res = inf;

	for (ll i = 0; i < (ll)Z.size(); i++){
		while (!parent.empty()){
			if (tin[parent.top()] <= tin[Z[i]] && tout[Z[i]] <= tout[parent.top()]) break;
			ll tp = parent.top(); parent.pop();
			res = min(res, d1[tp] + d2[tp]);
			if (!parent.empty()){
				ll dt = distance(parent.top(), tp);
				d1[parent.top()] = min(d1[parent.top()], d1[tp] + dt);
				d2[parent.top()] = min(d2[parent.top()], d2[tp] + dt);
			}
		}

		parent.push(Z[i]);
		if (op[Z[i]] == 1) d1[Z[i]] = 0;
		if (op[Z[i]] == 2) d2[Z[i]] = 0;
	}

	while (!parent.empty()){
		ll tp = parent.top(); parent.pop();
		res = min(res, d1[tp] + d2[tp]);
		if (!parent.empty()){
			ll dt = distance(parent.top(), tp);
			d1[parent.top()] = min(d1[parent.top()], d1[tp] + dt);
			d2[parent.top()] = min(d2[parent.top()], d2[tp] + dt);
		}
	}

	for (auto i: Z) op[i] = 0;
	for (auto i: Z) d1[i] = d2[i] = inf;

	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 852 KB Output is correct
2 Correct 593 ms 11536 KB Output is correct
3 Correct 669 ms 11536 KB Output is correct
4 Correct 618 ms 11572 KB Output is correct
5 Correct 563 ms 11792 KB Output is correct
6 Correct 510 ms 11464 KB Output is correct
7 Correct 661 ms 11660 KB Output is correct
8 Correct 628 ms 11796 KB Output is correct
9 Correct 562 ms 11876 KB Output is correct
10 Correct 516 ms 11568 KB Output is correct
11 Correct 664 ms 11548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1417 ms 420696 KB Output is correct
3 Correct 1440 ms 424616 KB Output is correct
4 Correct 1018 ms 418508 KB Output is correct
5 Correct 1179 ms 447936 KB Output is correct
6 Correct 1609 ms 424920 KB Output is correct
7 Correct 1363 ms 86264 KB Output is correct
8 Correct 816 ms 85484 KB Output is correct
9 Correct 740 ms 89268 KB Output is correct
10 Correct 1323 ms 86580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 852 KB Output is correct
2 Correct 593 ms 11536 KB Output is correct
3 Correct 669 ms 11536 KB Output is correct
4 Correct 618 ms 11572 KB Output is correct
5 Correct 563 ms 11792 KB Output is correct
6 Correct 510 ms 11464 KB Output is correct
7 Correct 661 ms 11660 KB Output is correct
8 Correct 628 ms 11796 KB Output is correct
9 Correct 562 ms 11876 KB Output is correct
10 Correct 516 ms 11568 KB Output is correct
11 Correct 664 ms 11548 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1417 ms 420696 KB Output is correct
14 Correct 1440 ms 424616 KB Output is correct
15 Correct 1018 ms 418508 KB Output is correct
16 Correct 1179 ms 447936 KB Output is correct
17 Correct 1609 ms 424920 KB Output is correct
18 Correct 1363 ms 86264 KB Output is correct
19 Correct 816 ms 85484 KB Output is correct
20 Correct 740 ms 89268 KB Output is correct
21 Correct 1323 ms 86580 KB Output is correct
22 Correct 2082 ms 423532 KB Output is correct
23 Correct 2185 ms 424156 KB Output is correct
24 Correct 2202 ms 427024 KB Output is correct
25 Correct 2274 ms 428512 KB Output is correct
26 Correct 2521 ms 427668 KB Output is correct
27 Correct 1979 ms 445888 KB Output is correct
28 Correct 1646 ms 422060 KB Output is correct
29 Correct 2458 ms 427088 KB Output is correct
30 Correct 2463 ms 426616 KB Output is correct
31 Correct 2491 ms 427172 KB Output is correct
32 Correct 972 ms 90600 KB Output is correct
33 Correct 929 ms 86016 KB Output is correct
34 Correct 1262 ms 85084 KB Output is correct
35 Correct 1270 ms 85044 KB Output is correct
36 Correct 1362 ms 85992 KB Output is correct
37 Correct 1402 ms 85816 KB Output is correct