Submission #94214

#TimeUsernameProblemLanguageResultExecution timeMemory
94214teomrnPictionary (COCI18_pictionary)C++14
140 / 140
304 ms24756 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...