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[105][105];
- bool used[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;
- 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);
- used[i][j] = false;
- }
- }
- cin >> t;
- vector <ll> tests(t);
- vector <ll> answ(t);
- num = 0;
- for (int i = 0; i < t; i++) cin >> tests[i];
- for (int g = t - 1; g >= 0; g--)
- {
- ll a = tests[g];
- for (int i = 0; i < t; i++)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- if (ini[i][j] <= a) continue;
- if (!used[i][j]) 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);
- }
- used[i][j] = true;
- }
- }
- answ[g] = num;
- }
- }
- for (int i = 0; i < t; i++) cout << answ[i] << " ";
- cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement