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;
#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 (stderr)
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) {
| ~~~~~~~~~~~^~~~~~
# | 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... |