122572024-12-10 17:11:15horkaHegyi levegőcpp17Wrong answer 20/1001.935s152492 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?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
1Accepted10ms12088 KiB
2Accepted9ms12088 KiB
subtask20/19
3Accepted14ms13460 KiB
4Wrong answer17ms13176 KiB
5Wrong answer14ms13264 KiB
6Wrong answer14ms13228 KiB
7Accepted18ms13324 KiB
8Wrong answer14ms13308 KiB
9Runtime error23ms20788 KiB
10Runtime error23ms30468 KiB
subtask320/20
11Accepted13ms12248 KiB
12Accepted12ms12344 KiB
13Accepted28ms14888 KiB
14Accepted246ms40232 KiB
15Accepted1.072s149036 KiB
subtask40/20
16Accepted1.034s147848 KiB
17Runtime error54ms39992 KiB
18Wrong answer796ms110788 KiB
19Wrong answer808ms109244 KiB
20Wrong answer990ms119748 KiB
subtask50/31
21Runtime error17ms18744 KiB
22Wrong answer277ms33720 KiB
23Wrong answer287ms33696 KiB
24Wrong answer280ms33696 KiB
25Accepted428ms41260 KiB
26Accepted330ms36004 KiB
27Accepted319ms37540 KiB
28Accepted395ms41508 KiB
29Accepted264ms37344 KiB
subtask60/10
30Runtime error46ms39736 KiB
31Wrong answer1.2s115136 KiB
32Wrong answer1.072s113548 KiB
33Wrong answer1.351s124028 KiB
34Accepted1.763s152200 KiB
35Wrong answer1.935s129340 KiB
36Accepted1.432s128644 KiB
37Accepted1.215s132228 KiB
38Accepted1.876s152492 KiB
39Accepted1.411s132452 KiB