Submission #576319

# Submission time Handle Problem Language Result Execution time Memory
576319 2022-06-13T02:46:38 Z timmyfeng Paint (COI20_paint) C++17
31 / 100
3000 ms 183764 KB
#include <bits/stdc++.h>
using namespace std;

const int DELTA_I[] = {0, 1, 0, -1}, DELTA_J[] = {-1, 0, 1, 0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

	int n, m; cin >> n >> m;

	vector<int> color(n * m);
	for (auto &i : color) cin >> i;

	vector<unordered_map<int, unordered_set<int>>> neighbors(n * m);
	vector<vector<int>> members(n * m);
	vector<int> leader(n * m);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			int id = i * m + j;
			leader[id] = id;
			members[id].push_back(id);
			for (int dir = 0; dir < 4; ++dir) {
				int i_new = i + DELTA_I[dir], j_new = j + DELTA_J[dir];
				if (0 <= i_new && i_new < n && 0 <= j_new && j_new < m) {
					neighbors[id][color[i_new * m + j_new]].insert(i_new * m + j_new);
				}
			}
		}
	}

	auto update = [&](int a, bool add) {
		for (auto &[c, c_neighbors] : neighbors[a]) {
			for (auto b : c_neighbors) {
				if (add) neighbors[b][color[a]].insert(a);
				else neighbors[b][color[a]].erase(a);
			}
		}
	};

	auto merge = [&](int a, int b) {
		if (members[a].size() < members[b].size()) swap(a, b);

		for (auto i : members[b]) leader[i] = a;

		members[a].insert(members[a].end(), members[b].begin(), members[b].end());
		members[b].clear();

		for (auto &[c, c_neighbors] : neighbors[b])
			neighbors[a][c].insert(c_neighbors.begin(), c_neighbors.end());
		neighbors[b].clear();
	};

	auto fill = [&](int a, int c) {
		a = leader[a];
		update(a, false);
		while (!neighbors[a][c].empty()) {
			int b = *neighbors[a][c].begin();
			neighbors[a][c].erase(b);
			if (leader[b] != a) {
				update(b, false);
				merge(a, b);
				a = leader[a];
			}
		}
		color[a] = c;
		update(a, true);
	};

	for (int i = 0; i < n * m; ++i)
		fill(i, color[i]);

	int q; cin >> q;

	while (q--) {
		int i, j, c; cin >> i >> j >> c; --i, --j;
		fill(i * m + j, c);
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << color[leader[i * m + j]] << " ";
		}
		cout << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 5 ms 1568 KB Output is correct
3 Correct 59 ms 15600 KB Output is correct
4 Correct 70 ms 10156 KB Output is correct
5 Execution timed out 3044 ms 9144 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 366 ms 44692 KB Output is correct
2 Correct 483 ms 61972 KB Output is correct
3 Execution timed out 3040 ms 121432 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 707 ms 124676 KB Output is correct
2 Correct 702 ms 124200 KB Output is correct
3 Correct 740 ms 123596 KB Output is correct
4 Correct 799 ms 121076 KB Output is correct
5 Correct 689 ms 115772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1381 ms 149952 KB Output is correct
2 Execution timed out 3069 ms 183764 KB Time limit exceeded
3 Halted 0 ms 0 KB -