Submission #375911

#TimeUsernameProblemLanguageResultExecution timeMemory
375911SavicSPictionary (COCI18_pictionary)C++14
140 / 140
391 ms25964 KiB
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define ff(i,a,b) for(int i=a;i<=b;i++) #define fb(i,b,a) for(int i=b;i>=a;i--) using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair<int,int> pii; const int maxn = 100005; const int inf = 1e9 + 5; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // os.order_of_key(k) the number of elements in the os less than k // *os.find_by_order(k) print the k-th smallest number in os(0-based) int n, m, q; vector<pii> g[maxn]; int sz[maxn]; int par[maxn]; int findpar(int x){ if(x == par[x])return x; return par[x] = findpar(par[x]); } void unite(int x, int y){ int a = findpar(x); int b = findpar(y); if(sz[a] > sz[b])swap(a, b); par[b] = a; sz[a] += sz[b]; } void init(){ ff(i,1,n){ par[i] = i; sz[i] = 1; } } int timer = 0; int d[maxn]; int in[maxn]; int out[maxn]; int deda[maxn][20]; int mx[maxn][20]; void dfs(int v, int p, int pw){ in[v] = ++timer; deda[v][0] = p; mx[v][0] = pw; ff(i,1,19){ deda[v][i] = deda[deda[v][i - 1]][i - 1]; mx[v][i] = max(mx[v][i - 1], mx[deda[v][i - 1]][i - 1]); } for(auto c : g[v]){ int u = c.fi; int w = c.se; if(u != p){ d[u] = d[v] + 1; dfs(u, v, w); } } out[v] = timer; } bool predak(int x, int y){ return (in[x] <= in[y] && out[x] >= out[y]); } int lca(int x, int y){ if(predak(x, y))return x; if(predak(y, x))return y; int najv = 0; fb(i,19,0){ if(deda[x][i] && !predak(deda[x][i], y)){ x = deda[x][i]; } } return deda[x][0]; } int calc(int x, int y){ int najv = 0; fb(i,19,0){ if(y & (1 << i)){ najv = max(najv, mx[x][i]); x = deda[x][i]; } } return najv; } int main() { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); cin >> n >> m >> q; init(); fb(i,m,1){ for(int j=2*i;j<=n;j+=i){ if(findpar(i) == findpar(j))continue; unite(i, j); g[i].pb({j, m - i + 1}); g[j].pb({i, m - i + 1}); } } dfs(1, 0, 0); while(q--){ int a, b; cin >> a >> b; int lc = lca(a, b); int ans = max(calc(a, d[a] - d[lc]), calc(b, d[b] - d[lc])); cout << ans << endl; } return 0; } /** // probati bojenje sahovski ili slicno **/

Compilation message (stderr)

pictionary.cpp: In function 'int lca(int, int)':
pictionary.cpp:83:6: warning: unused variable 'najv' [-Wunused-variable]
   83 |  int najv = 0;
      |      ^~~~
#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...