Submission #94214

# Submission time Handle Problem Language Result Execution time Memory
94214 2019-01-16T16:40:15 Z teomrn Pictionary (COCI18_pictionary) C++14
140 / 140
304 ms 24756 KB
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100010;
const int LGMAX = 20;

namespace UF {
	int tata[NMAX + 10], g[NMAX + 10];

	void init() {
		iota(tata, tata + NMAX, 0);
		fill(g, g + NMAX, 1);
	}

	int find(int n) {
		if (tata[tata[n]] != tata[n])
			tata[n] = find(tata[n]);
		return tata[n];
	}
	bool unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b)
			return 0;
		if (g[a] < g[b])
			swap(a, b);
		tata[b] = a, g[a] += g[b];
		return 1;
	}
}

namespace Arb {
	int h[NMAX + 10];
	int val[LGMAX + 1][NMAX + 10];
	int str[LGMAX + 1][NMAX + 10];
	vector <pair <int, int>> adia[NMAX + 10];

	void dfs(int nod, int t, int vnod) {
		h[nod] = 1 + h[t];
		val[0][nod] = vnod;
		str[0][nod] = t;
		for (int i = 1; i <= LGMAX; i++) {
			val[i][nod] = min(val[i - 1][nod], val[i - 1][str[i - 1][nod]]);
			str[i][nod] = str[i - 1][str[i - 1][nod]];
		}
		for (auto i : adia[nod])
			if (i.first != t)
				dfs(i.first, nod, i.second);
	}

	int lca(int a, int b) {
		if (h[a] < h[b])
			swap(a, b);
		int ans = 1e9;
		for (int i = LGMAX; i >= 0; i--)
			if (h[a] - (1 << i) >= h[b])
				ans = min(ans, val[i][a]), a = str[i][a];
		if (a == b)
			return ans;
		for (int i = LGMAX; i >= 0; i--)
			if (str[i][a] != str[i][b])
				ans = min(ans, min(val[i][a], val[i][b])), a = str[i][a], b = str[i][b];
		return min(ans, min(val[0][a], val[0][b]));
	}
}

int main()
{
	ios_base::sync_with_stdio(0);

	int n, m, q;
	cin >> n >> m >> q;

	UF::init();

	for (int i = m; i >= 1; i--) {
		for (int j = 2 * i; j <= n; j += i) {
			if (UF::unite(i, j)) {
				Arb::adia[j].push_back({ i, i });
				Arb::adia[i].push_back({ j, i });
			}
		}
	}

	assert(UF::g[UF::find(1)] == n);
	Arb::dfs(1, 0, 0);

	while (q--) {
		int a, b;
		cin >> a >> b;
		cout << m + 1 - Arb::lca(a, b) << '\n';
	}
		
	return 0;
}



# Verdict Execution time Memory Grader output
1 Correct 14 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 4088 KB Output is correct
2 Correct 120 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 4256 KB Output is correct
2 Correct 152 ms 4272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 8472 KB Output is correct
2 Correct 96 ms 8500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 10360 KB Output is correct
2 Correct 122 ms 11348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 13420 KB Output is correct
2 Correct 131 ms 13892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 16888 KB Output is correct
2 Correct 217 ms 18316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 20020 KB Output is correct
2 Correct 269 ms 22732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 24592 KB Output is correct
2 Correct 304 ms 24756 KB Output is correct