結果
問題 | No.2786 RMQ on Grid Path |
ユーザー | ゼット |
提出日時 | 2024-06-14 23:16:50 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,639 ms / 6,000 ms |
コード長 | 4,437 bytes |
コンパイル時間 | 1,768 ms |
コンパイル使用メモリ | 112,324 KB |
実行使用メモリ | 32,176 KB |
最終ジャッジ日時 | 2024-06-14 23:17:47 |
合計ジャッジ時間 | 56,463 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 4 ms
6,940 KB |
testcase_03 | AC | 4 ms
6,944 KB |
testcase_04 | AC | 4 ms
6,944 KB |
testcase_05 | AC | 4 ms
6,940 KB |
testcase_06 | AC | 4 ms
6,940 KB |
testcase_07 | AC | 4 ms
6,940 KB |
testcase_08 | AC | 4 ms
6,940 KB |
testcase_09 | AC | 4 ms
6,940 KB |
testcase_10 | AC | 4 ms
6,940 KB |
testcase_11 | AC | 4 ms
6,940 KB |
testcase_12 | AC | 2,628 ms
31,816 KB |
testcase_13 | AC | 2,606 ms
31,984 KB |
testcase_14 | AC | 2,629 ms
31,852 KB |
testcase_15 | AC | 2,639 ms
31,768 KB |
testcase_16 | AC | 2,612 ms
31,856 KB |
testcase_17 | AC | 2,624 ms
31,772 KB |
testcase_18 | AC | 2,597 ms
31,856 KB |
testcase_19 | AC | 2,614 ms
31,820 KB |
testcase_20 | AC | 2,607 ms
31,876 KB |
testcase_21 | AC | 2,587 ms
31,784 KB |
testcase_22 | AC | 1,736 ms
31,868 KB |
testcase_23 | AC | 1,691 ms
31,792 KB |
testcase_24 | AC | 2,510 ms
29,972 KB |
testcase_25 | AC | 2,518 ms
30,008 KB |
testcase_26 | AC | 2,532 ms
30,048 KB |
testcase_27 | AC | 884 ms
15,236 KB |
testcase_28 | AC | 755 ms
15,480 KB |
testcase_29 | AC | 1,729 ms
26,800 KB |
testcase_30 | AC | 803 ms
14,616 KB |
testcase_31 | AC | 75 ms
6,940 KB |
testcase_32 | AC | 1,249 ms
28,704 KB |
testcase_33 | AC | 448 ms
16,208 KB |
testcase_34 | AC | 2,462 ms
31,672 KB |
testcase_35 | AC | 2,444 ms
32,176 KB |
testcase_36 | AC | 2,462 ms
31,544 KB |
ソースコード
#include <iostream> #include <vector> #include <set> #include <cmath> #include <cassert> using namespace std; class DSU { public: int n; vector<int> parent_or_size; DSU(int N) { n = N; parent_or_size.resize(N, -1); } int leader(int a) { assert(0 <= a && a < n); if (parent_or_size[a] < 0) { return a; } return parent_or_size[a] = leader(parent_or_size[a]); } int merge(int a, int b) { assert(0 <= a && a < n); assert(0 <= b && b < n); int x = leader(a); int y = leader(b); if (x == y) { return x; } if (-parent_or_size[x] < -parent_or_size[y]) { swap(x, y); } parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < n); assert(0 <= b && b < n); return leader(a) == leader(b); } int size(int a) { assert(0 <= a && a < n); return -parent_or_size[leader(a)]; } vector<vector<int>> groups() { vector<int> leader_buf(n); vector<int> group_size(n, 0); for (int i = 0; i < n; ++i) { leader_buf[i] = leader(i); ++group_size[leader_buf[i]]; } vector<vector<int>> result(n); for (int i = 0; i < n; ++i) { result[leader_buf[i]].push_back(i); } vector<vector<int>> result2; for (int i = 0; i < n; ++i) { if (!result[i].empty()) { result2.push_back(result[i]); } } return result2; } }; int main() { 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]; } } set<int> C; vector<pair<int, int>> q; int Q; cin >> Q; for (int i = 0; i < Q; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; q.push_back({(a - 1) * W + (b - 1), (c - 1) * W + (d - 1)}); C.insert(i); } vector<vector<pair<int, int>>> L(H * W + 1); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (i < H - 1) { int x = A[i][j]; int y = A[i + 1][j]; int z = max(x, y); L[z].push_back({i * W + j, (i + 1) * W + j}); } if (j < W - 1) { int x = A[i][j]; int y = A[i][j + 1]; int z = max(x, y); L[z].push_back({i * W + j, i * W + j + 1}); } } } vector<int> result(Q, 0); DSU Z(H * W); int k = static_cast<int>(sqrt(H * W)); int M = (H * W + k - 1) / k; vector<vector<int>> D(M + 1); vector<bool> ans(Q, false); for (int i = 0; i <= M; ++i) { int l = i * k; int r = (i + 1) * k; for (int j = 0; j < k; ++j) { int z = i * k + j; if (z > H * W) { break; } for (auto& B : L[z]) { int pos1 = B.first, pos2 = B.second; Z.merge(pos1, pos2); } } for (int j = 0; j < Q; ++j) { int p1 = q[j].first, p2 = q[j].second; if (ans[j]) { continue; } if (Z.same(p1, p2)) { ans[j] = true; D[i].push_back(j); } } } Z = DSU(H * W); fill(ans.begin(), ans.end(), false); for (int i = 0; i <= M; ++i) { for (int j = 0; j < k; ++j) { int z = i * k + j; if (z > H * W) { break; } for (auto& B : L[z]) { int pos1 = B.first, pos2 = B.second; Z.merge(pos1, pos2); } if (!L[z].empty()) { for (int idx : D[i]) { int p1 = q[idx].first, p2 = q[idx].second; if (ans[idx]) { continue; } if (Z.same(p1, p2)) { ans[idx] = true; result[idx] = z; } } } } } for (int i = 0; i < Q; ++i) { cout << result[i] << endl; } return 0; }