#include <bits/stdc++.h>
#define div divisores
using namespace std;
const int maxn = 1e5 + 10;
int pai[maxn], peso[maxn], ans[maxn];
int n, m, q;
int gcd(int a, int b){
return b ? gcd(b, a%b) : a;
}
struct UnionFind{
vector<int> pai, peso;
int n;
UnionFind(int n=0): n(n){
init(n);
}
void init(int n){
pai.resize(n+1);
peso.resize(n+1);
for(int i=1;i<=n;i++)
pai[i] = i, peso[i] = 0;
}
int find(int u){
return (pai[u] == u ? u : pai[u] = find(pai[u]));
}
inline void join(int a, int b){
a = find(a);
b = find(b);
if(peso[a] == peso[b]) peso[b]++;
else if(peso[a] > peso[b]) swap(a, b);
pai[a] = b;
}
};
struct query{
int l, r, id;
};
vector<query> qq;
void solve(vector<query>& queries, UnionFind u, int ini, int fim){
int meio = (ini + fim) >> 1;
UnionFind u2 = u;
for(int dia=ini;dia<=meio;dia++){
int k = m - dia + 1;
for(int i=2;i*k<=n;i++){
//cout << "INICIO\n";
// /cout << k << endl;
//cout << "FIM\n";
if(u.find(k) != u.find(i*k)) u.join(k, i*k);
}
}
vector<query> l, r;
for(auto& q : queries){
if(u.find(q.l) == u.find(q.r)) ans[q.id] = min(ans[q.id], fim), l.push_back(q);
else r.push_back(q);
}
if(ini == fim) return;
solve(l, u2, ini, meio);
solve(r, u, meio+1, fim);
}
int main(){
//ios::sync_with_stdio(false), cin.tie(0);
memset(ans, 0x3f, sizeof ans);
cin >> n >> m >> q;
for(int i=0;i<q;i++){
int a, b;
cin >> a >> b;
qq.push_back({a, b, i});
}
// for(int k=m;k>=1;k--){
// for(int i=2;i*k<=n;i++){
// if(find(k) != find(i*k)) join(k, i*k);
// }
// for(int l=1;l<=q;l++){
// if(marcado[l]) continue;
// if(find(qq[l].first) == find(qq[l].second)) ans[l] = m - k + 1, marcado[l] = true;
// }
// }
solve(qq, UnionFind{n}, 1, m);
for(int i=0;i<q;i++)
cout << ans[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
2632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
8832 KB |
Output is correct |
2 |
Correct |
67 ms |
8832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
12788 KB |
Output is correct |
2 |
Correct |
82 ms |
12788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1563 ms |
12788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1566 ms |
12788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1567 ms |
17256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
17256 KB |
Output is correct |
2 |
Execution timed out |
1567 ms |
23516 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
26748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1557 ms |
34044 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |