Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- int idx[maxn], sz[maxn];
- void init() {
- for(int i = 0; i < maxn; i++) {
- sz[i] = 1;
- idx[i] = i;
- }
- }
- int find_root(int x) {
- while(x != idx[x]) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- void unite(int A, int B) {
- int rootA = find_root(A);
- int rootB = find_root(B);
- if(rootA != rootB) {
- if(sz[rootA] < sz[rootB]) {
- idx[rootA] = idx[rootB];
- sz[rootB] += sz[rootA];
- }
- else {
- idx[rootB] = idx[rootA];
- sz[rootA] += sz[rootB];
- }
- }
- }
- bool check(int A, int B) {
- return find_root(A) == find_root(B);
- }
- struct rudnik {
- int year, x, y, at, exploit;
- rudnik() {}
- rudnik(int _year, int _x, int _y, int _at, int _exploit) {
- year = _year;
- x = _x;
- y = _y;
- at = _at;
- exploit = _exploit;
- }
- bool operator < (const rudnik & tmp) const {
- if(year == tmp.year) {
- return exploit < tmp.exploit;
- }
- return year < tmp.year;
- }
- };
- int mat[305][305];
- int main() {
- ios_base::sync_with_stdio(false);
- vector<rudnik> rudnici;
- init();
- int k;
- cin >> k;
- vector<int> G(k);
- for(int i = 0; i < k; i++) {
- cin >> G[i];
- rudnici.push_back(rudnik(G[i], -1, -1, i, 1));
- }
- int n, m;
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- rudnici.push_back(rudnik(mat[i][j], i, j, -1, 0));
- }
- }
- sort(rudnici.begin(), rudnici.end());
- int oblasti = 0;
- vector<int> res(k + 1);
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- vector<vector<bool>> visited(n + 1, vector<bool>(m + 1, false));
- for(int i = (int) rudnici.size() - 1; i >= 0; i--) {
- if(rudnici[i].exploit == 1) {
- res[rudnici[i].at] = oblasti;
- }
- else {
- int ti = rudnici[i].x, tj = rudnici[i].y;
- visited[ti][tj] = true;
- oblasti++;
- for(int k = 0; k < 4; k++) {
- int pi = ti + di[k], pj = tj + dj[k];
- if(pi >= 0 and pi < n and pj >= 0 and pj < m and visited[pi][pj]) {
- int A = pi * m + pj;
- int B = ti * m + tj;
- if(!check(A, B)) {
- oblasti--;
- }
- unite(A, B);
- }
- }
- }
- }
- for(int i = 0; i < k; i++) {
- cout << res[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment