結果
| 問題 |
No.2786 RMQ on Grid Path
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2024-06-14 22:21:06 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,015 ms / 6,000 ms |
| コード長 | 2,393 bytes |
| コンパイル時間 | 5,080 ms |
| コンパイル使用メモリ | 316,452 KB |
| 実行使用メモリ | 33,160 KB |
| 最終ジャッジ日時 | 2024-06-14 22:21:46 |
| 合計ジャッジ時間 | 24,513 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector a(h, vector<int>(w));
rep(i, h)rep(j, w)cin >> a[i][j];
vector<pair<int, pair<int, int>>>edge;
vector edges(h * w + 1, vector<P>());
rep(i, h)rep(j, w) {
if (h - 1 != i) {
int ni = i + 1;
int nj = j + 0;
int index0 = i * w + j;
int index1 = ni * w + nj;
int weight = max(a[i][j], a[ni][nj]);
edges[weight].push_back({ index0,index1 });
}
if (w - 1 != j) {
int ni = i + 0;
int nj = j + 1;
int index0 = i * w + j;
int index1 = ni * w + nj;
int weight = max(a[i][j], a[ni][nj]);
edges[weight].push_back({ index0,index1 });
}
}
int q; cin >> q;
vector<int>rs(q), cs(q), rt(q), ct(q);
rep(i, q)cin >> rs[i] >> cs[i] >> rt[i] >> ct[i];
vector<P>query(q);
rep(i, q) {
rs[i]--, cs[i]--, rt[i]--, ct[i]--;
query[i].first = rs[i] * w + cs[i];
query[i].second = rt[i] * w + ct[i];
}
const int k = h * w + 1;
std::vector<int>ok(q, k), ng(q, -1);
while (true) {
bool fin = true;
std::vector<std::vector<int>>mid(k);
for (int i = 0; i < q; ++i) {
if (abs(ok[i] - ng[i]) > 1) {
fin = false;
int m = (ok[i] + ng[i]) / 2;
mid[m].push_back(i);
}
}
if (fin)break;
dsu uf(h * w);
for (int i = 0; i < k; ++i) {
for (auto e : edges[i]) {
uf.merge(e.first, e.second);
}
for (int j = 0; j < mid[i].size(); ++j) {
int idx = mid[i][j];
if (uf.same(query[idx].first,query[idx].second))ok[mid[i][j]] = i;
else ng[mid[i][j]] = i;
}
}
}
rep(i, q)cout << ok[i] << endl;
return 0;
}
kwm_t