제출 #1340703

#제출 시각아이디문제언어결과실행 시간메모리
1340703shiba_ensurePictionary (COCI18_pictionary)C++20
140 / 140
81 ms22600 KiB
#include <bits/stdc++.h>
#include <stdio.h>

#define __Shibae__      signed main()
#define IOS             ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fiopen(Path)    freopen(Path".INP", "r", stdin); freopen(Path".OUT", "w", stdout);
#define fipen(Path)     freopen(Path".INP", "r", stdin);
#define sz(s)           (int)s.size()
#define all(x)          x.begin(), x.end()
#define maxHeap         priority_queue<int>
#define minHeap         priority_queue<int, vector<int>, greater<int>>
#define getBit(x, k)    (((x) >> (k)) & 1)
#define MASK(i)         (1LL << (i))
#define SQR(x)          (1LL * ((x) * (x)))
#define db              double
#define ld              long double
#define ui              unsigned int
#define ll              long long
#define ii              pair<int, int>
#define pli             pair<ll, int>
#define pil             pair<int, ll>
#define pll             pair<ll, ll>
#define fi              first
#define se              second

#define FOR(i, a, b)    for(int i = a, _b = b; i <= _b; i += 1)
#define FOD(i, a, b)    for(int i = a, _b = b; i >= _b; i -= 1)
#define REP(i, a)       for(int i = 0, _a = a; i < _a; i++)
#define pb              push_back
#define fau(u, a)       for(auto &u : a)
#define debug           return cout << "debug", void();

using namespace std;

const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int MAX = 5e5+5;
 
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

ll Rand(ll l, ll r) 
{
    return uniform_int_distribution<ll>(l, r)(rd);
}

template<class SHIBA, class ENGINE>
    bool minimize(SHIBA &x, const ENGINE y)
    {
        if(x > y)
        {
            x = y;
            return true;
        } 
        else return false;
    }
template<class SHIBA, class ENGINE>
    bool maximize(SHIBA &x, const ENGINE y)
    {
        if(x < y)
        {
            x = y;
            return true;
        }
        else return false;
    }


/* Template by: Nguyen Nhat Anh from Luong Van Chanh High School for the gifted */
/* From Min Tuoi with love */
        /**       TRY HARD        **/
        /**          ORZ          **/

/* -----------------[ MAIN CODE ]----------------- */

const int lg = 17;

int n, m, q;

int dsu[MAX];
vector<int> g[MAX];
int sz[MAX];
int a[MAX];
int par[MAX][lg + 1], mx[MAX][lg + 1];
int h[MAX];

void input()
{
    cin >> n >> m >> q;
}

int find_set(int u)
{
    return u == dsu[u] ? u : dsu[u] = find_set(dsu[u]);
}

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

    if (u == v) return;

    dsu[v] = u;
    sz[u] += sz[v];
    g[u].pb(v);
    g[v].pb(u);
}

void dfs(int u, int pre)
{
    fau(v, g[u])
    {
        if (v == pre) continue;

        h[v] = h[u] + 1;
        par[v][0] = u;
        mx[v][0] = max(a[u], a[v]);

        FOR(i, 1, lg) 
        {
            par[v][i] = par[par[v][i-1]][i-1];
            mx[v][i] = max(mx[v][i-1], mx[par[v][i-1]][i-1]);
        }

        dfs(v, u);
    }
}

int get(int u, int v)
{
    int res = 0;
    if (h[u] < h[v]) swap(u, v);
    for (int k = h[u] - h[v]; k; k ^= k & -k) 
    {
        int i = __builtin_ctz(k);
        maximize(res, mx[u][i]);
        u = par[u][i];
    }

    if (u == v) return res;

    FOD(i, __lg(h[u]), 0) if (par[u][i] != par[v][i])
    {
        maximize(res, max(mx[u][i], mx[v][i]));
        u = par[u][i];
        v = par[v][i];
    }
    return max({res, mx[u][0], mx[v][0]});
}

void solve()
{
    FOR(i, 1, n) 
    {
        dsu[i] = i;
        sz[i] = 1;
    }
    FOD(i, m, 1)
    {
        if (!a[i]) a[i] = m-i+1;
        for (int j = 2 * i; j <= n; j += i) 
        {
            if (!a[j]) a[j] = m-i+1;
            join(j - i, j);
        }

        for (int j = i; j <= n; j += i)
        if (sz[find_set(n)] == n) break;
    }    
    dfs(1, -1);

    while(q--)
    {
        int u, v; cin >> u >> v;
        cout << get(u, v) << "\n";
    }
}

__Shibae__
{
    IOS

    const bool multitest = 0;
    int tt = 1; if(multitest) cin >> tt;
 
    while( tt-- ){
        input();
        solve();
        if(tt) cout << "\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...