Pagini recente » Istoria paginii runda/simularegrafuri/clasament | Cod sursa (job #2010797) | Cod sursa (job #713814) | Cod sursa (job #1609054) | Cod sursa (job #1146814)
#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) {
//cout << nodes << "\n";
father.assign(nodes, -1);
height.assign(nodes, 0);
}
inline bool query(int x, int y) {
//cout << " Querying " << x << " " << y << "\n";
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;
///up:
if(x > 0 && father[(x - 1) * n + y] != -1) {
unite(x * n + y, (x - 1) * n + y);
}
///down:
if(x < n - 1 && father[(x + 1) * n + y] != -1) {
unite(x * n + y, (x + 1) * n + y);
}
///right:
if(y < n - 1 && father[x * n + y + 1] != -1) {
unite(x * n + y, x * n + y + 1);
}
///left:
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) {
//cout << "Updating with " << pas << "\n";
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) {
//cout << "Query " << c[i].index << "\n";
while(p <= k && cell[p].value >= c[i].value) {
//cout << " Unpdating " << cell[p].x << " " << cell[p].y << "\n";
sets.update_node(cell[p].x, cell[p].y);
++ p;
}
//cout << " Updated " << p << " \n";
//cout << " " << query[c[i].index].start << " " << query[c[i].index].finish << "\n";
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;
}