#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});
}
cout << "\n";
int lk = m+1;
while (!qe.empty()) {
int l = qe.front().first, r = qe.front().second;
int k = (l+r+1)/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);
}
}
for (int i = 0; i < q; i++) cout << m-res[i]+1 << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
5628 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
6788 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
10480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
12948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
12948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
13656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
17032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
17032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
22844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
159 ms |
27044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |