#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 time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
122 ms |
1480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
176 ms |
1896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
88 ms |
5416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
7096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
158 ms |
10056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
13156 KB |
Output is correct |
2 |
Incorrect |
201 ms |
14744 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
206 ms |
16332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
258 ms |
20716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |