//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define int long long
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define debug(x) cerr<<x<<" ";
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define sz(s) s.size()
#define pii pair<int,int>
#define pb push_back
#define checktime cout << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define reb(i,m,n) for(int i=m;i>=n;i--)
#define ALL(v) v.begin(), v.end()
#define base 31
#define base2 37
using namespace std;
void phongbeo();
void de();
template<typename A, typename B>istream& operator>>(istream& in, pair<A, B> &v)
{
in >> v.first >> v.second;
return in;
}
template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B> &v)
{
out << v.first << " " << v.second << " ";
return out;
}
template < class T > bool minimize(T& a, const T& b)
{
return b < a ? a = b, 1 : 0;
}
template < class T > bool maximize(T& a, const T& b)
{
return a < b ? a = b, 1 : 0;
}
mt19937 rd(chrono::high_resolution_clock::now().time_since_epoch().count ());
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy2[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int mod2=1e9+7;
const int mod=1e9+7;
const int mod1=998244353;
struct ib
{
int a;
int b;
};
struct icd
{
int a,b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
int minn=1e12+1,maxx=-1e9;
int n,m,s2,s4,s3,sf,k,r,dem=0,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,l;
int s,i,s10,s12;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
//fin(task),fou(task);
#endif
NHP
//modwwe
phongbeo(),down
#ifndef ONLINE_JUDGE
//checktime
#endif
}
ib dsu[100001];
ib st[17][100001];
int depth[100001];
vector<ib> v[100001];
void reset(int x)
{
dsu[x]={1,x};
}
int tim(int x)
{
if(dsu[x].b==x) return x;
dsu[x].b=tim(dsu[x].b);
return dsu[x].b;
}
void noi(int x,int y)
{
if(dsu[x].a<dsu[y].a) swap(x,y);
dsu[x].a+=dsu[y].a;
dsu[y].b=x;
}
void dfs(int x,int y,int z)
{
st[0][x].a=y;
st[0][x].b=z;
depth[x]=depth[y]+1;
for(auto f:v[x])
if(f.a!=y)
dfs(f.a,x,f.b);
}
int lca(int x,int y)
{
if(depth[x]>depth[y]) swap(x,y);
int ss=depth[y]-depth[x];
for(int i=0;i<17;i++)
if(bit(ss,i))
y=st[i][y].a;
if(y==x) return x;
for(int i=16;i>=0;--i)
if(st[i][x].a!=st[i][y].a)
x=st[i][x].a,y=st[i][y].a;
return st[0][x].a;
}
int get(int x,int y)
{int ss=1e18;
for(int i=0;i<16;i++)
if(bit(y,i))
ss=min(st[i][x].b,ss),x=st[i][x].a;
return ss;
}
void phongbeo()
{
cin>>n>>k>>m;
for(int i=1;i<=n;i++)reset(i);
for(int i=k;i>=1;i--)
for(int j=i;j<=n-i;j+=i)
{
s2=tim(j);
s3=tim(j+i);
if(s2!=s3)
{
noi(s2,s3);
v[j].pb({j+i,i});
v[j+i].pb({j,i});
}
}
dfs(1,0,0);
for(int i=1;i<17;i++)
for(int j=1;j<=n;j++)
st[i][j].a=st[i-1][st[i-1][j].a].a,
st[i][j].b=min(st[i-1][j].b,st[i-1][st[i-1][j].a].b);
for(int i=1;i<=m;i++)
{
cin>>l>>r;
s2=lca(l,r);
s3=depth[l]-depth[s2];
s4=depth[r]-depth[s2];
cout<<k-min(get(l,s3),get(r,s4))+1;
down
//<<" "<<get(l,s3),down
}
}
Compilation message
pictionary.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main()
| ^~~~
pictionary.cpp: In function 'long long int lca(long long int, long long int)':
pictionary.cpp:122:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
122 | for(int i=16;i>=0;--i)
| ^~~
pictionary.cpp:125:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
125 | return st[0][x].a;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
32108 KB |
Output is correct |
2 |
Correct |
28 ms |
32084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
32592 KB |
Output is correct |
2 |
Correct |
35 ms |
32472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
33368 KB |
Output is correct |
2 |
Correct |
37 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
33884 KB |
Output is correct |
2 |
Correct |
55 ms |
33884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
34812 KB |
Output is correct |
2 |
Correct |
53 ms |
34584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
36200 KB |
Output is correct |
2 |
Correct |
141 ms |
36392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
37144 KB |
Output is correct |
2 |
Correct |
144 ms |
37644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
38748 KB |
Output is correct |
2 |
Correct |
175 ms |
38480 KB |
Output is correct |