Submission #83442

#TimeUsernameProblemLanguageResultExecution timeMemory
83442luciocfPictionary (COCI18_pictionary)C++14
140 / 140
106 ms27796 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; const int maxl = 15; int pai[maxn], sz[maxn], edge[maxn], nivel[maxn]; int tab[2][maxn][maxl], n; int Find(int x) { if (pai[x] == x) return x; return Find(pai[x]); } void Join(int x, int y, int tt) { x = Find(x), y = Find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y], pai[y] = x, edge[y] = tt; } void init(int n) { for (int i = 1; i <= n; i++) pai[i] = i, sz[i] = 1; } int dfs(int u) { if (u == Find(1)) return 0; return nivel[u] = dfs(pai[u])+1; } void build(void) { memset(tab, -1, sizeof tab); for (int i = 1; i <= n; i++) tab[0][i][0] = pai[i], tab[1][i][0] = edge[i]; for (int j = 1; j < maxl; j++) for (int i = 1; i <= n; i++) if (tab[0][i][j-1] != -1) tab[0][i][j] = tab[0][tab[0][i][j-1]][j-1], tab[1][i][j] = max(tab[1][i][j-1], tab[1][tab[0][i][j-1]][j-1]); } int lca(int u, int v) { int ans = -1; if (nivel[u] < nivel[v]) swap(u, v); for (int i = maxl-1; i >= 0; i--) if (nivel[u]-(1<<i) >= nivel[v]) ans = max(ans, tab[1][u][i]), u = tab[0][u][i]; if (u == v) return ans; for (int i = maxl-1; i >= 0; i--) if (tab[0][u][i] != -1 && tab[0][v][i] != -1 & tab[0][u][i] != tab[0][v][i]) ans = max({ans, tab[1][u][i], tab[1][v][i]}), u = tab[0][u][i], v = tab[0][v][i]; return max({ans, edge[u], edge[v]}); } int main(void) { ios::sync_with_stdio(false); cin.tie(0); int m, q; cin >> n >> m >> q; init(n); for (int i = m; i >= 1; i--) for (int j = i; j <= n; j += i) Join(j, i, m-i+1); for (int i = 1; i <= n; i++) dfs(i); build(); for (int i = 1; i <= q; i++) { int u, v; cin >> u >> v; cout << lca(u, v) << "\n"; } }

Compilation message (stderr)

pictionary.cpp: In function 'int lca(int, int)':
pictionary.cpp:67:42: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   if (tab[0][u][i] != -1 && tab[0][v][i] != -1 & tab[0][u][i] != tab[0][v][i])
                             ~~~~~~~~~~~~~^~~~~
#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...