Cod sursa(job #3267635)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 11 ianuarie 2025 17:00:39
Problema Matrice 2 Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.61 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define fi first
#define sc second
#define pb push_back

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
template<typename type>
using ordered_set = tree<type, null_type, less<type>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 305, mod = 1e9 + 7, inf = 2e9;
const int dl[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
const int ddl[] = {-1, -1, -1, 0, 1, 1, 1, 0}, ddc[] = {-1, 0, 1, 1, 1, 0, -1, -1};

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
int rng(int lo = 1, int hi = INT_MAX) {
    uniform_int_distribution<int> rnd(lo, hi);
    return rnd(gen);
}

struct mint {
    int val;
    mint(int32_t x = 0) {
        val = x % mod;
    }
    mint(long long x) {
        val = x % mod;
    }
    mint operator+(mint x) {
        return val + x.val;
    }
    mint operator-(mint x) {
        return val - x.val + mod;
    }
    mint operator*(mint x) {
        return 1LL * val * x.val;
    }
    void operator+=(mint x) {
        val = (*this + x).val;
    }
    void operator-=(mint x) {
        val = (*this - x).val;
    }
    void operator*=(mint x) {
        val = (*this * x).val;
    }
    friend auto operator>>(istream& in, mint &x) -> istream& {
        in >> x.val;
        x.val %= mod;
        return in;
    }
    friend auto operator<<(ostream& out, mint const &x) -> ostream& {
        out << x.val;
        return out;
    }
};

int n, q, timer, a[N][N], in[N*N], out[N*N], up[N*N][17];
vector<int> g[N*N];
vector<pii> v;
map<pii, int> mp;

struct union_find {
    int k, dad[N*N];

    void add(pii x) {
        ++k;
        dad[k] = k;
        mp[x] = k;
    }
    int get(int x) {
        if(x == dad[x])
            return x;
        return dad[x] = get(dad[x]);
    }
    void join(int x, int y) {
        int dx = get(x), dy = get(y);
        if(dx != dy) {
            dad[dx] = dy;
            g[dy].pb(dx);
        }
    }
} dsu;

void dfs(int nod, int p) {
    in[nod] = ++timer;
    up[nod][0] = p;
    for(int i=1; (1<<i)<=n*n; i++)
        up[nod][i] = up[up[nod][i-1]][i-1];

    for(auto nxt : g[nod])
        if(nxt != p)
            dfs(nxt, nod);
            
    
    out[nod] = timer;
}
bool is_ancestor(int x, int y) {
    return in[x] <= in[y] && out[y] <= out[x];
}
int get_lca(int x, int y) {
    if(is_ancestor(x, y))
        return x;
    if(is_ancestor(y, x))
        return y;
    
    for(int j=16; j>=0; j--)
        if(up[x][j] && !is_ancestor(up[x][j], y))
            x = up[x][j];
    return up[x][0];
}

int32_t main()
{
    freopen("matrice2.in", "r", stdin);
    freopen("matrice2.out", "w", stdout);
    cin.tie(nullptr)->sync_with_stdio(0);
    
    cin >> n >> q;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++) {
            cin >> a[i][j];
            v.pb({i, j});
        }
            
    sort(v.begin(), v.end(), [](pii x, pii y) {
        return a[x.fi][x.sc] > a[y.fi][y.sc];
    });

    for(auto x : v) {
        dsu.add(x);
        for(int k=0; k<4; k++) {
            int l = x.fi + dl[k], c = x.sc + dc[k];
            if(mp[{l, c}])
                dsu.join(mp[{l, c}], mp[x]);
        }
    }

    dfs(dsu.k, 0);

    for(int i=1; i<=q; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        int lca = get_lca(mp[{x1, y1}], mp[{x2, y2}]) - 1;
        cout << a[v[lca].fi][v[lca].sc] << '\n';
    }
    return 0;
}