Cod sursa(job #2784087)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 15 octombrie 2021 18:41:45
Problema Matrice 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("matrice2.in");
ofstream fout("matrice2.out");

const int NMAX(305), QMAX(20005);
vector<pair<int, int> > vec[NMAX * NMAX];
pair<short, short> ve[NMAX * NMAX];

int cnt, cnvB[NMAX * NMAX], X1, Y1, X2, Y2, n, q, nrP, mat[NMAX][NMAX], aux[NMAX][NMAX], rez[QMAX];
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};


class DSU
{
public:
    vector<int> p, rnk;
    DSU(int n)
    {
        p.resize(n + 1), rnk.resize(n + 1);
        for(int i = 1; i <= n; ++i)
        {
            p[i] = i;
            rnk[i] = 0;
        }
    }
    int findR(int x)
    {
        if(x == p[x])
            return x;
        return p[x] = findR(p[x]);
    }
    void Unite(int x, int y)
    {
        x = findR(x);
        y = findR(y);
        if(x == y)
            return;
        if(rnk[x] > rnk[y])
            swap(x, y);
        p[x] = y;
        rnk[y] += rnk[x];
    }
};

inline int getId(int x, int y)
{
    return (x - 1) * n + y;
}
bool cmp(const pair<short, short> &x, const pair<short, short> &y) {
  return mat[x.first][x.second] < mat[y.first][y.second];
}

int main()
{
    fin >> n >> q;

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
        {
            fin >> mat[i][j];
            ve[getId(i, j)] = {i, j};
        }

    sort(ve + 1, ve + n * n + 1, cmp);
    int last = 0;
    for (int i = 1; i <= n * n; ++i)
    {
        int l, c;
        tie(l, c) = ve[i];
        if (mat[l][c] != last)
        {
            cnvB[++cnt] = mat[l][c];
            last = mat[l][c];
        }
        vec[cnt].emplace_back(l, c);
    }

    vector<pair<int, int > > query;
    query.push_back({0, 0});
    for(int i = 1; i <= q; ++i)
    {
        fin >> X1 >> Y1 >> X2 >> Y2;
        query.push_back({getId(X1, Y1), getId(X2, Y2)});
    }

    nrP = 0;
    int step = 1;
    for(; step < cnt; step <<= 1);
    while(step)
    {
        vector<int> aq(q + 1, 0), parcg[cnt + 1];
        for(int i = 1; i <= q; ++i)
            if(rez[i] + step <= cnt)
            {
                aq[i] = rez[i] + step;
                parcg[aq[i]].push_back(i);
            }
        DSU Arb(n * n);
        ++nrP;
        for(int i = cnt; i >= 1; --i)
        {
            for(auto it: vec[i])
            {
                aux[it.first][it.second] = nrP;
                for(int k = 0; k < 4; ++k)
                {
                    int newX = it.first + dx[k];
                    int newY = it.second + dy[k];
                    if(aux[newX][newY] == nrP)
                        Arb.Unite(getId(it.first, it.second), getId(newX, newY));
                }
            }
            for(auto it: parcg[i])
                if(Arb.findR(query[it].first) == Arb.findR(query[it].second))
                    rez[it] = aq[it];
        }
        step >>= 1;
    }
    for(int i = 1; i <= q; ++i)
        fout << cnvB[rez[i]] << '\n';
    return 0;
}