#include <bits/stdc++.h>
using namespace std;
#define lli long long int
const int N = 100100;
int t, n, m, Q;
lli u[N], sol[N];
pair<lli, lli > q[N];
vector<lli>v;
vector<pair<int, int> > pom;
int ufind (int x) {
if (u[x]==x) return x;
lli sad = ufind(u[x]);
if (sad != u[x]) {
pom.push_back(make_pair(x, u[x]));
}
u[x]=sad;
return u[x];
}
void umerge (int x, int y) {
x = ufind(x);
y = ufind(y);
if (x==y) return;
if (rand()%2) {
pom.push_back(make_pair(x, u[x]));
u[x]=y;
}
else {
pom.push_back(make_pair(y, u[y]));
u[y]=x;
}
return;
}
int br=0;
void f (lli l, lli r, vector<lli>Qpos) {;
/*cout << br << endl;
br++;*/
//cout << "da\n";
//cout << l << " " << (l+r)/2 << " " << r << " " << Qpos.size() << endl;
/*for (int i = 0; i < Qpos.size(); i++) {
cout << Qpos[i] << " ";
}
cout << endl;*/
if (l==r) {
for (int i = 0; i < Qpos.size(); i++) {
sol[Qpos[i]]=l;
}
return;
}
if (Qpos.empty()) return;
int last = pom.size();
int mid = (l+r)/2;
for (int i = l; i <= mid; i++) {
int curr = m-i+1, foll = curr*2;
while (foll <= n) {
umerge (curr, foll);
foll+=curr;
}
}
//for (int i = 1; i <= n; i++) cout << i << ": " << ufind(i) << ", " << pom[i] << endl;
//system("pause");
vector<lli>left;
vector<lli>right;
for (int i = 0; i < Qpos.size(); i++) {
int a1=q[Qpos[i]].first, a2=q[Qpos[i]].second;
if (ufind(a1)==ufind(a2)) {
left.push_back(Qpos[i]);
}
else {
right.push_back(Qpos[i]);
}
}
/*cout << "right:";
for (int i = 0; i < right.size(); i++) cout << right[i] << " ";
cout << "\n";
cout << "idem desno: \n";*/
f (mid+1, r, right);
//cout << "idem lijevo: \n";
while (pom.size() > last) {
int k = pom.size()-1;
u[pom[k].first] = pom[k].second;
pom.pop_back();
}
f (l, mid, left);
return;
}
void solve () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> Q;
for (int i = 1; i <= Q; i++) {
cin >> q[i].first >> q[i].second;
v.push_back(i);
}
for (int i = 1; i <= n; i++) {
u[i]=i;
}
f (1, m, v);
for (int i = 1; i <= Q; i++) cout << sol[i] << "\n";
return;
}
int main () {
solve();
return 0;
}
/*
25 6 1
25 10
*/
Compilation message
pictionary.cpp: In function 'void f(long long int, long long int, std::vector<long long int>)':
pictionary.cpp:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < Qpos.size(); i++) {
| ~~^~~~~~~~~~~~~
pictionary.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < Qpos.size(); i++) {
| ~~^~~~~~~~~~~~~
pictionary.cpp:85:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
85 | while (pom.size() > last) {
| ~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
10484 KB |
Output is correct |
2 |
Correct |
33 ms |
7528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
13932 KB |
Output is correct |
2 |
Correct |
51 ms |
11288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
10728 KB |
Output is correct |
2 |
Correct |
80 ms |
10360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
10208 KB |
Output is correct |
2 |
Correct |
92 ms |
9824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
17900 KB |
Output is correct |
2 |
Correct |
152 ms |
14432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
14560 KB |
Output is correct |
2 |
Correct |
245 ms |
18652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
28816 KB |
Output is correct |
2 |
Correct |
323 ms |
27220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
30944 KB |
Output is correct |
2 |
Correct |
364 ms |
28244 KB |
Output is correct |