122552024-12-10 17:09:35horkaHegyi levegőcpp17Wrong answer 0/100335ms28988 KiB
// UUID: 9ed7f0c2-c1b2-4e4a-9c7d-da4b49fec435
#include <bits/stdc++.h>
using namespace std;
const int c=50005,k=20;
int n,m;
int be[c],ki[c],lift_cs[c][k],lift_ert[c][k],fonok[c],mer[c],val[c];
vector<int> adj[c];
int holvan(int x)
{
	return (fonok[x]==x?x:fonok[x]=holvan(fonok[x]));
}
bool lehet=1;
void unio(int a, int b)
{
	lehet=1;
	a=holvan(a),b=holvan(b);
	if(a==b)
	{
		lehet=0;
		return;
	}
	if(mer[a]>mer[b]) swap(a,b);
	mer[b]+=mer[a];
	fonok[a]=b;
}
int ido;
void dfs(int cs, int p)
{
	be[cs]=++ido;
	lift_ert[cs][0]=val[cs];
	lift_cs[cs][0]=p;
	for(int &i:adj[cs])
	{
		if(!be[i]) dfs(i,cs);
	}
	ki[cs]=++ido;
}
bool ose(int a, int b)
{
	if(a==-1) return 1;
	if(be[a]<=be[b] && ki[a]>=ki[b]) return 1;
	return 0;
}
int lca(int a, int b)
{
	if(ose(a,b)) return a;
	if(ose(b,a)) return b;
	for(int j=k-1; j>=0; j--)
	{
		if(!ose(lift_cs[a][j],b)) a=lift_cs[a][j];
	}
	return lift_cs[a][0];
}
int maxi(int a, int x)
{
	//a-rol x-re felmesz mennyi a max el addig
	if(a==x) return lift_ert[a][0];
	int mx=0;
	for(int j=k-1; j>=0; j--)
	{
		if(!ose(lift_cs[a][j],x))
		{
			mx=max(mx,lift_ert[a][j]);
			a=lift_cs[a][j];
		}
	}
	mx=max(mx,lift_ert[a][0]);
	return mx;

}
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);		
	cin>>n>>m;
	int q; cin>>q;
	vector<vector<int>> v(n, vector<int> (m)); //n*i+j
	for(int i=0; i<n; i++)
		for(int j=0; j<m; j++)
		{
			cin>>v[i][j];
			val[i*n+j]=v[i][j];
		}
	vector<array<int, 3>> elek;
	for(int i=0; i<n; i++)
		for(int j=0; j<m; j++)
		{
			if(i>0)
			{
				int a=i*n+j,b=(i-1)*n+j;
				int w=max(v[i][j],v[i-1][j]);
				elek.push_back({w,a,b});
			}
			if(j>0)
			{
				int a=i*n+j,b=i*n+j-1;
				int w=max(v[i][j],v[i][j-1]);
				elek.push_back({w,a,b});
			}
		}
	int ossz=n*m;
	for(int i=0; i<ossz; i++)
		mer[i]=1,fonok[i]=i;
	sort(elek.begin(),elek.end());
	for(auto [w,a,b]:elek)
	{
		unio(a,b);
		if(lehet)
		{
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
	}
	dfs(0,-1);
	for(int j=1; j<k; j++)
		for(int i=0; i<ossz; i++)
		{
			if(lift_cs[i][j-1]==-1) lift_cs[i][j]=-1;
			else lift_cs[i][j]=lift_cs[lift_cs[i][j-1]][j-1];
			lift_ert[i][j]=max(lift_ert[i][j-1],(lift_cs[i][j-1]==-1?0:lift_ert[lift_cs[i][j-1]][j-1]));

		}
	while(q--)
	{
		int a,b,c,d; cin>>a>>b>>c>>d;
		a--,b--,c--,d--;
		a=a*n+b;
		b=c*n+d;
		int x=lca(a,b);
		cout<<max(maxi(a,x),maxi(b,x))<<"\n";
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms1772 KiB
2Accepted2ms1592 KiB
subtask20/19
3Accepted7ms2872 KiB
4Wrong answer7ms2568 KiB
5Wrong answer6ms2792 KiB
6Wrong answer7ms2556 KiB
7Accepted8ms2724 KiB
8Wrong answer7ms2812 KiB
9Runtime error8ms6652 KiB
10Runtime error4ms3640 KiB
subtask30/20
11Accepted2ms1592 KiB
12Accepted4ms1848 KiB
13Accepted17ms4416 KiB
14Runtime error37ms4132 KiB
15Runtime error184ms14780 KiB
subtask40/20
16Runtime error165ms14736 KiB
17Runtime error43ms28988 KiB
18Runtime error308ms23676 KiB
19Runtime error319ms23948 KiB
20Runtime error321ms24200 KiB
subtask50/31
21Runtime error9ms7224 KiB
22Runtime error65ms13172 KiB
23Runtime error68ms13320 KiB
24Runtime error71ms12944 KiB
25Runtime error37ms4140 KiB
26Runtime error52ms5484 KiB
27Runtime error61ms5424 KiB
28Runtime error35ms4100 KiB
29Runtime error46ms12516 KiB
subtask60/10
30Runtime error43ms28968 KiB
31Runtime error310ms23492 KiB
32Runtime error321ms23948 KiB
33Runtime error335ms24120 KiB
34Runtime error188ms14732 KiB
35Runtime error263ms22688 KiB
36Runtime error266ms17816 KiB
37Runtime error307ms17816 KiB
38Runtime error182ms14736 KiB
39Runtime error190ms22592 KiB