Pagini recente » Cod sursa (job #973728) | Cod sursa (job #1571057) | Istoria paginii runda/testuletzzzz | Cod sursa (job #2333814) | Cod sursa (job #2790302)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
struct matrix
{
int l;
int c;
};
int latura, nr_query;
int teren[500][500];
matrix tabel[9][500][500];
int i, j, k;
void gen_tabel();
int rasp_query(int x, int y, int lat);
int main()
{
fin >> latura >> nr_query;
for (i = 0; i < latura; i++)
for (j = 0; j < latura; j++)
fin >> teren[i][j], tabel[0][i][j].l = i, tabel[0][i][j].c = j;
gen_tabel();
int x, y, l;
for (k = 0; k < nr_query; k++)
{
fin >> x >> y >> l;
fout << rasp_query(x - 1, y - 1, l) << "\n";
}
return 0;
}
void gen_tabel()
{
int put;
for (put = 1; (1 << put) <= latura; put++)
{
for (i = 0; i + (1 << put) - 1 < latura; i++)
for (j = 0; j + (1 << put) - 1 < latura; j++)
{
int a, b, c, d;
a = teren[tabel[put - 1][i][j].l][tabel[put - 1][i][j].c];
b = teren[tabel[put - 1][i][j + (1 << put - 1)].l][tabel[put - 1][i][j + (1 << put - 1)].c];
c = teren[tabel[put - 1][i + (1 << put - 1)][j].l][tabel[put - 1][i + (1 << put - 1)][j].c];
d = teren[tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].l][tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].c];
if (a >= b && a >= c && a >= d)
tabel[put][i][j].l = tabel[put - 1][i][j].l, tabel[put][i][j].c = tabel[put - 1][i][j].c;
else if (b >= a && b >= c && b >= d)
tabel[put][i][j].l = tabel[put - 1][i][j + (1 << put - 1)].l, tabel[put][i][j].c = tabel[put - 1][i][j + (1 << put - 1)].c;
else if (c >= a && c >= b && c >= d)
tabel[put][i][j].l = tabel[put - 1][i + (1 << put - 1)][j].l, tabel[put][i][j].c = tabel[put - 1][i + (1 << put - 1)][j].c;
else
tabel[put][i][j].l = tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].l, tabel[put][i][j].c = tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].c;
}
}
return;
}
int rasp_query(int x, int y, int lat)
{
int put = log2(lat);
int dif = lat - (1 << put);
int a, b, c, d;
a = teren[tabel[put][x][y].l][tabel[put][x][y].c];
b = teren[tabel[put][x][y+dif].l][tabel[put][x][y+dif].c];
c = teren[tabel[put][x+dif][y].l][tabel[put][x+dif][y].c];
d = teren[tabel[put][x+dif][y+dif].l][tabel[put][x+dif][y+dif].c];
if (a >= b && a >= c && a >= d)
return a;
else if (b >= a && b >= c && b >= d)
return b;
else if (c >= a && c >= b && c >= d)
return c;
else
return d;
}