Submission #278872

#TimeUsernameProblemLanguageResultExecution timeMemory
278872HynDufPictionary (COCI18_pictionary)C++14
140 / 140
154 ms22520 KiB
#include <bits/stdc++.h> #define task "P" #define all(v) (v).begin(), (v).end() #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define Rep(i, r, l) for (int i = (r); i >= (l); --i) #define DB(X) { cerr << #X << " = " << (X) << '\n'; } #define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; } #define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; } #define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; } #define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';} #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define pf push_front #define F first #define S second #define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a)); #define next ___next #define prev ___prev #define y1 ___y1 #define left ___left #define right ___right #define y0 ___y0 #define div ___div #define j0 ___j0 #define jn ___jn using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<ll> vl; const int N = 1e5 + 2; int n, m, q, rt[N], rk[N], pa[N][17], dep[N], mn[N][17]; vii g[N]; int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);} void dfs(int u, int p) { pa[u][0] = p; rep(k, 1, 16) pa[u][k] = pa[pa[u][k - 1]][k - 1], mn[u][k] = min(mn[u][k - 1], mn[pa[u][k - 1]][k - 1]); dep[u] = dep[p] + 1; for (ii v : g[u]) if (v.F != p) { mn[v.F][0] = v.S; dfs(v.F, u); } } int query(int x, int y) { if (dep[x] < dep[y]) swap(x, y); int mnn = 1e9; Rep(k, 16, 0) if (dep[pa[x][k]] >= dep[y]) mnn = min(mnn, mn[x][k]), x = pa[x][k]; if (x == y) return mnn; Rep(k, 16, 0) if (pa[x][k] != pa[y][k]) { mnn = min({mnn, mn[x][k], mn[y][k]}); x = pa[x][k], y = pa[y][k]; } return min({mnn, mn[x][0], mn[y][0]}); } int main() { #ifdef HynDuf freopen(task".in", "r", stdin); //freopen(task".out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n >> m >> q; int cnt = 0; rep(i, 1, n) rt[i] = i; Rep(i, m, 1) { for (int j = 2; j * i <= n; j++) { int x = frt(i), y = frt(j * i); if (x != y) { cnt++; if (rk[x] < rk[y]) swap(x, y); if (rk[x] == rk[y]) rk[x]++; rt[y] = x; g[i].eb(j * i, i); g[j * i].eb(i, i); } if (cnt == n - 1) break; } if (cnt == n - 1) break; } dfs(1, 0); while (q--) { int u, v; cin >> u >> v; cout << m - query(u, v) + 1 << '\n'; } return 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...