結果

問題 No.2786 RMQ on Grid Path
ユーザー shobonvip
提出日時 2024-06-14 21:50:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,865 ms / 6,000 ms
コード長 2,052 bytes
コンパイル時間 4,407 ms
コンパイル使用メモリ 260,280 KB
最終ジャッジ日時 2025-02-21 21:54:56
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

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

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

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int main(){
	int h, w; cin >> h >> w;
	vector a(h, vector<int>(w));
	rep(i,0,h)rep(j,0,w)cin>>a[i][j];
	vector bucket(h*w+1,vector<pair<int,int>>(0));
	rep(i,0,h)rep(j,0,w)bucket[a[i][j]].push_back(pair(i,j));
	int q;cin>>q;
	vector<int> sx(q),sy(q),gx(q),gy(q);
	rep(i,0,q){
		cin>>sx[i]>>sy[i]>>gx[i]>>gy[i];
		sx[i]--;sy[i]--;gx[i]--;gy[i]--;
	}
	vector<int> ub(q,h*w),lb(q,0);
	auto check=[&]()->bool {
		rep(i,0,q){
			if(ub[i]-lb[i]>1)return true;
		}
		return false;
	};
	while(check()){
		vector buckett(h*w+1,vector<int>(0));
		rep(i,0,q){
			int t=(ub[i]+lb[i])/2;
			buckett[t].push_back(i);
		}
		dsu uf(h*w);
		rep(t,0,h*w+1){
			for(auto[i,j]:bucket[t]){
				for(auto[x,y]:{
					pair(i+1,j),pair(i-1,j),pair(i,j-1),pair(i,j+1)
				}){
					if(!(0<=x&&x<h&&0<=y&&y<w))continue;
					if(a[x][y]<=t)uf.merge(x*w+y,i*w+j);
				}
			}
			for(int i:buckett[t]){
				if(uf.same(sx[i]*w+sy[i],gx[i]*w+gy[i])){
					ub[i]=t;
				}else{
					lb[i]=t;
				}
			}
		}
	}
	rep(i,0,q){
		cout<<ub[i]<<endl;
	}
}

0