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;
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 |
---|
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... |