#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
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 |
1 |
Correct |
9 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
6836 KB |
Output is correct |
2 |
Correct |
61 ms |
6568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
8024 KB |
Output is correct |
2 |
Correct |
86 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
5368 KB |
Output is correct |
2 |
Correct |
103 ms |
5064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
5596 KB |
Output is correct |
2 |
Correct |
122 ms |
5540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
6988 KB |
Output is correct |
2 |
Correct |
173 ms |
5328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
6924 KB |
Output is correct |
2 |
Correct |
287 ms |
8756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
316 ms |
8240 KB |
Output is correct |
2 |
Correct |
338 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
8764 KB |
Output is correct |
2 |
Correct |
388 ms |
7980 KB |
Output is correct |