Cod sursa(job #1146816)

Utilizator dariusdariusMarian Darius dariusdarius Data 19 martie 2014 12:13:04
Problema Matrice 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.28 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 305;
const int MAX_Q = 20005;

struct Cell {
    int value, x, y;
    inline bool operator < (const Cell & other) const {
        return value > other.value;
    }
} cell[MAX_N * MAX_N];
struct Query {
    int start, finish, answer;
} query[MAX_Q];
struct CurrentQuery {
    int index, value;
    inline bool operator < (const CurrentQuery &other) const {
        return value > other.value;
    }
} c[MAX_Q];
int n, m, k;

class DisjointSets {
    public:
        DisjointSets() {}
        DisjointSets(int nodes) {
            father.assign(nodes, -1);
            height.assign(nodes, 0);
        }
        inline bool query(int x, int y) {
            return get_father(x) != -1 && get_father(x) == get_father(y);
        }
        inline void update_node(int x, int y) {
            father[x * n + y] = x * n + y;
            if(x > 0 && father[(x - 1) * n + y] != -1) {
                unite(x * n + y, (x - 1) * n + y);
            }
            if(x < n - 1 && father[(x + 1) * n + y] != -1) {
                unite(x * n + y, (x + 1) * n + y);
            }
            if(y < n - 1 && father[x * n + y + 1] != -1) {
                unite(x * n + y, x * n + y + 1);
            }
            if(y > 0 && father[x * n + y - 1] != -1) {
                unite(x * n + y, x * n + y - 1);
            }
        }
    private:
        vector<int> father, height;
        inline int get_father(int node) {
            if(father[node] == -1) {
                return -1;
            }
            return (node == father[node]) ? node : (father[node] = get_father(father[node]));
        }
        inline void unite(int v, int w) {
            v = get_father(v);
            w = get_father(w);
            if(height[v] == height[w]) {
                father[w] = v;
                ++ height[v];
            } else if(height[v] > height[w]) {
                father[w] = v;
            } else {
                father[v] = w;
            }
        }
};

int main() {
    ifstream fin("matrice2.in");
    ofstream fout("matrice2.out");
    fin >> n >> m;
    for(int i = 0; i < n; ++ i) {
        for(int j = 0; j < n; ++ j) {
            int a;
            fin >> a;
            cell[++ k].value = a;
            cell[k].x = i;
            cell[k].y = j;
        }
    }
    sort(cell + 1, cell + k + 1);
    for(int i = 1; i <= m; ++ i) {
        int x1, y1, x2, y2;
        fin >> x1 >> y1 >> x2 >> y2;
        query[i].start = (x1 - 1) * n + (y1 - 1);
        query[i].finish = (x2 - 1) * n + (y2 - 1);
        query[i].answer = 0;
    }
    for(int pas = 1 << 19; pas; pas >>= 1) {
        for(int i = 1; i <= m; ++ i) {
            c[i].index = i;
            c[i].value = query[i].answer + pas;
        }
        sort(c + 1, c + m + 1);
        int p = 1;
        DisjointSets sets(k);
        for(int i = 1; i <= m; ++ i) {
            while(p <= k && cell[p].value >= c[i].value) {
                sets.update_node(cell[p].x, cell[p].y);
                ++ p;
            }
            if(sets.query(query[c[i].index].start, query[c[i].index].finish)) {
                query[c[i].index].answer += pas;
            }
        }
    }
    for(int i = 1; i <= m; ++ i) {
        fout << query[i].answer << "\n";
    }
    return 0;
}