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>
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> ii;
using std::swap;
const int MAX_N = 100000;
#if 0
#include "../codes/uf-partial-1.cpp"
#include "../codes/uf-partial-2.cpp"
#else
int tempo;
int pai[MAX_N+1];
int tam[MAX_N+1];
int his[MAX_N+1];
void init() {
tempo = 0;
for (int i = 1; i <= MAX_N; ++i) {
pai[i] = i;
tam[i] = 1;
his[i] = 0;
}
}
int find(int x, int t) {
if (pai[x] == x) return x;
if (his[x] > t) return x;
return find(pai[x], t);
}
void merge(int u, int v) {
tempo += 1;
int a = find(u, tempo);
int b = find(v, tempo);
if (tam[a] > tam[b]) swap(a, b);
pai[a] = b;
his[a] = tempo;
tam[b] += tam[a];
}
#endif
bool same(int u, int v, int t) {
return find(u, t) == find(v, t);
}
inline int find_time(int a, int b) {
int ans = -1;
int l = 0, r = tempo;
while (l <= r) {
const int m = (l+r)/2;
if (same(a, b, m)) {
ans = m;
r = m-1;
} else {
l = m+1;
}
}
return ans;
}
int32_t main() {
using namespace std;
ios::sync_with_stdio(false);
cin.tie(0);
init();
int n, m, q;
cin>>n>>m>>q;
map<int, int> ans;
ans[tempo] = 0;
for (int day = m; day >= 1; --day) {
for (int i = 2; i*day <= n; ++i) {
if (!same(day, i*day, tempo))
merge(day, i*day);
else
tempo += 1;
ans[tempo] = m-day+1;
}
}
while (q--) {
int a, b;
cin>>a>>b;
int time = find_time(a, b);
cout<<ans[time]<<'\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... |