122602024-12-10 17:24:25horkaHegyi levegőcpp17Wrong answer 20/1001.761s152712 KiB
// UUID: 9ed7f0c2-c1b2-4e4a-9c7d-da4b49fec435
#include <bits/stdc++.h>
using namespace std;
const int c=500005,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][1]);
	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 || lift_cs[lift_cs[i][j-1]][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
1Accepted12ms12276 KiB
2Accepted9ms12088 KiB
subtask20/19
3Accepted17ms13404 KiB
4Wrong answer14ms13356 KiB
5Wrong answer17ms13304 KiB
6Wrong answer14ms13148 KiB
7Accepted18ms13308 KiB
8Wrong answer16ms13496 KiB
9Runtime error18ms20732 KiB
10Runtime error28ms30584 KiB
subtask320/20
11Accepted13ms12088 KiB
12Accepted12ms12344 KiB
13Accepted28ms14872 KiB
14Accepted229ms40232 KiB
15Accepted1.001s149120 KiB
subtask40/20
16Accepted1.24s147844 KiB
17Runtime error46ms39976 KiB
18Wrong answer726ms110784 KiB
19Wrong answer778ms109452 KiB
20Wrong answer833ms119684 KiB
subtask50/31
21Runtime error21ms18744 KiB
22Wrong answer275ms33720 KiB
23Wrong answer270ms33692 KiB
24Wrong answer268ms33696 KiB
25Accepted442ms41024 KiB
26Accepted361ms36004 KiB
27Accepted300ms37540 KiB
28Accepted326ms41408 KiB
29Accepted250ms37344 KiB
subtask60/10
30Runtime error46ms40008 KiB
31Wrong answer1.161s114972 KiB
32Wrong answer1.029s113656 KiB
33Wrong answer1.312s123872 KiB
34Accepted1.725s152240 KiB
35Wrong answer1.679s129440 KiB
36Accepted1.383s128640 KiB
37Accepted1.179s132224 KiB
38Accepted1.761s152712 KiB
39Accepted1.174s132668 KiB