답안 #238680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
238680 2020-06-12T10:38:24 Z Vimmer Pictionary (COCI18_pictionary) C++14
98 / 140
186 ms 65536 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100101
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
//using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll a, b, cur;

pair <int, int>  t[N][20];

int pr[N], rk[N], h[N], logs[N], in[N], tr[N][20], lc;

vector <int> gr;

int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}

void link(int a, int b)
{
    if (rk[b] > rk[a]) swap(a, b);

    pr[b] = a;

    rk[a] += rk[b];
}

vector <pair <int, int> > g[N];

void add(int x, int y, int val)
{
    g[x].pb({y, val});

    g[y].pb({x, val});
}

void dfs(int v, int p, int val)
{
    if (p != -1) h[v] = h[p] + 1;

    in[v] = sz(gr);

    gr.pb(v);

    if (v != 1) t[v][0] = {val, p};

    for (auto it : g[v])
    {
        if (it.F == p) continue;

        dfs(it.F, v, it.S);

        gr.pb(v);
    }
}

int lca(int a, int b)
{
    if (in[a] > in[b]) swap(a, b);

    int j = logs[in[b] - in[a] + 1];

    a = tr[in[a]][j];

    b = tr[in[b] - (1 << j) + 1][j];

    if (h[a] > h[b]) return b;

    return a;
}
int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m, q;

    cin >> n >> m >> q;

    for (int i = 2; i < N; i++) logs[i] = logs[i >> 1] + 1;

    for (int i = 1; i <= n; i++) {rk[i] = 1; pr[i] = i;}

    for (int x = m; x >= 2; x--)
    {
        if (m / x == 1)
            for (int j = 1; j <= n / x; j++)
                for (int u = j + 1; u <= n / x; u++)
                {
                    int a = fnd(j * x), b = fnd(u * x);

                    if (a != b) {add(j * x, u * x, m - x + 1); link(a, b);}
                }
        else
            for (int j = 2; j <= n / x; j++)
            {
                int a = fnd(j * x), b = fnd(x);

                if (a != b) {add(j * x, x, m - x + 1); link(a, b);}
            }
    }

    for (int i = 2; i <= n; i++)
    {
        int a = fnd(1), b = fnd(i);

        if (a != b) {link(a, b); add(1, i, m); }
    }

    dfs(1, -1, -1);

    for (int i = 0; i < sz(gr); i++) tr[i][0] = gr[i];

    for (int j = 1; j < 20; j++)
        {
            for (int i = 1; i <= n; i++)
            {
                if (h[i] < (1 << j)) continue;

                pair <int, int> a = t[i][j - 1];

                pair <int, int> b = t[a.S][j - 1];

                t[i][j] = {max(a.F, b.F), b.S};
            }

            for (int i = 0; i < sz(gr); i++)
            {
                int a = tr[i][j - 1];

                int b = tr[min(sz(gr) - 1, i + (1 << (j - 1)))][j - 1];

                if (h[a] > h[b]) tr[i][j] = b; else tr[i][j] = a;
            }
        }

    for (; q > 0; q--)
    {
        int x, y;

        cin >> x >> y;

        lc = lca(x, y);

        int ans = -1;

        for (int j = 19; j >= 0; j--)
        {
            if (h[x] >= (1 << j) && h[x] - (1 << j) >= h[lc])
            {
                ans = max(ans, t[x][j].F);

                x = t[x][j].S;
            }

            if (h[y] >= (1 << j) && h[y] - (1 << j) >= h[lc])
            {
                ans = max(ans, t[y][j].F);

                y = t[y][j].S;
            }
        }

        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 3200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 3328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 3704 KB Output is correct
2 Correct 29 ms 3712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 3960 KB Output is correct
2 Correct 41 ms 3960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 11772 KB Output is correct
2 Correct 43 ms 12288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 15348 KB Output is correct
2 Correct 62 ms 17648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 20720 KB Output is correct
2 Correct 74 ms 22380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 186 ms 25460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 102 ms 57196 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 133 ms 65536 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -