結果
| 問題 |
No.2786 RMQ on Grid Path
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-15 01:54:55 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,654 ms / 6,000 ms |
| コード長 | 3,629 bytes |
| コンパイル時間 | 2,783 ms |
| コンパイル使用メモリ | 176,160 KB |
| 実行使用メモリ | 16,860 KB |
| 最終ジャッジ日時 | 2024-06-15 01:55:58 |
| 合計ジャッジ時間 | 58,040 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 35 |
ソースコード
#pragma GCC optimize("O3")
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <tuple>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ranges>
#include <iomanip>
#include <bitset>
using namespace std;
using ll = long long;
auto range(int n) { return views::iota(0, n); }
template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T> ostream& operator<<(ostream& os, const set<T>& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
#ifdef ONLINE_JUDGE
#define dump(expr) ;
#else
#define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; }
#endif
struct UF {
vector<int> data;
UF(int n) : data(vector<int>(n, -1)) {}
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
bool find(int x, int y) { return root(x) == root(y); }
void uni(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[x] < data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
}
};
void solve() {
int h, w; cin >> h >> w;
vector<vector<int>> vs(h, vector<int>(w));
auto d = [&](int i, int j) -> int { return i * w + j; };
vector<pair<int, int>> valpos(h * w);
for (int i : range(h))
for (int j : range(w)) {
cin >> vs[i][j];
valpos[i * w + j] = {vs[i][j], d(i, j)};
}
struct Q {
int idx, r1, c1, r2, c2;
};
int q; cin >> q;
vector<Q> queries;
for (int i : range(q)) {
int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2;
r1--; c1--;
r2--; c2--;
queries.push_back({i, r1, c1, r2, c2});
}
UF uf(h * w), uf0(h * w);
bitset<500 * 500> filled, filled0;
auto fillin = [&](int i, int j) {
filled[d(i, j)] = 1;
if (i > 0 && filled[d(i - 1, j)]) uf.uni(d(i, j), d(i - 1, j));
if (i < h - 1 && filled[d(i + 1, j)]) uf.uni(d(i, j), d(i + 1, j));
if (j > 0 && filled[d(i, j - 1)]) uf.uni(d(i, j), d(i, j - 1));
if (j < w - 1 && filled[d(i, j + 1)]) uf.uni(d(i, j), d(i, j + 1));
};
sort(valpos.begin(), valpos.end());
int C = 500;
vector<int> ans(q, -1);
int k0 = 0;
for (int k : range(h * w)) {
{
int i = valpos[k].second / w;
int j = valpos[k].second % w;
fillin(i, j);
}
if (k % C == C - 1 || k == h * w - 1) {
vector<Q> qs;
for (auto query : queries) if (ans[query.idx] < 0) {
if (uf.find(d(query.r1, query.c1), d(query.r2, query.c2))) {
qs.push_back(query);
}
}
swap(filled, filled0);
swap(uf0.data, uf.data);
for (int now = k0; now <= k; ++now) {
int i = valpos[now].second / w;
int j = valpos[now].second % w;
fillin(i, j);
for (const auto query : qs) {
if (ans[query.idx] < 0) {
if (uf.find(d(query.r1, query.c1), d(query.r2, query.c2))) {
ans[query.idx] = valpos[now].first;
}
}
}
}
k0 = k;
}
}
for (auto a : ans) cout << a << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
solve();
}