Submission #748746

#TimeUsernameProblemLanguageResultExecution timeMemory
748746ETheBest3Pictionary (COCI18_pictionary)C++14
0 / 140
127 ms49868 KiB
#include<bits/stdc++.h> #define lli long long #define endl "\n" using namespace std; const lli MAXN=100005, LOGN=25, INF=999999999; lli N, M, Q, par[MAXN], sz[MAXN], dep[MAXN], ti[MAXN], up[LOGN+1][MAXN]; lli koren, maxx[LOGN+1][MAXN], a, b; vector<lli> v[MAXN]; void add_node(lli x){ par[x]=x; sz[x]=1; ti[x]=x; return; } lli find_parent(lli x){ if(par[x]==x)return x; return find_parent(par[x]); } void merge_v(lli x, lli y){ lli rootx=find_parent(x), rooty=find_parent(y); if(rootx==rooty)return; if(sz[rootx]<=sz[rooty]){ par[rootx]=rooty; sz[rooty]+=sz[rootx]; ti[rootx]=min(x, y); }else{ par[rooty]=rootx; sz[rootx]+=sz[rooty]; ti[rooty]=min(x, y); } return; } void dfs(lli x){ for(lli i=0; i<v[x].size(); i++){ dep[v[x][i]]=dep[x]+1; dfs(v[x][i]); } return; } lli lca(lli x, lli y){ if(dep[x]<dep[y])swap(x, y); lli ans=INF; for(lli i=LOGN; i>=0; i--){ if(dep[x]-(1<<i)>=dep[y]){ ans=min(ans, maxx[i][x]); x=up[i][x]; } } if(x==y)return ans; for(lli i=LOGN; i>=0; i--){ if(up[i][x]!=up[i][y]){ ans=min(ans, maxx[i][x]); ans=min(ans, maxx[i][y]); x=up[i][x]; y=up[i][y]; } } ans=min(ans, maxx[0][x]); ans=min(ans, maxx[0][y]); return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>N>>M>>Q; for(lli i=1; i<=N; i++)add_node(i); for(lli i=M; i>0; i--){ for(lli j=2*i; j<=N; j+=i){ merge_v(i, j); } } for(lli i=1; i<=N; i++){ up[0][i]=par[i]; maxx[0][i]=ti[i]; if(par[i]==i)koren=i; else v[par[i]].push_back(i); } dep[koren]=1; dfs(koren); for(lli st=1; st<=LOGN; st++){ for(lli i=1; i<=N; i++){ up[st][i]=up[st-1][up[st-1][i]]; maxx[st][i]=min(maxx[st-1][i], maxx[st-1][maxx[st-1][i]]); } } for(lli q=0; q<Q; q++){ cin>>a>>b; cout<<M-lca(a, b)+1<<endl; } return 0; }

Compilation message (stderr)

pictionary.cpp: In function 'void dfs(long long int)':
pictionary.cpp:34:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for(lli i=0; i<v[x].size(); i++){
      |                  ~^~~~~~~~~~~~
#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...