Submission #1264747

#TimeUsernameProblemLanguageResultExecution timeMemory
1264747dzhoz0Pictionary (COCI18_pictionary)C++20
140 / 140
45 ms3400 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define INF 1e18
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>

const int MOD = 1'000'000'000 + 7;

void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    if (!name.empty())
    {
        freopen((name + ".INP").c_str(), "r", stdin);
        freopen((name + ".OUT").c_str(), "w", stdout);
    }
#endif
}

const int MAXN = 2e5;
 
int par[MAXN + 5];
int sz[MAXN + 5];
int time_changed[MAXN + 5];
 
void make_set(int v) {
    par[v] = v;
    sz[v] = 1;
}
 
int find_set(int v, int time) {
    if(par[v] == v || time_changed[v] > time) return v;
    return find_set(par[v], time);
}
 
void union_set(int u, int v, int time) {
    u = find_set(u, time);
    v = find_set(v, time);
    if(u != v) {
        if(sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        time_changed[v] = time;
    }
}

void solve()
{
    int n, m, q; cin >> n >> m >> q; 
    for(int i = 1; i <= n; i++) make_set(i);
    for(int i = 1; i <= m; i++) {
        int g = m - i + 1;
        for(int j = g * 2; j <= n; j += g) {
            union_set(g, j, i);
        } 
    }

    while(q--) {
        int u, v; cin >> u >> v;
        int l = 0, r = m;
        int ans = m;
        while(l <= r) {
            int mid = (l + r) / 2;
            if(find_set(u, mid) == find_set(v, mid)) {
                ans = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        cout << ans << '\n';
    }
}
signed main()
{
    setIO();
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}

Compilation message (stderr)

pictionary.cpp: In function 'void setIO(std::string)':
pictionary.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen((name + ".INP").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen((name + ".OUT").c_str(), "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...