Cod sursa(job #2717518)

Utilizator AACthAirinei Andrei Cristian AACth Data 7 martie 2021 15:38:02
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
const int Max = 501;
void nos()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int n,q;
int rmq[Max][32][Max];
void read()
{
    f>>n>>q;
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            f>>rmq[i][0][j];
}

void create_rmq(int which)
{
    int i,j;
    for(i = 1; (1<<i) <= n; ++i)
    {
        // pentru toate lungimile
        for(j=0; j + (1<<i) - 1 < n; ++j)
            rmq[which][i][j] = max(rmq[which][i-1][j], rmq[which][i-1][j + (1 << (i-1))]);
    }
}
int ans;

void ask_rmq(int which, int left, int dim)
{
    int half_log = log2(dim);
    int this_ans = max(rmq[which][half_log][left], rmq[which][half_log][left + dim - (1<<half_log)]);
    ans = max(ans,this_ans);
}

void solve()
{
    int i;
    for(i=0;i<n;i++)
        create_rmq(i);
    while(q--)
    {
        int xs,ys,dim;
        f>>xs>>ys>>dim;
        --xs,--ys,ans = 0;
        for(int j = xs;j<=xs + dim - 1; ++j)
            ask_rmq(j,ys,dim);
        g<<ans<<'\n';
    }
}
void restart()
{

}

int32_t main()
{
    nos();

        read();
        solve();
        restart();

    return 0;
}