This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, const T b) {a = min(a, b); return (a == b);}
template <typename T> inline bool chmax(T& a, const T b) {a = max(a, b); return (a == b);}
const int steps[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int man = (int)(2e5 + 500);
int n, m;
int a[man], col[man], p[man], l[man], r[man];
bool was[man];
int to(int x, int y) {
return (x * m) + y;
}
int f(int x) {
return (x == p[x]) ? x : p[x] = f(p[x]);
}
void un(int x, int y) {
x = f(x);
y = f(y);
p[y] = x;
l[x] = min(l[x], l[y]);
r[x] = max(r[x], r[y]);
}
void btool(int x, int y, int c) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
was[to(i, j)] = false;
}
}
queue <array <int, 2> > q;
q.push({x, y});
was[to(x, y)] = true;
while (!q.empty()) {
auto cur = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int cx = cur[0] + steps[i][0];
int cy = cur[1] + steps[i][1];
if ((cx >= 0) && (cx < n) && (cy >= 0) && (cy < m) && !was[to(cx, cy)] && (a[to(cx, cy)] == a[to(x, y)])) {
was[to(cx, cy)] = true;
q.push({cx, cy});
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
a[to(i, j)] = (was[to(i, j)] ? c : a[to(i, j)]);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[to(i, j)];
}
}
if (n != 1) {
int q;
cin >> q;
while (q--) {
int x, y, c;
cin >> x >> y >> c;
--x, --y;
btool(x, y, c);
}
for (int i = 0; i < n; ++i, cout << "\n") {
for (int j = 0; j < m; ++j) {
cout << a[to(i, j)] << " ";
}
}
return;
}
for (int i = 0; i < m; ++i) {
p[i] = i;
l[i] = r[i] = i;
col[i] = a[i];
}
for (int i = 0; (i + 1) < m; ++i) {
if (col[f(i)] == col[f(i + 1)]) {
un(i, i + 1);
}
}
int q;
cin >> q;
while (q--) {
int x, y, c;
cin >> x >> y >> c;
--x, --y;
y = f(y);
col[y] = c;
if ((l[y] - 1) >= 0) {
if (col[f(l[y] - 1)] == col[y]) {
un(l[y] - 1, y);
}
}
if ((r[y] + 1) < m) {
if (col[f(r[y] + 1)] == col[y]) {
un(r[y] + 1, y);
}
}
}
for (int i = 0; i < m; ++i) {
cout << col[f(i)] << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef _LOCAL
system("color a");
freopen("in.txt", "r", stdin);
#endif
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |