Submission #575542

#TimeUsernameProblemLanguageResultExecution timeMemory
575542PietraPictionary (COCI18_pictionary)C++14
0 / 140
258 ms20716 KiB
#include<bits/stdc++.h> using namespace std ; const int maxl = 22 ; const int maxn = 2e5 + 5 ; int n, m, q ; int tab[maxl][maxn], mx[maxl][maxn], nivel[maxn] ; int sz[maxn], pai[maxn] ; struct DSU{ int find(int a){return (pai[a] == a ? a : pai[a] = find(pai[a])) ; } void join(int a, int b, int id){ a = find(a), b = find(b) ; if(sz[a] > sz[b]) swap(a, b) ; tab[0][a] = b ; mx[0][a] = id ; nivel[a] = nivel[b] + 1 ; sz[b] += sz[a] ; pai[a] = b ; } } dsu ; int lca(int a, int b){ int ans = 0 ; if(nivel[a] > nivel[b]) swap(a, b) ; for(int i = maxl - 1 ; i >= 0 ; i--){ if(tab[i][b] != -1 && nivel[tab[i][b]] >= nivel[a]){ ans = max(ans, mx[i][b]) ; b = tab[i][b] ; } } if(a == b) return ans ; for(int i = maxl - 1 ; i >= 0 ; i--){ if(tab[i][a] != -1 && tab[i][b] != -1 && tab[i][a] != tab[i][b]){ ans = max({mx[i][a], ans, mx[i][b]}) ; b = tab[i][b], a = tab[i][a] ; } } return max({ans, mx[0][a], mx[0][b]}) ; } int main(){ cin >> n >> m >> q ; for(int i = 1 ; i <= n ; i++) pai[i] = i, sz[i] = 1 ; for(int i = 1 ; i < maxl ; i++){ for(int j = 1 ; j <= n ; j++){ tab[i][j] = -1 ; } } for(int md = m ; md > 0 ; md--){ for(int id = md + md ; id <= n ; id += md){ if(dsu.find(md) == dsu.find(id)) continue ; dsu.join(md, id, m - md + 1) ; } } for(int i = 1 ; i < maxl ; i++){ for(int j = 1 ; j <= n ; j++){ if(tab[i-1][j] == -1) continue ; mx[i][j] = max(mx[i-1][j], mx[i-1][tab[i-1][j]]) ; tab[i][j] = tab[i-1][tab[i-1][j]] ; } } for(int i = 1 ; i <= q ; i++){ int a, b ; cin >> a >> b ; cout << lca(a, b) << "\n" ; } }
#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...