#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int L = 20;
struct dsu {
int lab[N];
dsu() {
memset(lab, -1, sizeof(lab));
}
void reset() {
memset(lab, -1, sizeof(lab));
}
int find(int u) {
return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
bool merge(int u, int v) {
if ((u = find(u)) == (v = find(v))) {
return false;
}
if (lab[u] > lab[v]) {
swap(u, v);
}
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
int n, m, q;
int a[N], b[N];
int l[N], r[N];
vector<int> que[N];
int ans[N];
dsu g;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= q; i++) {
cin >> a[i] >> b[i];
l[i] = 0, r[i] = m;
}
for (int ite = 1; ite <= L; ite++) {
for (int i = 1; i <= q; i++) {
if (l[i] > r[i]) {
continue;
}
que[(l[i] + r[i]) >> 1].emplace_back(i);
}
for (int i = 0; i <= m; i++) {
if (i != 0) {
for (int j = m - i + 1; j + (m - i + 1) <= n; j += (m - i + 1)) {
g.merge(j, j + (m - i + 1));
}
}
for (auto x : que[i]) {
if (g.find(a[x]) == g.find(b[x])) {
r[x] = i - 1;
ans[x] = i;
} else {
l[x] = i + 1;
}
}
que[i].clear();
}
g.reset();
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
8160 KB |
Output is correct |
2 |
Correct |
18 ms |
7376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
10212 KB |
Output is correct |
2 |
Correct |
26 ms |
9000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
8220 KB |
Output is correct |
2 |
Correct |
42 ms |
7792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
8772 KB |
Output is correct |
2 |
Correct |
39 ms |
7560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
11000 KB |
Output is correct |
2 |
Correct |
62 ms |
8336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
9296 KB |
Output is correct |
2 |
Correct |
110 ms |
12304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
14224 KB |
Output is correct |
2 |
Correct |
122 ms |
13012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
16212 KB |
Output is correct |
2 |
Correct |
137 ms |
14572 KB |
Output is correct |