結果

問題 No.2786 RMQ on Grid Path
ユーザー zyoobnzyoobn
提出日時 2024-06-15 18:19:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 852 ms / 6,000 ms
コード長 3,679 bytes
コンパイル時間 2,741 ms
コンパイル使用メモリ 216,260 KB
実行使用メモリ 143,928 KB
最終ジャッジ日時 2024-06-15 18:20:03
合計ジャッジ時間 21,175 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 777 ms
127,412 KB
testcase_13 AC 755 ms
126,380 KB
testcase_14 AC 788 ms
127,028 KB
testcase_15 AC 750 ms
127,924 KB
testcase_16 AC 852 ms
127,924 KB
testcase_17 AC 796 ms
126,004 KB
testcase_18 AC 797 ms
124,852 KB
testcase_19 AC 807 ms
126,136 KB
testcase_20 AC 815 ms
123,436 KB
testcase_21 AC 809 ms
123,316 KB
testcase_22 AC 604 ms
127,928 KB
testcase_23 AC 629 ms
127,792 KB
testcase_24 AC 557 ms
130,232 KB
testcase_25 AC 616 ms
130,228 KB
testcase_26 AC 569 ms
130,224 KB
testcase_27 AC 169 ms
24,056 KB
testcase_28 AC 107 ms
6,656 KB
testcase_29 AC 697 ms
118,784 KB
testcase_30 AC 121 ms
11,008 KB
testcase_31 AC 22 ms
7,680 KB
testcase_32 AC 552 ms
119,352 KB
testcase_33 AC 64 ms
5,376 KB
testcase_34 AC 625 ms
143,928 KB
testcase_35 AC 665 ms
143,924 KB
testcase_36 AC 718 ms
143,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

struct dsu {
    int n;
    vector<int> p, size;
    dsu(int n) : n(n), p(n + 1), size(n + 1, 1) {
        iota(p.begin(), p.end(), 0);
    }
    int find(int x) {
        while(x != p[x]) {
            p[x] = p[p[x]];
            x = p[x];
        }
        return x;
    }
    void merge(int x, int y) {
        int x_fa = find(x), y_fa = find(y);
        if(x_fa != y_fa) {
            p[y_fa] = x_fa;
            size[x_fa] += size[y_fa];
        }
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

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

    vector<int> a(n * m);
    for (int i = 0; i < n * m; ++i) {
        cin >> a[i];
    }

    vector<int> id(n * m);
    iota(id.begin(), id.end(), 0);

    sort(id.begin(), id.end(), [&] (auto& x, auto& y) {
        return a[x] < a[y];
    });

    array<int, 4> dx{0, 1, 0, -1};
    array<int, 4> dy{1, 0, -1, 0};
 
    dsu d(2 * n * m);
    vector g(2 * n * m, vector<int>());
    int pos = n * m;
    vector<int> val(2 * n * m);
    vector<bool> vis(n * m);
    for (auto& x : id) {
        vis[x] = true;
        for (int i = 0; i < 4; ++i) {
            int xx = x / m + dx[i], yy = x % m + dy[i];
            int y = xx * m + yy;
            if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[y]) {
                int u = d.find(x), v = d.find(y);
                if (u == v) {
                    continue;
                }
                g[pos].push_back(u);
                g[u].push_back(pos);
                g[pos].push_back(v);
                g[v].push_back(pos);
                d.merge(pos, u);
                d.merge(pos, v);
                val[pos] = a[x];
                pos++;
            }
        }
    }

    const int lg = __lg(2 * n * m);
    vector<int> dep(2 * n * m);
    vector fa(lg + 1, vector<int>(2 * n * m, -1));
    vector w(lg + 1, vector<int>(2 * n * m));

    auto dfs = [&] (auto dfs, int u, int f) -> void {
        w[0][u] = val[u];
        for (int i = 1; i <= lg; ++i) {
            if (fa[i - 1][u] == -1) {
                continue;
            }
            fa[i][u] = fa[i - 1][fa[i - 1][u]];
            w[i][u] = max(w[i - 1][u], w[i - 1][fa[i - 1][u]]);
        }
        for (auto& v: g[u]) {
            if (v == f) {
                continue;
            }
            fa[0][v] = u;
            dep[v] = dep[u] + 1;
            dfs(dfs, v, u);
        }
    };
    dfs(dfs, 0, -1);

    auto query = [&] (int u, int v) {
        if (dep[u] < dep[v]) {
            swap(u, v);
        }

        int maxx = 0;

        for (int i = lg; i >= 0; --i) {
            if (dep[u] - (1 << i) >= dep[v]) {
                maxx = max(maxx, w[i][u]);
                u = fa[i][u];
                maxx = max(maxx, w[0][u]);
            }
        }

        if (u == v) {
            return maxx;
        }

        for (int i = lg; i >= 0; --i) {
            if (fa[i][u] != fa[i][v]) {
                maxx = max(maxx, w[i][u]);
                u = fa[i][u];
                maxx = max(maxx, w[0][u]);

                maxx = max(maxx, w[i][v]);
                v = fa[i][v];
                maxx = max(maxx, w[0][v]);
            }
        }


        return max(maxx, w[0][fa[0][u]]);
    };


    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        --x1, --y1, --x2, --y2;
        cout << query(x1 * m + y1, x2 * m + y2) << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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