結果
| 問題 | 
                            No.2786 RMQ on Grid Path
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-06-15 00:41:22 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 3,196 bytes | 
| コンパイル時間 | 2,715 ms | 
| コンパイル使用メモリ | 162,612 KB | 
| 実行使用メモリ | 266,032 KB | 
| 最終ジャッジ日時 | 2024-06-15 00:41:35 | 
| 合計ジャッジ時間 | 12,446 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 10 TLE * 1 -- * 24 | 
ソースコード
#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) ;
// #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)};
  }
  vector<UF> ufs;
  vector<bitset<500 * 500>> fs;
  UF uf(h * w);
  bitset<500 * 500> filled;
  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 = 1000;
  for (int k : range(h * w)) {
    if (k % C == 0) {
      ufs.push_back(uf);
      fs.push_back(filled);
    }
    int i = valpos[k].second / w;
    int j = valpos[k].second % w;
    dump(k << " " << i << " " << j);
    fillin(i, j);
  }
  dump("A");
  int q; cin >> q;
  while (q--) {
    int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2;
    r1--; c1--;
    r2--; c2--;
    int p = 0;
    for (; p < (int)ufs.size() && !ufs[p].find(d(r1, c1), d(r2, c2)); p++);
    p--;
    dump(p);
    uf = ufs[p];
    filled = fs[p];
    for (int k = p * h; ; k++) {
      int i = valpos[k].second / w;
      int j = valpos[k].second % w;
      fillin(i, j);
      if (uf.find(d(r1, c1), d(r2, c2))) {
        cout << valpos[k].first << endl;
        break;
      }
    }
  }
}
int main() {
  cout << fixed << setprecision(12);
  solve();
}