Cod sursa(job #3150254)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 15 septembrie 2023 18:43:29
Problema Matrice 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.45 kb
#include <fstream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

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

struct str
{
    int x, y, val;
    bool operator < (const str & aux) const
    {
        return val > aux.val;
    }
};

struct DSU
{
    int fat;
    set <pair <int, int>> qr;
};

const int max_size = 3e2 + 1, max_q = 2e4 + 1;

int a[max_size][max_size], n, ans[max_q];
bool activ[max_size][max_size];
vector <str> valz;
DSU t[max_size * max_size];

int getpoz (int x, int y)
{
    return (x - 1) * n + y;
}

int rad (int x)
{
    if (t[x].fat == x)
    {
        return x;
    }
    return t[x].fat = rad(t[x].fat);
}

int main ()
{
    int q;
    in >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            in >> a[i][j];
            valz.push_back({i, j, a[i][j]});
            t[getpoz(i, j)].fat = getpoz(i, j);
        }
    }
    sort(valz.begin(), valz.end());
    for (int i = 1; i <= q; i++)
    {
        int x1, y1, x2, y2;
        in >> x1 >> y1 >> x2 >> y2;
        int x = getpoz(x1, y1), y = getpoz(x2, y2);
        t[x].qr.insert({y, i});
        t[y].qr.insert({x, i});
    }
    for (auto f : valz)
    {
        activ[f.x][f.y] = 1;
        int x = getpoz(f.x, f.y);
        if (f.x > 1 && activ[f.x - 1][f.y] == 1)
        {
            int y = getpoz(f.x - 1, f.y), rx = rad(x), ry = rad(y);
            if (rx != ry)
            {
                if (t[rx].qr.size() < t[ry].qr.size())
                {
                    swap(rx, ry);
                }
                for (auto it : t[ry].qr)
                {
                    if (rad(it.first) == rx)
                    {
                        ans[it.second] = min(a[f.x][f.y], a[f.x - 1][f.y]);
                    }
                    else
                    {
                        t[rx].qr.insert(it);
                    }
                }
                t[ry].fat = rx;
                t[ry].qr.clear();
            }
        }
        if (f.y > 1 && activ[f.x][f.y - 1] == 1)
        {
            int y = getpoz(f.x, f.y - 1), rx = rad(x), ry = rad(y);
            if (rx != ry)
            {
                if (t[rx].qr.size() < t[ry].qr.size())
                {
                    swap(rx, ry);
                }
                for (auto it : t[ry].qr)
                {
                    if (rad(it.first) == rx)
                    {
                        ans[it.second] = min(a[f.x][f.y], a[f.x][f.y - 1]);
                    }
                    else
                    {
                        t[rx].qr.insert(it);
                    }
                }
                t[ry].fat = rx;
                t[ry].qr.clear();
            }
        }
        if (f.x < n && activ[f.x + 1][f.y] == 1)
        {
            int y = getpoz(f.x + 1, f.y), rx = rad(x), ry = rad(y);
            if (rx != ry)
            {
                if (t[rx].qr.size() < t[ry].qr.size())
                {
                    swap(rx, ry);
                }
                for (auto it : t[ry].qr)
                {
                    if (rad(it.first) == rx)
                    {
                        ans[it.second] = min(a[f.x][f.y], a[f.x + 1][f.y]);
                    }
                    else
                    {
                        t[rx].qr.insert(it);
                    }
                }
                t[ry].fat = rx;
                t[ry].qr.clear();
            }
        }
        if (f.y < n && activ[f.x][f.y + 1] == 1)
        {
            int y = getpoz(f.x, f.y + 1), rx = rad(x), ry = rad(y);
            if (rx != ry)
            {
                if (t[rx].qr.size() < t[ry].qr.size())
                {
                    swap(rx, ry);
                }
                for (auto it : t[ry].qr)
                {
                    if (rad(it.first) == rx)
                    {
                        ans[it.second] = min(a[f.x][f.y], a[f.x][f.y + 1]);
                    }
                    else
                    {
                        t[rx].qr.insert(it);
                    }
                }
                t[ry].fat = rx;
                t[ry].qr.clear();
            }
        }
    }
    for (int i = 1; i <= q; i++)
    {
        out << ans[i] << '\n';
    }
    in.close();
    out.close();
    return 0;
}