/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 100005
#define maxm 500005
#define pii pair <int,int>
#define Task "Pictionary"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,q,root[maxn],cnt,h[maxn];
pii par[maxn][17];
vector <pii> a[maxn];
int Get_root(int x)
{
return root[x] ? root[x] = Get_root(root[x]) : x;
}
void Prepare()
{
FOR(i,0,m-1)
{
int cur = m - i;
for (int j = 2 * cur; j <= n; j += cur)
{
int u = Get_root(cur),
v = Get_root(j);
if (u == v) continue;
root[u] = v;
a[u].pb(mp(v,i));
a[v].pb(mp(u,i));
++cnt;
if (cnt == n - 1) break;
}
if (cnt == n - 1) break;
}
}
void Dfs(int u)
{
for (auto v : a[u])
if (par[u][0].F != v.F)
{
h[v.F] = h[u] + 1;
par[v.F][0] = mp(u,v.S);
FOR(i,1,16)
{
par[v.F][i].F = par[par[v.F][i-1].F][i-1].F;
par[v.F][i].S = max(par[v.F][i-1].S,par[par[v.F][i-1].F][i-1].S);
}
Dfs(v.F);
}
}
int Lca(int u,int v)
{
int res = -maxc;
if (h[u] < h[v]) swap(u,v);
int dis = h[u] - h[v];
FOR(i,0,16)
if ((dis >> i) & 1)
{
res = max(res,par[u][i].S);
u = par[u][i].F;
}
if (u == v) return res + 1;
FORD(i,16,0)
if (par[u][i].F != par[v][i].F)
{
res = max(res,par[u][i].S);
res = max(res,par[v][i].S);
u = par[u][i].F;
v = par[v][i].F;
}
res = max(res,par[u][0].S);
res = max(res,par[v][0].S);
return res + 1;
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
read(n),read(m),read(q);
Prepare();
Dfs(1);
while (q--)
{
int x,y;
read(x),read(y);
writeln(Lca(x,y));
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
4136 KB |
Output is correct |
2 |
Correct |
21 ms |
4660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
5780 KB |
Output is correct |
2 |
Correct |
29 ms |
6528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
10916 KB |
Output is correct |
2 |
Correct |
32 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
13656 KB |
Output is correct |
2 |
Correct |
53 ms |
15304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
17700 KB |
Output is correct |
2 |
Correct |
59 ms |
18756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
24084 KB |
Output is correct |
2 |
Correct |
94 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
27400 KB |
Output is correct |
2 |
Correct |
117 ms |
30348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
33876 KB |
Output is correct |
2 |
Correct |
140 ms |
34820 KB |
Output is correct |