結果

問題 No.2786 RMQ on Grid Path
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-06-14 22:42:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,244 bytes
コンパイル時間 3,557 ms
コンパイル使用メモリ 236,452 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2024-06-14 22:42:17
合計ジャッジ時間 12,662 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

class UnionFind {
   public:
    vector<map<int, int>> par;
    void init(int n) {
        par.resize(n);
        for (int i = 0; i < n; i++) {
            par[i][0] = -1;
        }
    };
    int leader(int u, int t = 1e9) {
        auto it = par[u].lower_bound(t + 1);
        it--;
        int p = it->second;
        if (p < 0) {
            return u;
        }
        return par[u][t] = leader(p, t);
    }
    bool same(int u, int v, int t = 1e9) {
        return leader(u, t) == leader(v, t);
    }
    void merge(int u, int v, int t) {
        u = leader(u, t);
        v = leader(v, t);
        if (u == v) {
            return;
        }
        par[u][t] = v;
    }
};
int main() {
    fast_io();
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }
    vector<tuple<int, int, int>> edges;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int id = i * w + j;
            if (i + 1 < h) {
                int id2 = (i + 1) * w + j;
                int cost = max(a[i][j], a[i + 1][j]);
                edges.emplace_back(cost, id, id2);
            }
            if (j + 1 < w) {
                int id2 = i * w + j + 1;
                int cost = max(a[i][j], a[i][j + 1]);
                edges.emplace_back(cost, id, id2);
            }
        }
    }
    sort(edges.begin(), edges.end());
    UnionFind uf;
    uf.init(h * w);
    for (auto [cost, u, v] : edges) {
        uf.merge(u, v, cost);
    }
    int q;
    cin >> q;
    vector<int> rs(q), cs(q), rt(q), ct(q);
    for (int i = 0; i < q; i++) {
        cin >> rs[i] >> cs[i] >> rt[i] >> ct[i];
        rs[i]--;
        cs[i]--;
        rt[i]--;
        ct[i]--;
        int ng = 0, ok = h * w;
        while (ok - ng > 1) {
            int mid = (ok + ng) / 2;
            if (uf.same(rs[i] * w + cs[i], rt[i] * w + ct[i], mid)) {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        cout << ok << "\n";
    }
}
0