#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
template <int maxn>
struct Dsu {
int n;
int pai[maxn], w[maxn];
int find(int x)
{
if (x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
bool find(int a, int b)
{
a = find(a), b= find(b);
return a == b;
}
void join(int a, int b)
{
a = find(a), b = find(b);
if (w[a] < w[b]) swap(a, b);
pai[b] = a;
w[a] += w[b];
}
void init(int n_) {
n = n_;
fill(w+1, w+n+1, 1);
iota(pai+1, pai+n+1, 1);
}
};
struct Query
{
int ff, ss, id;
};
int const maxn = 101010;
Dsu<maxn> dsu;
vector<Query> qr[maxn];
vector<Query> nqr[maxn];
int res[maxn];
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
int q;
cin >> n >> m >> q;
dsu.init(n);
queue<ii> qe, nq;
qe.push({1, m+1});
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
qr[1].push_back({a, b, i});
}
int lk = m+1;
while (!qe.empty()) {
int l = qe.front().first, r = qe.front().second;
int k = (l+r)/2;
qe.pop();
for (; lk > k;) {
lk--;
for (int j = lk+lk; j <= n; j += lk) {
if (dsu.find(lk) != dsu.find(j)) {
dsu.join(lk, j);
}
}
}
// cout << l << " " << qr[l].size() << "\n";
if (l+1 < r) {
bool isl = false, isr = false;
for (auto p : qr[l]) {
if (!dsu.find(p.ff, p.ss)) {
if (!isl) isl = true, nq.push({l, k});
nqr[l].push_back(p);
}
else {
if (!isr) isr = true, nq.push({k, r});
nqr[k].push_back(p);
}
}
qr[l].clear();
}
else {
for (auto p : qr[l]) {
res[p.id] = l;
}
}
if (qe.empty()) {
swap(qe, nq), swap(qr, nqr), lk = m+1, dsu.init(n);
vector<ii> v;
while (!qe.empty()) {
v.push_back(qe.front());
qe.pop();
}
sort(v.begin(), v.end(), greater<ii>{});
for (ii a : v) qe.push(a);
}
}
for (int i = 0; i < q; i++) cout << m-res[i]+1 << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
6656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
10060 KB |
Output is correct |
2 |
Correct |
38 ms |
10144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
12972 KB |
Output is correct |
2 |
Correct |
49 ms |
12972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
12972 KB |
Output is correct |
2 |
Correct |
61 ms |
12972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
14080 KB |
Output is correct |
2 |
Correct |
58 ms |
14080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
18128 KB |
Output is correct |
2 |
Correct |
94 ms |
18128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
18128 KB |
Output is correct |
2 |
Correct |
144 ms |
21868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
24544 KB |
Output is correct |
2 |
Correct |
178 ms |
24544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
28464 KB |
Output is correct |
2 |
Correct |
205 ms |
28464 KB |
Output is correct |