結果

問題 No.2786 RMQ on Grid Path
ユーザー nwo
提出日時 2024-06-14 21:42:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,280 bytes
コンパイル時間 3,325 ms
コンパイル使用メモリ 257,204 KB
実行使用メモリ 820,828 KB
最終ジャッジ日時 2024-06-14 21:42:54
合計ジャッジ時間 12,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 MLE * 1 -- * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)

template<class T> bool chmax(T &a, T b)
{
	if(a<b)
	{
		a = b;
		return true;
	}
	return false;
}
template<class T> bool chmin(T &a, T b)
{
	if(a>b)
	{
		a = b;
		return true;
	}
	return false;
}

vector<int> dx = {1,0,-1,0};
vector<int> dy = {0,1,0,-1};
int H, W, a, b, c, d;
bool in(int h, int w)
{
	return (0<=h && h<H && 0<=w && w<W);
}
using P = pair<int,int>;
int main()
{
	cin >> H >> W;
	vector<vector<int>> A(H, vector<int>(W));
	rep(i,H) rep(j,W) cin >> A[i][j];
	int mx = 0;
	rep(i,H) rep(j,W) chmax(mx, A[i][j]);
	auto f = [&](int x) -> bool {
		if(A[a][b]>x || A[c][d]>x) return false;
		queue<P> q;
		q.push({a,b});
		vector<bitset<500>> seen(H);
		while(!q.empty())
		{
			auto [h,w] = q.front();
			q.pop();
			seen[h][w] = 1;
			rep(t,4)
			{
				int nh = h+dx[t];
				int nw = w+dy[t];
				if(in(nh,nw) && !seen[nh][nw] && A[nh][nw]<=x)
				{
					if(nh==c && nw==d) return true;
					q.push({nh,nw});
				}
			}
		}
		return seen[c][d];
	};
	int Q;
	cin >> Q;
	while(Q--)
	{
		cin >> a >> b >> c >> d;
		a--;
		b--;
		c--;
		d--;
		int l = 0;
		int r = mx;
		while(r-l>1)
		{
			int mid = (l+r)/2;
			if(f(mid)) r = mid;
			else l = mid;
		}
		cout << r << '\n';
	}
}
0