#include <bits/stdc++.h>
using namespace std;
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;
/////////////////////////////////////////////////////// BEGIN OF UNION-FIND WITH PARTIAL PERSISTENCY ///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//////////////// Note: this class supports both 0-indexed and 1-indexed elements ////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
class UnionFindPartial {
private:
int m_current_time;
std::vector<int> m_parent; // [0 .. size]
std::vector<int> m_time; // [0 .. size]
std::vector<int> m_size; // [0 .. size]
public:
UnionFindPartial(const int size)
: m_current_time(0)
, m_parent(size+1)
, m_time(size+1, 0)
, m_size(size+1, 1)
{
for (int i = 0; i <= size; ++i)
m_parent[i] = i;
}
inline int time() const {
return m_current_time;
}
inline int find(int x, const int last_time) const {
while (m_time[x] <= last_time && x != m_parent[x])
x = m_parent[x];
return x;
}
inline int find(const int x) const {
return find(x, time());
}
void merge(int a, int b) {
a = find(a);
b = find(b);
m_current_time += 1;
if (a != b) {
if (m_size[a] < m_size[b]) {
m_parent[a] = b;
m_time[a] = m_current_time;
m_size[b] += m_size[a];
} else {
m_parent[b] = a;
m_time[b] = m_current_time;
m_size[a] += m_size[b];
}
}
}
bool same(const int a, const int b) {
return find(a) == find(b);
}
bool same(const int a, const int b, const int last_time) {
return find(a, last_time) == find(b, last_time);
}
inline int find_time(int a, int b) {
int ans = -1;
int l = 0, r = time();
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;
}
int size(const int a) {
const int gpa = find(a);
return m_size[gpa];
}
};
/////////////////////////////////////////////////////// END OF UNION-FIND WITH PARTIAL PERSISTENCY ///////////////////////////////////////////////////////
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin>>n>>m>>q;
UnionFindPartial uf(n);
map<int, int> ans;
ans[uf.time()] = 0;
for (int day = m; day >= 1; --day) {
for (int i = 2; i*day <= n; ++i) {
uf.merge(day, i*day);
ans[uf.time()] = m-day+1;
}
}
while (q--) {
int a, b;
cin>>a>>b;
int time = uf.find_time(a, b);
cout<<ans[time]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
760 KB |
Output is correct |
2 |
Correct |
36 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
1016 KB |
Output is correct |
2 |
Correct |
50 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
10524 KB |
Output is correct |
2 |
Correct |
115 ms |
10204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
15096 KB |
Output is correct |
2 |
Correct |
123 ms |
13532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
22500 KB |
Output is correct |
2 |
Correct |
197 ms |
23960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
16864 KB |
Output is correct |
2 |
Correct |
333 ms |
35268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
39852 KB |
Output is correct |
2 |
Correct |
401 ms |
44612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
487 ms |
52216 KB |
Output is correct |
2 |
Correct |
475 ms |
50992 KB |
Output is correct |