Cod sursa(job #1314329)

Utilizator xtreme77Patrick Sava xtreme77 Data 11 ianuarie 2015 19:26:33
Problema Matrice 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.5 kb
/*
 * Code by Spiromanul
 */

#include <fstream>
#include <algorithm>

const char IN [ ] = "matrice2.in" ;
const char OUT [ ] = "matrice2.out" ;
const int MAX_MAT = 333 ;
const int MAXQ = 22222 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

struct matrrr {
    int x , y , val ;
};

matrrr a [ MAX_MAT * MAX_MAT ] ;

bool cmp ( matrrr a , matrrr b )
{
    return a.val > b.val ;
}

struct queries {
    int x1 , x2 ;
    int y1 , y2 ;
    int ind ;
    int sol ;
    void sett ( int unu , int doi , int trei , int patru , int cinci )
    {
        x1 = unu ;
        y1 = doi ;
        x2 = trei ;
        y2 = patru ;
        ind = cinci ;
    }
};

queries q [ MAXQ ] ;

bool compare_bin ( queries a , queries b )
{
    return a.sol > b.sol ;
}

bool cmp_afis ( queries a , queries b )
{
    return a.ind < b.ind ;
}

int tata [ MAX_MAT * MAX_MAT ] , rang [ MAX_MAT * MAX_MAT ] ;

inline int stramos ( int nod )
{
    int R = nod ;
    for ( ; tata [ R ] != R ; R = tata [ R ] ) ;
    while ( nod != tata [ nod ] )
    {
        int aux = tata [ nod ] ;
        tata [ nod ] = R ;
        nod = aux ;
    }
    return R ;
}

inline void unite ( int a , int b )
{
    a = stramos ( a ) ;
    b = stramos ( b ) ;
    if ( a == b )
        return ;
    if ( rang [ a ] > rang [ b ] )
    {
        rang [ a ] += rang [ b ] ;
        tata [ b ] = a ;
    }
    else {
        rang [ b ] += rang [ a ] ;
        tata [ a ] = b ;
    }
}

int code [ MAX_MAT ] [ MAX_MAT ] ;

int dx [ ] = { 1 , - 1 , 0 , 0 } ;
int dy [ ] = { 0 , 0 , - 1 , 1 } ;

inline void four_shoots ( int x , int y , int n )
{
    tata [ code [ x ] [ y ] ] = code [ x ] [ y ] ;
    for ( int i = 0 ; i <= 3 ; ++ i )
    {
        int tempx = x + dx [ i ] ;
        int tempy = y + dy [ i ] ;
        if ( tata [ code [ tempx ] [ tempy ] ] == 0 ) continue ;
        unite ( code [ tempx ] [ tempy ] , code [ x ] [ y ] ) ;
    }
}

inline void ANS ( int n , int m )
{
    sort ( a + 1 , a + n * n + 1 , cmp ) ;
    for ( int i = 30 ; i >= 0 ; -- i )
    {
        for ( int j = 1 ; j <= n * n ; ++ j )
        {
            tata [ j ] = 0 ;
            rang [ j ] = 1 ;
        }
        sort ( q + 1 , q + m + 1 , compare_bin ) ;
        int indice = 1 ;
        for ( int j = 1 ; j <= m ; ++ j )
        {
            for ( ; q [ j ].sol + ( 1 << i ) <= a [ indice ].val and indice <= n * n ; ++ indice ) four_shoots( a [ indice ].x , a [ indice ].y , n ) ;
            int colt1 = stramos ( code [ q [ j ].x1 ] [ q [ j ].y1 ] ) ;
            int colt2 = stramos ( code [ q [ j ].x2 ] [ q [ j ].y2 ] ) ;
            if ( colt1 == colt2 and colt1 and colt2  )
                q [ j ].sol += ( 1 << i ) ;
        }
    }
    sort ( q + 1 , q + m + 1 , cmp_afis ) ;
}

int main(  )
{
    int n , m ;
    fin >> n >> m ;
    for ( int i = 1 ; i <= n ; ++ i )
        for ( int j = 1 ; j <= n ; ++ j )
        {
            code [ i ] [ j ] = ( i - 1 ) * n + j ;
            a [ code [ i ] [ j ] ].x = i ;
            a [ code [ i ] [ j ] ].y = j ;
            fin >> a [ code [ i ] [ j ] ].val ;
        }
    for ( int i = 1 ; i <= m ; ++ i )
    {
        int unu , doi , trei , patru , cinci ;
        fin >> unu >> doi >> trei >> patru ;
        cinci = i ;
        q [ i ].sett ( unu , doi , trei , patru , cinci ) ;
    }
    ANS ( n , m ) ;
    for ( int i = 1 ; i <= m ; ++ i )
        fout << q [ i ].sol << '\n' ;
    return 0;
}