#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
struct DSU {
int n, comp;
vector<int> par, size;
void config(int _n) {
n = _n + 1;
comp = _n;
par.resize(n+1);
size.resize(n+1, 1);
for(int i=1; i<=n; i++) par[i] = i;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return false;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
comp--;
return true;
}
int getComp() { return comp; }
int getSize(int u) { return size[get(u)]; }
bool sameSet(int u, int v) { return get(u) == get(v); }
};
int32_t main() {
int n, m, q;
cin >> n >> m >> q;
vector<pii> qus(q);
for(pii &x : qus) cin >> x.first >> x.second;
vector<int> L(q, 1), R(q, m), ANS(q, m);
bool changed = 1;
stack<int> to_check[m+1];
while(changed) {
changed = 0;
for(int i=0; i<q; i++) {
if(L[i] <= R[i]) {
to_check[(L[i] + R[i]) / 2].push(i);
changed = 1;
}
}
DSU dsu; dsu.config(n);
int curr = m;
for(int i=1; i<=m; i++) {
for(int j=curr; j<=n; j+=curr)
if(j+curr <= n) dsu.uni(j, j+curr);
while(!to_check[i].empty()) {
int id = to_check[i].top();
to_check[i].pop();
if(dsu.sameSet(qus[id].first, qus[id].second)) ANS[id] = i, R[id] = i - 1;
else L[id] = i + 1;
}
curr--;
}
}
for(int &x : ANS) cout << x << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3268 KB |
Output is correct |
2 |
Correct |
35 ms |
3036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
4180 KB |
Output is correct |
2 |
Correct |
50 ms |
3684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
17388 KB |
Output is correct |
2 |
Correct |
51 ms |
8304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
15212 KB |
Output is correct |
2 |
Correct |
55 ms |
3344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
34068 KB |
Output is correct |
2 |
Correct |
81 ms |
15856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
4104 KB |
Output is correct |
2 |
Correct |
133 ms |
24928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
41992 KB |
Output is correct |
2 |
Correct |
153 ms |
19364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
75 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |