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>
#define breturn return
using namespace std;
int n, m, q, a, b, par[100001], ans[100001];
vector<pair<int, pair<int, int> > > mid[100001];
vector<pair<int, int> > quer;
bool done;
int gp(int x) {
if(x == par[x]) breturn x;
else breturn par[x] = gp(par[x]);
}
void con(int x, int y) {
int xx = gp(x), yy = gp(y);
if(xx == yy) breturn;
// cout << "connecting " << x << " and " << y << '\n';
par[xx] = yy;
}
int main() {
cin >> n >> m >> q;
for(int i = 0; i < q; i++) cin >> a >> b, quer.push_back({a, b}), mid[(m + 1)/2].push_back({i, {1, m}});
while(!done) {
done = true;
for(int i = 1; i <= n; i++) par[i] = i;
for(int i = 1; i <= m; i++) {
int ad = m - i + 1;
int las = ad;
int gc = las + ad;
while(gc <= n) {
con(las, gc);
las = gc;
gc += ad;
}
for(int j = 0; j < mid[i].size(); j++) {
int y = mid[i][j].first;
int l = mid[i][j].second.first;
int r = mid[i][j].second.second;
int midl = (l + r)/2;
/* cout << y << " " << l << " " << r << " " << midl << '\n';
cout << quer[y].first << " < - > " << quer[y].second << '\n';
cout << gp(quer[y].first) << " . . . " << gp(quer[y].second) << '\n';
*/
if(gp(quer[y].first) != gp(quer[y].second)) {
l = midl + 1;
} else {
r = midl;
}
midl = (l + r)/2;
if(l == r) ans[mid[i][j].first] = midl;
else {
done = false;
mid[midl].push_back({mid[i][j].first, {l, r}});
}
}
mid[i].clear();
mid[i].shrink_to_fit();
}
}
for(int i = 0; i < q; i++) cout << ans[i] << '\n';
}
Compilation message (stderr)
pictionary.cpp: In function 'int main()':
pictionary.cpp:37:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j = 0; j < mid[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# | 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... |