I Liked Matrix! [Simulation]

Description–

Given an n * m matrix A, its secondary query q: to (x1, y1) of the upper left corner, (x2, y2) for the lower right corner of the rectangular sub-
array, the maximum of all the elements.


Input–

The first line contains three integers n, m and q.
After n lines each containing m integers Ai, j.
After q lines contains four integers x1, y1, x2 and y2.

Output–

Comprises a total of q rows integer ans, it represents the maximum value of all elements in the sub-matrices.


Sample Input–

3 3 2
1 2 3
1 2 3
2 3 1
1 1 2 2
2 2 3 3

Sample Output–

2
3


Notes -

To 100% of the data: n, m, q ≤ 100


Code -

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,q,x1,x2,y1,y2,ans,a[105][105];
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for (int i=1;i<=n;++i)
	  for (int j=1;j<=m;++j)
	    scanf("%d",&a[i][j]);
	for (int k=1;k<=q;++k)
	{
		ans=0;
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		for (int i=x1;i<=x2;++i)
		  for (int j=y1;j<=y2;++j)
		    ans=max(ans,a[i][j]);
		printf("%d\n",ans);
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/90698814