#include<bits/stdc++.h>
#define lli long long
#define endl "\n"
using namespace std;
const lli MAXN=100005, LOGN=25, INF=999999999;
lli N, M, Q, par[MAXN], sz[MAXN], dep[MAXN], ti[MAXN], up[LOGN+1][MAXN];
lli koren, maxx[LOGN+1][MAXN], a, b;
vector<lli> v[MAXN];
void add_node(lli x){
par[x]=x;
sz[x]=1;
ti[x]=x;
return;
}
lli find_parent(lli x){
if(par[x]==x)return x;
return find_parent(par[x]);
}
void merge_v(lli x, lli y){
lli rootx=find_parent(x), rooty=find_parent(y);
if(rootx==rooty)return;
if(sz[rootx]<=sz[rooty]){
par[rootx]=rooty;
sz[rooty]+=sz[rootx];
ti[rootx]=min(x, y);
}else{
par[rooty]=rootx;
sz[rootx]+=sz[rooty];
ti[rooty]=min(x, y);
}
return;
}
void dfs(lli x){
for(lli i=0; i<v[x].size(); i++){
dep[v[x][i]]=dep[x]+1;
dfs(v[x][i]);
}
return;
}
lli lca(lli x, lli y){
if(dep[x]<dep[y])swap(x, y);
lli ans=INF;
for(lli i=LOGN; i>=0; i--){
if(dep[x]-(1<<i)>=dep[y]){
ans=min(ans, maxx[i][x]);
x=up[i][x];
}
}
if(x==y)return ans;
for(lli i=LOGN; i>=0; i--){
if(up[i][x]!=up[i][y]){
ans=min(ans, maxx[i][x]);
ans=min(ans, maxx[i][y]);
x=up[i][x];
y=up[i][y];
}
}
ans=min(ans, maxx[0][x]);
ans=min(ans, maxx[0][y]);
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>N>>M>>Q;
for(lli i=1; i<=N; i++)add_node(i);
for(lli i=M; i>0; i--){
for(lli j=2*i; j<=N; j+=i){
merge_v(i, j);
}
}
for(lli i=1; i<=N; i++){
up[0][i]=par[i];
maxx[0][i]=ti[i];
if(par[i]==i)koren=i;
else v[par[i]].push_back(i);
}
dep[koren]=1;
dfs(koren);
for(lli st=1; st<=LOGN; st++){
for(lli i=1; i<=N; i++){
up[st][i]=up[st-1][up[st-1][i]];
maxx[st][i]=min(maxx[st-1][i], maxx[st-1][maxx[st-1][i]]);
}
}
for(lli q=0; q<Q; q++){
cin>>a>>b;
cout<<M-lca(a, b)+1<<endl;
}
return 0;
}
Compilation message
pictionary.cpp: In function 'void dfs(long long int)':
pictionary.cpp:34:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(lli i=0; i<v[x].size(); i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
3328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
4276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
4624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
13828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
17996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
24828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
31856 KB |
Output is correct |
2 |
Incorrect |
87 ms |
35916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
39956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
125 ms |
49868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |