#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], pom2[N];
pair<lli, lli > q[N];
vector<lli>v;
int ufind (int x) {
if (u[x]==x) return x;
u[x] = ufind(u[x]);
return u[x];
}
void umerge (int x, int y) {
x = ufind(x);
y = ufind(y);
if (x==y) return;
if (rand()%2) {
u[x]=y;
}
else {
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;*/
vector<lli>pom;
for (int i = 1; i <= n; i++) pom.push_back(u[i]);
if (l==r) {
for (int i = 0; i < Qpos.size(); i++) {
sol[Qpos[i]]=l;
}
return;
}
if (Qpos.empty()) return;
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);
for (int i = 0; i < pom.size(); i++) u[i+1]=pom[i];
//cout << "idem lijevo: \n";
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:42:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < Qpos.size(); i++) {
| ~~^~~~~~~~~~~~~
pictionary.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < Qpos.size(); i++) {
| ~~^~~~~~~~~~~~~
pictionary.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i < pom.size(); i++) u[i+1]=pom[i];
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
10868 KB |
Output is correct |
2 |
Correct |
31 ms |
7912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
14352 KB |
Output is correct |
2 |
Correct |
45 ms |
11652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1549 ms |
10776 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1597 ms |
11648 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1586 ms |
17956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
12388 KB |
Output is correct |
2 |
Execution timed out |
1593 ms |
22688 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1583 ms |
26692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1584 ms |
33300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |