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;
struct dsu{
vector<int> pai, rank, tempo;
int n,t;
dsu(int a){
pai.assign(a,-1); rank.assign(a,0); tempo.assign(a,1e6);
n = a;
t=0;
}
int find(int a){
if(pai[a] == -1) return a;
return find(pai[a]);
}
void join(int a, int b){
a = find(a); b = find(b);
if(a == b) return;
if(rank[a] > rank[b]){pai[b] = a; tempo[b] = t;}
else if(rank[b] > rank[a]){pai[a] = b; tempo[a] = t;}
else{pai[b] = a; rank[a]++; tempo[b] = t;}
return;
}
int query(int a, int b){
int res = 0;
while(a != b){
if(tempo[a] < tempo[b]){res = max(res,tempo[a]); a = pai[a];}
else {res = max(res,tempo[b]); b = pai[b];}
if(a == b)return res;
}
return res;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,q; cin >> n >> m >> q;
dsu v(n);
for(int i=m; i>=1; i--){
v.t++;
for(int j = i+i; j<=n; j+=i) v.join(i-1, j-1);
}
while(q--){
int a,b; cin >> a >> b;
a--; b--;
cout << v.query(a,b) << '\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... |