Submission #1244444

#TimeUsernameProblemLanguageResultExecution timeMemory
1244444ducdevPaint (COI20_paint)C++20
8 / 100
660 ms3912 KiB
// Author: 4uckd3v - Nguyen Cao Duc #include <bits/stdc++.h> using namespace std; #define cell(x, y) (((x) - 1) * n + (y)) typedef long long ll; typedef pair<int, int> ii; const int MAX_N = 2e5; const int MAX_Q = 1e5; const int MOD = 1e9 + 7; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; struct Query { int x, y, color; Query() {}; Query(int x, int y, int color) : x(x), y(y), color(color) {}; }; struct DSU { vector<int> par, sz, color; int n; int findSet(int u) { return u == par[u] ? u : par[u] = findSet(par[u]); }; bool unionSet(int u, int v, int c) { u = findSet(u), v = findSet(v); color[u] = c; if (u == v) return false; // if (sz[u] < sz[v]) swap(u, v); // sz[u] += sz[v]; par[v] = u; return true; }; DSU(int n) : n(n) { par.resize(n + 1), sz.resize(n + 1), color.resize(n + 1); for (int u = 1; u <= n; u++) { par[u] = u; sz[u] = 1; }; }; }; int m, n, q; vector<Query> queries; vector<vector<int>> a; bool valid(int x, int y) { return (x >= 1 && x <= m && y >= 1 && y <= n); }; namespace SUBTASK_1 { const int MN = 1e4; bool vis[MN + 5]; vector<vector<int>> b; bool checkSubtask() { return m * n <= 1e4 && q <= 1e4; }; void dfs(int x, int y, int color) { vis[cell(x, y)] = true; for (int i = 0; i < 4; i++) { int u = x + dx[i]; int v = y + dy[i]; if (valid(u, v) && !vis[cell(u, v)] && b[u][v] == b[x][y]) dfs(u, v, color); }; b[x][y] = color; }; void Solve() { b = a; for (const Query &q : queries) { int x = q.x, y = q.y, color = q.color; for (int i = 1; i <= m * n; i++) vis[i] = false; dfs(x, y, color); }; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cout << b[i][j] << " "; }; cout << '\n'; }; }; }; // namespace SUBTASK_1 namespace SUBTASK_2 { const int M = 1; const int N = MAX_N; const int Q = MAX_Q; bool checkSubtask() { return m <= M && n <= N && q <= Q; }; void Solve() { DSU nxt(n), prv(n); for (int i = 1; i <= n; i++) { prv.unionSet(i, i, a[1][i]); nxt.unionSet(i, i, a[1][i]); }; for (int i = 1; i < n; i++) { if (a[1][i] == a[1][i + 1]) { prv.unionSet(i, i + 1, a[1][i]); nxt.unionSet(i + 1, i, a[1][i]); }; }; for (const Query &q : queries) { int idx = q.y, color = q.color; int l = prv.findSet(idx), r = nxt.findSet(idx); int newL = (l - 1 == 0 ? l : prv.findSet(l - 1)); int newR = (r + 1 > n ? r : nxt.findSet(r + 1)); if (prv.color[newL] == color) { prv.unionSet(newL, l, color); }; if (nxt.color[newR] == color) { nxt.unionSet(newR, r, color); }; prv.unionSet(l, r, color); nxt.unionSet(r, l, color); }; vector<int> &color = prv.color; for (int i = 1; i <= n; i++) cout << color[prv.findSet(i)] << ' '; cout << '\n'; }; }; // namespace SUBTASK_2 int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen("PAINT.INP", "r")) { freopen("PAINT.INP", "r", stdin); freopen("PAINT.OUT", "w", stdout); }; cin >> m >> n; a.resize(m + 2, vector<int>(n + 2, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; }; }; cin >> q; queries.resize(q); for (Query &q : queries) cin >> q.x >> q.y >> q.color; if (SUBTASK_1::checkSubtask()) return SUBTASK_1::Solve(), 0; if (SUBTASK_2::checkSubtask()) return SUBTASK_2::Solve(), 0; };

Compilation message (stderr)

paint.cpp: In function 'int main()':
paint.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |         freopen("PAINT.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
paint.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |         freopen("PAINT.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...