結果

問題 No.2786 RMQ on Grid Path
ユーザー inksamuraiinksamurai
提出日時 2024-06-19 17:36:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,559 bytes
コンパイル時間 4,371 ms
コンパイル使用メモリ 278,040 KB
実行使用メモリ 40,140 KB
最終ジャッジ日時 2024-06-19 17:36:21
合計ジャッジ時間 13,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int) a.size()
#define all(a) a.begin(),a.end()
#define vec(...) vector<__VA_ARGS__>
#define _4c5UlRw ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

struct dsu{
	int _n;
	vi si,par,leb;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		leb=vi(_n,-1);
		rep(i,n){
			si[i]=1;
			par[i]=i;
		}	
	}
	int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
	bool same(int u,int v){return parent(u)==parent(v);}
	void merge(int u,int v){
		u=parent(u),v=parent(v);
		if(!same(u,v)){
			if(si[u]<si[v]) swap(u,v);
			leb[u]=max(leb[u],leb[v]);
			_n-=1;
			si[u]+=si[v];
			par[v]=u;
		}
	}
	int size(int v=-1){return v==-1?_n:si[parent(v)];}
};

void slv(){
	int h,w;
	cin>>h>>w;
	const int n=h*w;

	vec(vi) a(h,vi(w));
	rep(i,h){
		rep(j,w){
			cin>>a[i][j];
		}
	}

	vi tmp;
	rep(i,h){
		rep(j,w){
			tmp.pb(a[i][j]);
		}
	}
	sort(all(tmp));
	tmp.erase(unique(all(tmp)),tmp.end());
	const int si=sz(tmp);

	const int di[]={1,-1,0,0};
	const int dj[]={0,0,1,-1};
	auto ok=[&](int i,int j)->int{
		return min(i,j)>=0 and i<h and j<w;
	};
	vec(vec(pii)) es(si);
	rep(i,h){
		rep(j,w){
			rep(dir,4){
				int ni=i+di[dir],nj=j+dj[dir];
				if(!ok(ni,nj)) continue;
				int id=max(a[i][j],a[ni][nj]);
				id=lower_bound(all(tmp),id)-tmp.begin();
				es[id].pb(minmax(i*w+j,ni*w+nj));
			}
		}
	}
	rep(i,si){
		sort(all(es[i]));
		es[i].erase(unique(all(es[i])),es[i].end());
	}

	int q;
	cin>>q;
	vec(array<int,4>) qry;
	rep(i,q){
		int si,sj,ti,tj;
		cin>>si>>sj>>ti>>tj;
		si-=1,sj-=1,ti-=1,tj-=1;
		qry.pb({si,sj,ti,tj});
	}

	vi pns(q);
	vi qsl(q),qsr(q,si-1);
	while(1){
		vec(vi) adqry(si);
		bool fnd=0;
		rep(i,q){
			if(qsl[i]<=qsr[i]){
				fnd=1;
				int md=(qsl[i]+qsl[i])/2;
				adqry[md].pb(i);
			}
		}
		if(!fnd) break;
		dsu uf(n);
		rep(i,si){
			for(auto [u,v]:es[i]){
				// if(!i) print(u,v);
				uf.merge(u,v);
			}
			for(auto id:adqry[i]){
				auto [si,sj,ti,tj]=qry[id];
				int u=si*w+sj,v=ti*w+tj;
				if(uf.same(u,v)){
					pns[id]=i;
					qsr[id]=i-1;
				}else{
					qsl[id]=i+1;
				}
			}
		}
	}

	rep(i,q){
		cout<<tmp[pns[i]]<<"\n";
	}
}

signed main(){
_4c5UlRw;
	slv();
}
0