Submission #670899

# Submission time Handle Problem Language Result Execution time Memory
670899 2022-12-11T09:41:41 Z Do_you_copy Pictionary (COCI18_pictionary) C++17
140 / 140
117 ms 24896 KB
/*
    I find it wholesome to be alone the greater part of the time.
    To be in company, even with the best, is soon wearisome and dissipating.
    I love to be alone.
    I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, m, q;
struct TDSU{
    vector <pii> save;
    vector <int> lab;
    TDSU(){}
    TDSU(int _n){
        lab.resize(_n, -1);
    }
    inline void resize(int _n){
        lab.resize(_n, -1);
    }
    inline int find_set(int u){
        if (lab[u] < 0) return u;
        return lab[u] = find_set(lab[u]);
    }
    inline int find(int u){
        if (lab[u] < 0) return u;
        return find(lab[u]);
    }
    inline bool unite(int u, int v){
        u = find_set(u);
        v = find_set(v);
        if (u == v) return 0;
        if (lab[u] > lab[v]) swap(u, v);
        save.pb({v, lab[v]});
        lab[u] += lab[v];
        lab[v] = u;
        return 1;
    }
    inline void undo(){
        int v = save.back().fi;
        int u = lab[v];
        lab[v] = save.back().se;
        lab[u] -= lab[v];
        save.pop_back();
    }
    inline void clear(){
        lab.clear();
        save.clear();
    }
    inline void reset(int _n){
        clear();
        resize(_n);
    }
    inline void roll_back(int _n){
        while (save.size() > _n) undo();
    }
};

vector <int> adj[maxN];
int depth[maxN];
int lift[maxN][20], rmq[maxN][20];
void dfs(int u, int p){
    depth[u] = depth[p] + 1;
    rmq[u][0] = abs(p - u);
    lift[u][0] = p;
    for (int i = 1; i < 20; ++i){
        lift[u][i] = lift[lift[u][i - 1]][i - 1];
        rmq[u][i] = min(rmq[u][i - 1], rmq[lift[u][i - 1]][i - 1]);
    }
    for (int i: adj[u]){
        if (i == p) continue;
        dfs(i, u);
    }
}

inline int lca(int u, int v){
    if (depth[u] < depth[v]) swap(u, v);
    int k = depth[u] - depth[v];
    int res = m + 1;
    while (k){
        int t = __builtin_ctz(k);
        res = min(res, rmq[u][t]);
        u = lift[u][t];
        k -= 1 << t;
    }
    if (u == v) return m - res + 1;
    for (int i = 19; i >= 0; --i){
        if (lift[u][i] != lift[v][i]){
            res = min(res, rmq[u][i]);
            res = min(res, rmq[v][i]);
            u = lift[u][i];
            v = lift[v][i];
        }
    }
    res = min(res, rmq[u][0]);
    res = min(res, rmq[v][0]);
    return m - res + 1;
}

void Init(){
    cin >> n >> m >> q;
    TDSU dsu(n + 1);
    for (int i = m; i >= 1; --i){
        for (int j = 2 * i; j <= n; j += i){
            if (dsu.unite(j - i, j)){
                adj[j - i].pb(j);
                adj[j].pb(j - i);
            }
        }
    }
    dfs(1, 0);
    while (q--){
        int a, b;
        cin >> a >> b;
        cout << lca(a, b) << "\n";
    }
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message

pictionary.cpp: In member function 'void TDSU::roll_back(int)':
pictionary.cpp:66:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   66 |         while (save.size() > _n) undo();
      |                ~~~~~~~~~~~~^~~~
pictionary.cpp: In function 'int main()':
pictionary.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3580 KB Output is correct
2 Correct 20 ms 3652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4096 KB Output is correct
2 Correct 25 ms 3924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 8008 KB Output is correct
2 Correct 35 ms 7924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 9904 KB Output is correct
2 Correct 36 ms 10768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 13232 KB Output is correct
2 Correct 47 ms 13472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 16708 KB Output is correct
2 Correct 77 ms 18332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 20200 KB Output is correct
2 Correct 96 ms 22416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 24896 KB Output is correct
2 Correct 117 ms 24860 KB Output is correct