#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
#define pb push_back
const int N = 1e5+1;
vi dad(N),change(N),sz(N,1);
int find(int x,int t) {
if (x == dad[x] || change[x] > t) return x;
return find(dad[x],t);
}
void unite(int x,int y,int t) {
int a = find(x,t),b = find(y,t);
if (a == b) return;
if (sz[a] > sz[b]) swap(a,b);
sz[b]+=sz[a];
dad[a] = b;
change[a] = t;
}
void solve() {
int n,m,q;
cin >> n >> m >> q;
F(i,n) dad[i] = i;
for (int i=m;i>=1;i--) {
for (int j=i;j+i<=n;j+=i) unite(j,j+i,m-i+1);
}
while (q--) {
int a,b;
cin >> a >> b;
int l = 1;
int r = m;
while (l<=r) {
int mm = (l+r) >>1;
if (find(a,mm) == find(b,mm)) r = mm-1;
else l = mm+1;
}
cout << l << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
3412 KB |
Output is correct |
2 |
Correct |
94 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
3924 KB |
Output is correct |
2 |
Correct |
117 ms |
3744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
3556 KB |
Output is correct |
2 |
Correct |
73 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
3540 KB |
Output is correct |
2 |
Correct |
74 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
3852 KB |
Output is correct |
2 |
Correct |
102 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
3668 KB |
Output is correct |
2 |
Correct |
129 ms |
3872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
4096 KB |
Output is correct |
2 |
Correct |
143 ms |
4064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
3920 KB |
Output is correct |
2 |
Correct |
160 ms |
3920 KB |
Output is correct |