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