Submission #1294205

#TimeUsernameProblemLanguageResultExecution timeMemory
1294205thaibeo123Pictionary (COCI18_pictionary)C++20
140 / 140
125 ms41748 KiB
#include <bits/stdc++.h> using namespace std; #define NAME "A" #define ll long long #define fi first #define se second #define pb push_back #define all(x) x.begin(), x.end() #define MASK(x) (1ll << (x)) #define BIT(x, i) (((x) >> (i)) & 1) const int N = 2e5 + 5; const int LG = 19; struct DSU { vector<int> lab; DSU(int n): lab(n + 1, -1) {} int find_set(int u) { return lab[u] < 0 ? u : lab[u] = find_set(lab[u]); } bool union_sets(int u, int v) { u = find_set(u); v = find_set(v); if (u == v) return 0; lab[u] += lab[v]; lab[v] = u; return 1; } }; int n, m, q, timer; int L[N * 2][21], hi[N], ri[N]; vector<int> g[N]; int get(int u, int v) { int l = ri[u], r = ri[v]; if (l > r) swap(l, r); int k = __lg(r - l + 1); u = L[l][k]; v = L[r - MASK(k) + 1][k]; return hi[u] < hi[v] ? u : v; } void dfs(int u) { L[++timer][0] = u; ri[u] = timer; for (int v : g[u]) { hi[v] = hi[u] + 1; dfs(v); L[++timer][0] = u; } } void input() { cin >> n >> m >> q; } void solve() { DSU dsu(n + m); for (int i = 1; i <= m; i++) { int x = m - i + 1; for (int j = x; j <= n; j += x) { int u = dsu.find_set(j); if (u != n + i) { g[n + i].pb(u); dsu.union_sets(n + i, u); } } } dfs(n + m); for (int j = 1; j <= LG; j++) { for (int i = 1; i + MASK(j) - 1 <= timer; i++) { int u = L[i][j - 1]; int v = L[i + MASK(j - 1)][j - 1]; L[i][j] = hi[u] < hi[v] ? u : v; } } while (q--) { int u, v; cin >> u >> v; cout << get(u, v) - n << "\n"; } } signed main() { if (fopen(NAME".INP", "r")) { freopen(NAME".INP", "r", stdin); freopen(NAME".OUT", "w", stdout); } cin.tie(0)->sync_with_stdio(0); input(); solve(); return 0; }

Compilation message (stderr)

pictionary.cpp: In function 'int main()':
pictionary.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen(NAME".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(NAME".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...