Submission #1176483

#TimeUsernameProblemLanguageResultExecution timeMemory
1176483InvMODPictionary (COCI18_pictionary)C++20
140 / 140
90 ms14660 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;


struct DSU{
    vector<int> par;
    DSU(int n = 0): par(n + 1, -1) {}

    int asc(int x){
        return par[x] < 0 ? x : par[x] = asc(par[x]);
    }

    void join(int u, int v){
        u = asc(u), v = asc(v);

        if(u != v){
            if(par[u] > par[v]) swap(u, v);
            par[u] += par[v];
            par[v] = u;
        }
    }

    void reset(int n){
        FOR(i, 0, n) par[i] = -1;
    }

    bool same(int u, int v){
        return asc(u) == asc(v);
    }
};

void solve()
{
    int n,m,q; cin >> n >> m >> q;

    vector<pair<int,int>> Q(q);
    FOR(i, 0, q - 1){
        int u,v; cin >> u >> v;
        Q[i] = make_pair(u, v);
    }

    vector<int> ql(q + 1), qr(q + 1);
    FOR(i, 0, q - 1){
        ql[i] = 0, qr[i] = m + 1;
    }

    vector<vector<int>> Qid(m + 1, vector<int>());

    bool changed = true; int cntQ = 0; DSU dsu(n);
    while(changed){
        changed = false;

        FOR(i, 0, q - 1){
            if(ql[i] + 1 < qr[i]){
                Qid[(ql[i] + qr[i]) >> 1].push_back(i);
                ++cntQ; changed = true;
            }
        }

        for(int i = m; i >= 1; i--){
            if(!cntQ) break;
            for(int j = i * 2; j <= n; j += i){
                dsu.join(i, j);
            }

            while(sz(Qid[i])){
                --cntQ;

                int id = Qid[i].back(); Qid[i].pop_back();
                if(dsu.same(Q[id].fi, Q[id].se)){
                    ql[id] = i;
                }
                else qr[id] = i;
            }
        }

        dsu.reset(n);
    }

    FOR(i, 0, q - 1){
        cout << m - ql[i] + 1 << "\n";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message (stderr)

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