結果
問題 | No.2786 RMQ on Grid Path |
ユーザー |
![]() |
提出日時 | 2024-06-15 19:07:09 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,600 ms / 6,000 ms |
コード長 | 1,984 bytes |
コンパイル時間 | 4,548 ms |
コンパイル使用メモリ | 257,328 KB |
最終ジャッジ日時 | 2025-02-21 22:58:39 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 35 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; vector<int> di = {-1, 0, 1, 0}; vector<int> dj = {0, 1, 0, -1}; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); vector<vector<pair<int, int>>> grid(h * w + 1); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; grid[a[i][j]].push_back({i, j}); } } auto inc = [&](int i, int j) { return (0 <= i && i < h && 0 <= j && j < w); }; 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]--; } vector<int> ok(q, h * w), ng(q, 0); auto finish = [&]() { for (int i = 0; i < q; i++) { if (ok[i] - ng[i] > 1) { return false; } } return true; }; while (!finish()) { vector<vector<int>> mids(h * w + 1); for (int i = 0; i < q; i++) { int mid = (ok[i] + ng[i]) / 2; mids[mid].push_back(i); } dsu uf(h * w); for (int i = 0; i <= h * w; i++) { for (auto [x, y] : grid[i]) { for (int k = 0; k < 4; k++) { int nx = x + di[k], ny = y + dj[k]; if (!inc(nx, ny)) { continue; } if (a[nx][ny] <= i) { uf.merge(w * x + y, w * nx + ny); } } } for (int id : mids[i]) { if (uf.same(w * rs[id] + cs[id], w * rt[id] + ct[id])) { ok[id] = i; } else { ng[id] = i; } } } } for (int i = 0; i < q; i++) { cout << ok[i] << endl; } }