Pagini recente » Cod sursa (job #789419) | Cod sursa (job #2179107) | Cod sursa (job #1376635) | Cod sursa (job #219284) | Cod sursa (job #1048763)
//
// main.cpp
// rmq
//
// Created by Catalina Brinza on 12/2/13.
// Copyright (c) 2013 Catalina Brinza. All rights reserved.
//
#include <fstream>
#include <math.h>
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int main()
{int rmq[501][501][17];
int n,i,x,m,y,l,ji,z,h;
f>>n>>m;
for(i=0;i<n;++i)
for (ji=0;ji<n;++ji)
f>>rmq[i][ji][0];
for (l=1;(1<<l)<=n;++l)
{
h=1<<(l-1);
for (i=0;i<n;++i)
{ for (ji=0;ji<n;++ji)
{
rmq[i][ji][l]=rmq[i][ji][l-1];
if (ji+h<=n && rmq[i][ji+h][l-1]> rmq[i][ji][l]) rmq[i][ji][l]=rmq[i][ji+h][l-1];
if (i+h<=n && rmq[i][ji][l]<rmq[i+h][ji][l-1]) rmq[i][ji][l]=rmq[i+h][ji][l-1];
if (i+h<=n && ji+h<=n && rmq[i][ji][l]<rmq[i+h][ji+h][l-1]) rmq[i][ji][l]=rmq[i+h][ji+h][l-1];
;
}
}}
for (i=0;i<m;++i)
{
f>>x>>y>>z;
x--;y--;
int l=log2(z);
x=rmq[x][y][l];
if (x<rmq[x+z-(1<<l)][y][l]) x=rmq[x+z-(1<<l)][y][l];
if (x<rmq[x][y+z-(1<<l)][l]) x=rmq[x][y+z-(1<<l)][l];
if (x<rmq[x+z-(1<<l)][y+z-(1<<l)][l]) x=rmq[x+z-(1<<l)][y+z-(1<<l)][l];
g<<x<<"\n";
}
return 0;
}