Pagini recente » Borderou de evaluare (job #2247149) | Borderou de evaluare (job #2247153) | Borderou de evaluare (job #2247152) | Cod sursa (job #2253233) | Cod sursa (job #3167023)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("matrice2.in");
ofstream fout("matrice2.out");
class DSU
{
public:
vector <int> t;
DSU(int n)
{
t.resize(90005);
}
int Find(int x)
{
int r = x, y;
while (t[r])
{
r = t[r];
}
while (x != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
void Union(int x, int y)
{
t[y] = x;
//cout << x << " " << y << "\n";
}
};
struct vrajeala
{
int i, j, val;
bool operator < (const vrajeala A)const
{
return val > A.val;
}
};
int n, q, sol[20005];
bitset <90005> viz;
vector <vrajeala> a;
set<pair <int, int> > Q[90005];
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int Liniarizare(int x, int y)
{
return (x - 1) * n + y;
}
bool OK(int i, int j)
{
return (1 <= i and i <= n and 1 <= j and j <= n);
}
int main()
{
int i, j, x, y, xs, ys, xf, yf, k, val, val2;
fin >> n >> q;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
fin >> x;
a.push_back({ i, j, x });
}
sort(a.begin(), a.end());
for (i = 1; i <= q; i++)
{
fin >> xs >> ys >> xf >> yf;
x = Liniarizare(xs, ys);
y = Liniarizare(xf, yf);
Q[x].insert({ y, i });
Q[y].insert({ x, i });
}
DSU tree(n + 5);
for (auto w : a)
{
val = Liniarizare(w.i, w.j);
viz[val] = 1;
for (k = 0; k <= 3; k++)
{
x = w.i + dx[k];
y = w.j + dy[k];
val2 = Liniarizare(x, y);
if (OK(x, y) and viz[val2] == 1)
{
val = tree.Find(val);
val2 = tree.Find(val2);
if (val != val2)
{
for (auto w2 : Q[val2])
{
if (tree.Find(w2.first) == val)
sol[w2.second] = w.val;
else
Q[val].insert(w2);
}
tree.Union(val, val2);
Q[val2].clear();
}
}
}
}
for (i = 1; i <= q; i++)
fout << sol[i] << "\n";
return 0;
}