結果
問題 | No.2786 RMQ on Grid Path |
ユーザー | ir5 |
提出日時 | 2024-06-15 01:54:55 |
言語 | C++23(gcc13) (gcc 13.2.0 + boost 1.83.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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 3 ms
6,940 KB |
testcase_03 | AC | 4 ms
6,940 KB |
testcase_04 | AC | 3 ms
6,944 KB |
testcase_05 | AC | 4 ms
6,940 KB |
testcase_06 | AC | 3 ms
6,940 KB |
testcase_07 | AC | 4 ms
6,944 KB |
testcase_08 | AC | 3 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 4 ms
6,944 KB |
testcase_12 | AC | 2,595 ms
14,460 KB |
testcase_13 | AC | 2,654 ms
13,436 KB |
testcase_14 | AC | 2,557 ms
13,696 KB |
testcase_15 | AC | 2,564 ms
13,476 KB |
testcase_16 | AC | 2,603 ms
13,496 KB |
testcase_17 | AC | 2,604 ms
14,096 KB |
testcase_18 | AC | 2,609 ms
13,424 KB |
testcase_19 | AC | 2,587 ms
14,392 KB |
testcase_20 | AC | 2,586 ms
13,372 KB |
testcase_21 | AC | 2,634 ms
14,384 KB |
testcase_22 | AC | 1,310 ms
14,904 KB |
testcase_23 | AC | 1,289 ms
14,356 KB |
testcase_24 | AC | 2,419 ms
13,172 KB |
testcase_25 | AC | 2,434 ms
13,264 KB |
testcase_26 | AC | 2,390 ms
13,244 KB |
testcase_27 | AC | 971 ms
10,648 KB |
testcase_28 | AC | 1,341 ms
10,452 KB |
testcase_29 | AC | 1,673 ms
11,996 KB |
testcase_30 | AC | 1,152 ms
10,320 KB |
testcase_31 | AC | 103 ms
6,944 KB |
testcase_32 | AC | 2,412 ms
13,004 KB |
testcase_33 | AC | 310 ms
16,860 KB |
testcase_34 | AC | 2,469 ms
12,800 KB |
testcase_35 | AC | 2,483 ms
12,932 KB |
testcase_36 | AC | 2,478 ms
12,920 KB |
ソースコード
#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(); }