Submission #921897

# Submission time Handle Problem Language Result Execution time Memory
921897 2024-02-04T13:10:01 Z falaz Factories (JOI14_factories) C++17
100 / 100
3157 ms 287520 KB
//#include "factories.h"
#include <bits/stdc++.h> 
using namespace std;
 
const int M = 5e5 + 1;
int st[M], fn[M], t, cnt, n , loc[M], ql, qr , TV = 1;
vector<pair<int, long long>> adj[M];
long long dis[M], seg[2][M << 2];
pair<int, int> par[20][M << 1];
 
void dfs(int v, int p, long long d1, int h) {
	dis[v] = d1;
	st[v] = t++;
	loc[v] = cnt;
	par[0][cnt++] = {h, v};
	for (auto u : adj[v]) 
		if (u.first != p) { 
			dfs(u.first, v, d1 + 1ll * u.second, h + 1);
			par[0][cnt++] = {h, v};
		}
	fn[v] = t - 1;
	return;
}
 
void update(int i, long long k, bool g) {
	i = i + TV;
	seg[g][i] = k;
	i /= 2;
	while (i) {
		seg[g][i] = min(seg[g][i << 1], seg[g][i << 1 | 1]);
		i /= 2;
	}
}
 
long long get(bool g) {
	ql = ql + TV;
	qr = qr + 1 + TV;
	long long ret = 1e18;
	while (ql < qr) {
		if (ql & 1)
			ret = min(ret, seg[g][ql++]);
		if (qr & 1)
			ret = min(ret, seg[g][--qr]);
		ql /= 2;
		qr /= 2;
	}
	return ret;
}
 
void rem(int i, bool g) {
	i = i + TV;
	seg[g][i] = 1e18;
	i /= 2;
	while (i) {
		seg[g][i] = 1e18;
		i /= 2;
	}
}
 
void Init(int N, int A[], int B[], int D[]) {
	n = N;
	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, 0, 0);
	while (TV < N)
		TV *= 2;
	for (int i = 0; i < 2 * TV; i++)
		seg[0][i] = seg[1][i] = 1e18;
	for (int i = 1; i < 20; i++) 
		for (int j = 0; j + (1 << i) < 2 * n; j++) 
			par[i][j] = min(par[i - 1][j], par[i - 1][j + (1 << (i - 1))]);
}
 
long long Query(int S, int X[], int T, int Y[]) {
	vector<pair<int, int>> sv;
	for (int i = 0; i < S; i++) {
		update(st[X[i]], dis[X[i]], 0);
		sv.push_back({st[X[i]], X[i]});
	}
	for (int i = 0; i < T; i++) {
		update(st[Y[i]], dis[Y[i]], 1);
		sv.push_back({st[Y[i]], Y[i]});
	}
	sort(sv.begin(), sv.end());
	long long ans = 1e18 ;
	for (int i = 0; i < int(sv.size()); i++) {
		int v = sv[i].second;
		ql = st[v];
		qr = fn[v];
		long long f = get(0);
		ql = st[v];
		qr = fn[v];
		f += get(1);
		f -= (2ll * dis[v]);
		ans = min(ans, f);
		if (i < int(sv.size()) - 1) {
			int vv = sv[i].second, uu = sv[i + 1].second;
			if (loc[vv] > loc[uu])
				swap(vv, uu);
			int SV = loc[vv], SU = loc[uu], LG = __lg(SU - SV + 1);
			pair<int, int> lca = min(par[LG][SV], par[LG][SU - (1 << LG) + 1]);
			v = lca.second;
			ql = st[v];
			qr = fn[v];
			f = get(0);
			ql = st[v];
			qr = fn[v];
			f += get(1);
			f -= (2ll * dis[v]);
	 		ans = min(ans, f);
		}
	}
	for (int i = 0; i < S; i++) 
		rem(st[X[i]], 0);
	for (int i = 0; i < T; i++)
		rem(st[Y[i]], 1);
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 22 ms 59740 KB Output is correct
2 Correct 557 ms 74720 KB Output is correct
3 Correct 671 ms 74684 KB Output is correct
4 Correct 635 ms 74784 KB Output is correct
5 Correct 806 ms 75144 KB Output is correct
6 Correct 507 ms 74676 KB Output is correct
7 Correct 664 ms 74720 KB Output is correct
8 Correct 667 ms 74580 KB Output is correct
9 Correct 821 ms 75148 KB Output is correct
10 Correct 497 ms 74840 KB Output is correct
11 Correct 659 ms 74832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 59996 KB Output is correct
2 Correct 1509 ms 246252 KB Output is correct
3 Correct 1832 ms 252364 KB Output is correct
4 Correct 1275 ms 244096 KB Output is correct
5 Correct 2045 ms 287520 KB Output is correct
6 Correct 1853 ms 252496 KB Output is correct
7 Correct 1250 ms 122492 KB Output is correct
8 Correct 859 ms 121076 KB Output is correct
9 Correct 1268 ms 126776 KB Output is correct
10 Correct 1306 ms 122964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 59740 KB Output is correct
2 Correct 557 ms 74720 KB Output is correct
3 Correct 671 ms 74684 KB Output is correct
4 Correct 635 ms 74784 KB Output is correct
5 Correct 806 ms 75144 KB Output is correct
6 Correct 507 ms 74676 KB Output is correct
7 Correct 664 ms 74720 KB Output is correct
8 Correct 667 ms 74580 KB Output is correct
9 Correct 821 ms 75148 KB Output is correct
10 Correct 497 ms 74840 KB Output is correct
11 Correct 659 ms 74832 KB Output is correct
12 Correct 12 ms 59996 KB Output is correct
13 Correct 1509 ms 246252 KB Output is correct
14 Correct 1832 ms 252364 KB Output is correct
15 Correct 1275 ms 244096 KB Output is correct
16 Correct 2045 ms 287520 KB Output is correct
17 Correct 1853 ms 252496 KB Output is correct
18 Correct 1250 ms 122492 KB Output is correct
19 Correct 859 ms 121076 KB Output is correct
20 Correct 1268 ms 126776 KB Output is correct
21 Correct 1306 ms 122964 KB Output is correct
22 Correct 2485 ms 247256 KB Output is correct
23 Correct 2380 ms 247436 KB Output is correct
24 Correct 2649 ms 252108 KB Output is correct
25 Correct 2593 ms 253080 KB Output is correct
26 Correct 3136 ms 250964 KB Output is correct
27 Correct 2953 ms 279700 KB Output is correct
28 Correct 2113 ms 245788 KB Output is correct
29 Correct 3157 ms 250528 KB Output is correct
30 Correct 2987 ms 249744 KB Output is correct
31 Correct 3096 ms 250592 KB Output is correct
32 Correct 1137 ms 128648 KB Output is correct
33 Correct 783 ms 122048 KB Output is correct
34 Correct 958 ms 121056 KB Output is correct
35 Correct 932 ms 120916 KB Output is correct
36 Correct 1303 ms 121964 KB Output is correct
37 Correct 1230 ms 121852 KB Output is correct