Submission #98179

#TimeUsernameProblemLanguageResultExecution timeMemory
98179dalgerokPictionary (COCI18_pictionary)C++17
140 / 140
469 ms25644 KiB
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5, M = 20; int n, m, k, pp[N], d[N]; int p[M + 1][N], dp[M + 1][N]; vector < pair < int, int > > g[N]; int dsu_get(int v){ return pp[v] == v ? v : pp[v] = dsu_get(pp[v]); } void dfs(int v, int pr = 0, int prval = 0){ p[0][v] = pr; dp[0][v] = prval; for(int i = 1; i <= M; i++){ p[i][v] = p[i - 1][p[i - 1][v]]; dp[i][v] = max(dp[i - 1][v], dp[i - 1][p[i - 1][v]]); } for(auto it : g[v]){ int to = it.first, len = it.second; if(to != pr){ d[to] = d[v] + 1; dfs(to, v, len); } } } inline int lca(int x, int y){ if(x == y){ return x; } if(d[x] > d[y]){ swap(x, y); } int len = d[y] - d[x]; for(int i = 0; i <= M; i++){ if((len >> i) & 1){ y = p[i][y]; } } if(x == y){ return x; } for(int i = M; i >= 0; i--){ if(p[i][x] != p[i][y]){ x = p[i][x]; y = p[i][y]; } } return p[0][x]; } inline int get(int x, int y){ if(x == y){ return 0; } int ans = 0, len = d[y] - d[x]; for(int i = M; i >= 0; i--){ if((len >> i) & 1){ ans = max(ans, dp[i][y]); y = p[i][y]; } } return ans; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m >> k; for(int i = 1; i <= n; i++){ pp[i] = i; } for(int i = m; i >= 1; i--){ for(int j = 2; j * i <= n; j++){ if(dsu_get(i) != dsu_get(i * j)){ pp[dsu_get(i)] = dsu_get(i * j); g[i].push_back(make_pair(i * j, m - i + 1)); g[i * j].push_back(make_pair(i, m - i + 1)); } } } dfs(1); while(k--){ int x, y; cin >> x >> y; int z = lca(x, y); cout << max(get(z, x), get(z, y)) << "\n"; } }
#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...