제출 #381480

#제출 시각아이디문제언어결과실행 시간메모리
381480NONAMEPaint (COI20_paint)C++17
8 / 100
3065 ms48748 KiB
#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 p[man], sz[man], col[man]; set <int> s[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) { int px, py; px = f(x); py = f(y); p[py] = px; for (auto& i : s[py]) { s[px].insert(i); } s[py].clear(); sz[py] = 0; } void letsTry(int x, int y, int c) { int pr = f(to(x, y)); if (col[pr] == c) { return; } set <int> cur = s[pr]; auto it = cur.begin(); while ((!cur.empty()) && (it != cur.end())) { auto lst = it; ++it; if (col[f(*lst)] == c) { un(pr, *lst); cur.erase(lst); } } col[pr] = c; } void solve() { cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int c; cin >> c; p[to(i, j)] = to(i, j); col[to(i, j)] = c; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if ((i - 1) >= 0) { int p1 = f(to(i, j)); int p2 = f(to(i - 1, j)); if (col[p1] == col[p2]) { p[p1] = p2; } } if ((j - 1) >= 0) { int p1 = f(to(i, j)); int p2 = f(to(i, j - 1)); if (col[p1] == col[p2]) { p[p1] = p2; } } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int t = 0; t < 4; ++t) { int cx = i + steps[t][0]; int cy = j + steps[t][1]; if ((cx >= 0) && (cx < n) && (cy >= 0) && (cy < m)) { s[f(to(i, j))].insert(f(to(cx, cy))); } } } } // for (int i = 0; i < n; ++i, cerr << "\n") { // for (int j = 0; j < m; ++j) { // cerr << f(to(i, j)) << " "; // } // } // cerr << "\n"; int q; cin >> q; while (q--) { int x, y, c; cin >> x >> y >> c; --x, --y; letsTry(x, y, c); // for (int i = 0; i < n; ++i, cerr << "\n") { // for (int j = 0; j < m; ++j) { //// cerr << col[f(to(i, j))] << " "; // cerr << f(to(i, j)) << " "; // } // } // cerr << "\n"; } for (int i = 0; i < n; ++i, cout << "\n") { for (int j = 0; j < m; ++j) { cout << col[f(to(i, j))] << " "; } } } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...