Submission #559447

# Submission time Handle Problem Language Result Execution time Memory
559447 2022-05-09T20:34:16 Z aryan12 Pictionary (COCI18_pictionary) C++17
98 / 140
150 ms 36948 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 5;
int dp[19][N];
int par[3 * N], values[3 * N];

int Find(int x)
{
	if(par[x] == x)
	{
		return x;
	}
	return par[x] = Find(par[x]);
}

void Unite(int a, int b, int pos)
{
	// cout << "Unite(" << a << ", " << b << ", " << pos << ")\n";
	values[pos] = a;
	a = Find(a), b = Find(b);
	// cout << "rep of a: " << a << ", rep of b: " << b << "\n";
	par[a] = par[b] = pos;
	dp[0][a] = pos;
	dp[0][b] = pos;
}

int LCA(int x, int y)
{
	int temp_x = x, temp_y = y;
	int depth_x = 0, depth_y = 0;
	for(int i = 18; i >= 0; i--)
	{
		if(dp[i][temp_x] != 0)
		{
			temp_x = dp[i][temp_x];
			depth_x += (1 << i);
		}
		if(dp[i][temp_y] != 0)
		{
			temp_y = dp[i][temp_y];
			depth_y += (1 << i);
		}
	}
	if(depth_y <= depth_x)
	{
		swap(depth_x, depth_y);
		swap(x, y);
	}
	int diff = depth_y - depth_x;
	for(int i = 18; i >= 0; i--)
	{
		if((1 << i) & diff)
		{
			y = dp[i][y];
		}
	}
	if(x == y)
	{
		return x;
	}
	for(int i = 18; i >= 0; i--)
	{
		if(dp[i][x] != dp[i][y])
		{
			x = dp[i][x];
			y = dp[i][y];
		}
	}
	return dp[0][x];
}

void Solve() 
{
	int n, m, q;
	cin >> n >> m >> q;
	for(int i = 0; i <= 3 * n; i++)
	{
		par[i] = i;
	}
	int pos = n + 1;
	for(int i = m; i > 0; i--)
	{
		for(int j = i * 2; j <= n; j += i)
		{
			if(Find(i) != Find(j))
			{
				// cout << "uniting: " << i << ", " << j << "\n";
				Unite(i, j, pos++);
			}
		}
		// for(int j = 1; j <= 2 * n; j++)
		// {
		// 	cout << Find(j) << " ";
		// }
		// cout << "\n";
	}
	// for(int i = 1; i <= 2 * n; i++)
	// {
	// 	cout << values[i] << " ";
	// }
	// cout << "\n";
	for(int i = 1; i <= 18; i++)
	{
		for(int j = 1; j <= 2 * n; j++)
		{
			dp[i][j] = dp[i - 1][dp[i - 1][j]];
		}
	}
	for(int i = 1; i <= q; i++)
	{
		int u, v;
		cin >> u >> v;
		// cout << "LCA(u, v) = " << LCA(u, v) << "\n";
		cout << m + 1 - values[LCA(u, v)] << "\n";
	}
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 932 KB Output is correct
2 Correct 45 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 1136 KB Output is correct
2 Correct 48 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 7924 KB Output is correct
2 Correct 76 ms 7888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 10948 KB Output is correct
2 Correct 145 ms 12252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 15696 KB Output is correct
2 Correct 120 ms 16740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 34 ms 34480 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 34 ms 35516 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 36948 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -