Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int rep[1000005];
- int ini[1000005];
- int num;
- int n, m, t;
- int ptn(int x, int y) {
- return x * m + y;
- }
- pair <int, int> ntp(int x) {
- return make_pair(x / m, x % m);
- }
- int find(int x, int y) {
- if (ntp(rep[ptn(x, y)]).first == x && ntp(rep[ptn(x, y)]).second == y) return ptn(x, y);
- rep[ptn(x, y)] = find(ntp(rep[ptn(x, y)]).first, ntp(rep[ptn(x, y)]).second);
- return rep[ptn(x, y)];
- }
- void uunion(int x1, int yjeden, int x2, int y2) {
- if (rep[ptn(x1, yjeden)] == rep[ptn(x2, y2)]) return;
- int p1 = find(x1, yjeden);
- int p2 = find(x2, y2);
- if (p1 == p2) return;
- rep[p1] = p2;
- num--;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- //map <int, vector <int>> wart;
- priority_queue < pair<int, int>> wart;
- int mx = 0;
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> ini[ptn(i, j)];
- rep[ptn(i, j)] = ptn(i, j);
- wart.push(make_pair(ini[ptn(i, j)], ptn(i, j)));
- mx = max(ini[ptn(i, j)], mx);
- }
- }
- cin >> t;
- vector <int> tests(t);
- vector <int> answ(t);
- num = 0;
- for (int i = 0; i < t; i++) cin >> tests[i];
- int g = t - 1;
- // for (auto it = wart.rbegin(); it != wart.rend(); ++it)
- while (!wart.empty())
- {
- pair <int, int> cur = wart.top();
- int val = cur.first;
- //cout << val << '\n';
- while (g >= 0 && val <= tests[g]) answ[g] = num, g--;
- int a = tests[g];
- int i = ntp(cur.second).first;
- int j = ntp(cur.second).second;
- num++;
- if (j > 0)
- {
- if (ini[ptn(i, j - 1)] > a) uunion(i, j, i, j - 1);
- }
- if (i > 0)
- {
- if (ini[ptn(i - 1, j)] > a) uunion(i, j, i - 1, j);
- }
- if (j < m - 1)
- {
- if (ini[ptn(i, j + 1)] > a) uunion(i, j, i, j + 1);
- }
- if (i < n - 1)
- {
- if (ini[ptn(i + 1, j)] > a) uunion(i, j, i + 1, j);
- }
- wart.pop();
- }
- 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