Cod sursa(job #3137226)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 11 iunie 2023 18:20:26
Problema Matrice 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.17 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
using pii = pair<int,int>;

ifstream cin("matrice2.in");
ofstream cout("matrice2.out");

const int MAX = 3e2 + 1;

int dl[] = {0,0,-1,1};
int dc[] = {1,-1,0,0};

int mat[MAX][MAX], n, x, q, y , xx , yy;

bool activ[MAX][MAX];

vector <pii> v;

struct qry{ int sx , sy , fx , fy, ind , rez=0;};

vector <qry> querys;

bool crit( pii a , pii b){

    return (mat[a.first][a.second] > mat[b.first][b.second]);
}

bool criteriu( qry a , qry b ){

    return (a.rez < b.rez);
}

bool criterius( qry a , qry b){

    return (a.ind < b.ind);
}

bool inmat(int &x , int &y){

    return(x>=1&&x<=n&&y>=1&&y<=n);
}

struct dsu{

    pii t[MAX][MAX];

    void init(){

        for(int i = 1 ; i <= n ; i++){

            for(int j = 1 ; j <= n ; j++){

                activ[i][j] = 0;
                t[i][j].first = 0;
            }
        }
    }

    pii findc( int x , int y){

        int rx = x , ry = y , auxx , auxy;
        while(t[rx][ry].first){

            auxx = t[rx][ry].first;
            auxy = t[rx][ry].second;
            rx = auxx;
            ry = auxy;
        }

        while(t[x][y].first){
            auxx = t[x][y].first;
            auxy = t[x][y].second;
            t[x][y].first = rx;
            t[x][y].second = ry;
            x = auxx;
            y = auxy;
        }

        return make_pair(rx,ry);
    }

    void unionp(int &l , int &c){

        activ[l][c] = 1;
        for(int i = 0 ; i < 4 ; i++){

            int ll = l + dl[i];
            int cc = c + dc[i];

            if(!inmat(ll,cc) || !activ[ll][cc]) continue;

            pii r = findc(l,c);
            pii rl = findc(ll,cc);

            if(r.first==rl.first&&r.second==rl.second) continue;

            t[r.first][r.second] = rl;
        }
    }
}uf;


signed main()
{
    cin >> n >> q;

    for(int i = 1 ; i <= n ; i++){

        for(int j = 1 ; j <= n ; j++){

            cin >> mat[i][j];
            v.push_back({i,j});
        }
    }

    sort(v.begin(),v.end(),crit);

    for(int i = 1 ; i <= q ; i++){

        cin >> x >> y >> xx >> yy;
        querys.push_back({x,y,xx,yy,i});
    }

    int pw = 0;
    while((1<<pw)<=(n*n))pw++;
    pw--;

    while(pw>=0){

        uf.init();

        sort(querys.begin(),querys.end(),criteriu);

        int tp = (1<<pw);

        int poz = 0;

        for(int i = 0 ; i < q ; i++){

            if(querys[i].rez + tp > n*n) break;

            for(;poz+1 <= querys[i].rez + tp ; poz++){

                uf.unionp(v[poz].first,v[poz].second);
            }

            pii fst = uf.findc(querys[i].sx,querys[i].sy);
            pii scd = uf.findc(querys[i].fx,querys[i].fy);

            if(fst.first==scd.first && fst.second==scd.second){

                continue;
            }

            querys[i].rez += tp;
        }

        pw--;
    }

    sort(querys.begin(),querys.end(),criterius);

    for(auto it : querys){

        cout << mat[v[it.rez].first][v[it.rez].second] << '\n';
    }

    return 0;
}