#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 20;
int n, m, k, pp[N], d[N];
int p[M + 1][N], dp[M + 1][N];
vector < pair < int, int > > g[N];
int dsu_get(int v){
return pp[v] == v ? v : pp[v] = dsu_get(pp[v]);
}
void dfs(int v, int pr = 0, int prval = 0){
p[0][v] = pr;
dp[0][v] = prval;
for(int i = 1; i <= M; i++){
p[i][v] = p[i - 1][p[i - 1][v]];
dp[i][v] = max(dp[i - 1][v], dp[i - 1][p[i - 1][v]]);
}
for(auto it : g[v]){
int to = it.first, len = it.second;
if(to != pr){
d[to] = d[v] + 1;
dfs(to, v, len);
}
}
}
inline int lca(int x, int y){
if(x == y){
return x;
}
if(d[x] > d[y]){
swap(x, y);
}
int len = d[y] - d[x];
for(int i = 0; i <= M; i++){
if((len >> i) & 1){
y = p[i][y];
}
}
if(x == y){
return x;
}
for(int i = M; i >= 0; i--){
if(p[i][x] != p[i][y]){
x = p[i][x];
y = p[i][y];
}
}
return p[0][x];
}
inline int get(int x, int y){
if(x == y){
return 0;
}
int ans = 0, len = d[y] - d[x];
for(int i = M; i >= 0; i--){
if((len >> i) & 1){
ans = max(ans, dp[i][y]);
y = p[i][y];
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m >> k;
for(int i = 1; i <= n; i++){
pp[i] = i;
}
for(int i = m; i >= 1; i--){
for(int j = 2; j * i <= n; j++){
if(dsu_get(i) != dsu_get(i * j)){
pp[dsu_get(i)] = dsu_get(i * j);
g[i].push_back(make_pair(i * j, m - i + 1));
g[i * j].push_back(make_pair(i, m - i + 1));
}
}
}
dfs(1);
while(k--){
int x, y;
cin >> x >> y;
int z = lca(x, y);
cout << max(get(z, x), get(z, y)) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
3832 KB |
Output is correct |
2 |
Correct |
41 ms |
3964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
4344 KB |
Output is correct |
2 |
Correct |
73 ms |
4260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
8284 KB |
Output is correct |
2 |
Correct |
80 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
10336 KB |
Output is correct |
2 |
Correct |
132 ms |
11396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
13608 KB |
Output is correct |
2 |
Correct |
184 ms |
13964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
293 ms |
17528 KB |
Output is correct |
2 |
Correct |
296 ms |
18948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
20768 KB |
Output is correct |
2 |
Correct |
382 ms |
23348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
469 ms |
25644 KB |
Output is correct |
2 |
Correct |
433 ms |
25544 KB |
Output is correct |