Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- pair<int, int> rep[1005][1005];
- ll ini[1005][1005];
- ll num;
- pair <int, int> find(int x, int y) {
- if (rep[x][y].first == x && rep[x][y].second == y) return make_pair(x, y);
- rep[x][y] = find(rep[x][y].first, rep[x][y].second);
- return rep[x][y];
- }
- void uunion(int x1, int yjeden, int x2, int y2) {
- if (rep[x1][yjeden] == rep[x2][y2]) return;
- pair <int, int> p1 = find(x1, yjeden);
- pair <int, int> p2 = find(x2, y2);
- if (p1 == p2) return;
- rep[p1.first][p1.second] = p2;
- num--;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n, m, t;
- map <ll, vector < pair <int, int>>> wart;
- ll mx = 0;
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> ini[i][j];
- rep[i][j] = make_pair(i, j);
- wart[ini[i][j]].push_back(make_pair(i, j));
- mx = max(ini[i][j], mx);
- }
- }
- cin >> t;
- vector <ll> tests(t);
- vector <ll> answ(t);
- num = 0;
- for (int i = 0; i < t; i++) cin >> tests[i];
- ll cur = mx;
- int g = t - 1;
- for (auto it = wart.rbegin(); it != wart.rend(); ++it)
- {
- while (g >= 0 && it->first <= tests[g]) answ[g] = num, g--;
- ll a = tests[g];
- for (int k = 0; k < it->second.size(); k++)
- {
- int i = it->second[k].first;
- int j = it->second[k].second;
- num++;
- if (j > 0)
- {
- if (ini[i][j - 1] > a) uunion(i, j, i, j - 1);
- }
- if (i > 0)
- {
- if (ini[i - 1][j] > a) uunion(i, j, i - 1, j);
- }
- if (j < m - 1)
- {
- if (ini[i][j + 1] > a) uunion(i, j, i, j + 1);
- }
- if (i < n - 1)
- {
- if (ini[i + 1][j] > a) uunion(i, j, i + 1, j);
- }
- }
- }
- while (g >= 0) answ[g] = num, g--;
- for (int i = 0; i < t; i++)
- {
- cout << answ[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement