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>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int MAXN = 3e5 + 10;
int n , m, q, pai[MAXN], peso[MAXN], sz[MAXN], k;
vector<pair<int,pii>> v;
int mdc(int a, int b){
if(b == 0) return a;
return mdc(b, a%b);
}
int find(int x){
if(x == pai[x]) return x;
k++;
return find(pai[x]);
}
void join(int x, int y, int z){
x = find(x);
y = find(y);
if(x == y) return;
if(sz[x] < sz[y]){
pai[x] = y;
peso[x] = z;
}else if(sz[y] < sz[x]){
pai[y] = x;
peso[y] = z;
}else{
pai[x] = y;
peso[x] = z;
sz[y]++;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// freopen("wormhole.in","r",stdin);
// freopen("wormhole.out","w",stdout);
cin >> n >> m >> q;
for(int i = 1; i <= n; i++){
pai[i] = i;
}
for(int i = m; i > 0; i--){
for(int j = i; j <= n; j += i){
join(i, j, m-i+1);
}
}
for(int i = 0; i < q; i++){
int x, y, a, b, ans = -1;
cin >> x >> y;
if(find(x) == find(y)){
ans = 0;
k = 0;
find(x);
a = k;
k = 0;
find(y);
b = k;
while(x != y){
if(a > b){
ans = max(peso[x], ans);
x = pai[x];
a--;
}else{
ans = max(peso[y], ans);
y = pai[y];
b--;
}
}
}
cout << ans << endl;
}
return 0;
}
# | 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... |