This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
using namespace std;
int n, m, q;
int anc[100010];
int tim[100010];
int w[100010];
int find(int x, int t){
if(anc[x] == x) return x;
if(tim[x] > t) return x;
return find(anc[x], t);
}
void join(int x, int y, int t){
x = find(x, t);
y = find(y, t);
if(x == y) return;
if(w[x] < w[y]) swap(x, y);
w[x] += w[y];
anc[y] = x;
tim[y] = t;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> q;
for(int i=1; i<=n; i++){
anc[i] = i;
w[i] = 1;
}
for(int i=m; i>=1; i--){
for(int j=2*i; j<=n; j+=i){
join(i, j, m-(i-1));
}
}
for(int i=0; i<q; i++){
int a, b;
cin >> a >> b;
int l = 1, r = m;
while(l < r){
int mid = (l+r)/2;
if(find(a, mid) == find(b, mid)){
r = mid;
} else {
l = mid+1;
}
}
cout << r << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |