Cod sursa(job #2026517)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 24 septembrie 2017 15:50:42
Problema Plantatie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <bits/stdc++.h>
#define Nmax 501
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int arb[6*Nmax*Nmax];
int a[Nmax][Nmax];
int a1,b1,a2,b2;
void update(int x1, int y1, int x2, int y2, int poz)
{
    if(x1==x2 and y1==y2)
        arb[poz]=a[x1][y1];
    else
    {
        int m1,m2;
        m1=(x1+x2)/2;
        m2=(y1+y2)/2;
        if(x1<x2 and y1<y2)
        {
            update(x1,y1,m1,m2,4*poz-2);
            update(x1,m2+1,m1,y2,4*poz-1);
            update(m1+1,y1,x2,m2,4*poz);
            update(m1+1,m2+1,x2,y2,4*poz+1);
            arb[poz]=max(max(arb[4*poz-2],arb[4*poz-1]),max(arb[4*poz],arb[4*poz+1]));
        }
        else
        {
            if(x1==x2)
            {
                update(x1,y1,x1,m2,4*poz-2);
                update(x1,m2+1,x1,y2,4*poz-1);
                arb[poz]=max(arb[4*poz-2],arb[4*poz-1]);
            }
            else
            {
                update(x1,y1,m1,y1,4*poz);
                update(m1+1,y1,x2,y1,4*poz+1);
                arb[poz]=max(arb[4*poz],arb[4*poz+1]);
            }
        }
    }
}
int querry(int x1, int y1, int x2, int y2, int poz)
{
    if(a1<=x1 and b1<=y1 and a2>=x2 and b2>=y2) return arb[poz];
    else
    {
        int m1,m2,ans1,ans2,ans3,ans4;
        ans1=ans2=ans3=ans4=0;
        m1=(x1+x2)/2;
        m2=(y1+y2)/2;
        if(x1<x2 and y1<y2)
        {
            if(a1<=m1)
            {
                if(b1<=m2) ans1=querry(x1,y1,m1,m2,4*poz-2);
                if(m2<b2) ans2=querry(x1,m2+1,m1,y2,4*poz-1);
            }
            if(m1<a2)
            {
                if(b1<=m2) ans3=querry(m1+1,y1,x2,m2,4*poz);
                if(m2<b2) ans4=querry(m1+1,m2+1,x2,y2,4*poz+1);
            }
        }
        else
        {
            if(x1==x2)
            {
                if(b1<=m2) ans1=querry(x1,y1,x1,m2,4*poz-2);
                if(m2<b2) ans2=querry(x1,m2+1,x1,y2,4*poz-1);
            }
            else
            {
                if(a1<=m1) ans1=querry(x1,y1,m1,y1,4*poz);
                if(m1<a2) ans2=querry(m1+1,y1,x2,y1,4*poz+1);
            }
        }
        return max(max(ans1,ans2),max(ans3,ans4));
    }
}
int main()
{
    int n,i,j,m,k;
    f>>n>>m;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        f>>a[i][j];
    update(1,1,n,n,1);
    for(;m;--m)
    {
        f>>a1>>b1>>k;
        a2=a1+k-1;
        b2=b1+k-1;
        g<<querry(1,1,n,n,1)<<'\n';
    }

    return 0;
}