Cod sursa(job #2292324)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 29 noiembrie 2018 13:33:52
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
int n,m,i,j,d[20][510][510],pow,x1,x2,y1,y2,lat,maxim,p,k,log,max1,max2;
int l[510];
int main() {
	fin>>n>>m;
	for (i=2;i<=n;i++)
		l[i]=1+l[i/2];
	for (i=1;i<=n;i++)
		for (j=1;j<=n;j++)
			fin>>d[0][i][j];
	for (k=1;k<=l[n];k++) {
		pow=(1<<(k-1));
		for (i=1;i<=n-pow;i++) {
			for (j=1;j<=n-pow;j++) {
				max1=max(d[k-1][i][j],d[k-1][i+pow][j]);
				max2=max(d[k-1][i][j+pow],d[k-1][i+pow][j+pow]);
				d[k][i][j]=max(max1,max2);
			}
		}
	}
	for (i=1;i<=m;i++) {
		fin>>x1>>y1>>lat;
		x2=x1+lat-1, y2=y1+lat-1;
		log=l[lat];
		p=(1<<log);
		max1=max(d[log][x1][y1],d[log][x2-p+1][y1]);
		max2=max(d[log][x1][y2-p+1],d[log][x2-p+1][y2-p+1]);
		maxim=max(max1,max2);
		fout<<maxim<<"\n";
	}
	return 0;
}