Submission #711402

#TimeUsernameProblemLanguageResultExecution timeMemory
711402Ahmed57Pictionary (COCI18_pictionary)C++14
0 / 140
233 ms38984 KiB
#include<bits/stdc++.h> using namespace std; //DSU #define int long long int mn = 100001; int pr[100001]; int gs[100001]; int findleader(int x){ if(pr[x]==x){ return x; } return pr[x] = findleader(pr[x]); } bool mergegroup(int x,int y){ int led1 = findleader(x); int led2 = findleader(y); if(led1==led2)return 0; if(gs[led1]>gs[led2]){ pr[led2]=led1; gs[led1]+=gs[led2]; }else{ pr[led1]=led2; gs[led2]+=gs[led1]; } return 1; } long long P[100001][18]; long long ma[100001][18]; long long hi[100001]; vector<pair<int,int>> adj[100001]; void dfs(int i,int pr,long long co){ P[i][0] = pr; ma[i][0] = co; hi[i] = hi[pr]+1; for(int j = 1;j<18;j++){ P[i][j] = P[P[i][j-1]][j-1]; ma[i][j] = max(ma[i][j-1],ma[P[i][j-1]][j-1]); } for(auto j:adj[i]){ if(j.first!=pr)dfs(j.first,i,j.second); } }long long lca(int y,int x){ if(hi[x]<hi[y]) swap(x,y); long long ans = -1e18; for(int k=17;k>=0;k--) { if(hi[x]-(1<<k) >= hi[y]){ ans = max(ans,ma[x][k]); x=P[x][k]; } } if(x==y) return ans; for(int k=17;k>=0;k--) { if(P[x][k] != P[y][k]){ ans = max({ans,ma[x][k],ma[y][k]}); x=P[x][k],y=P[y][k]; } } return max({ans,ma[x][0],ma[y][0]}); } signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); long long n,m,q; cin>>n>>m>>q; for(int i = 0;i<=mn;i++){ pr[i] = i;gs[i] =1 ; } for(int i = m;i>=1;i--){ for(int j = i+i;j<=n;j+=i){ if(mergegroup(i,j)){ adj[i].push_back({j,i}); adj[j].push_back({i,i}); } } } dfs(1,1,1e18); while(q--){ int a,b;cin>>a>>b; cout<<m-lca(a,b)+1<<endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...