답안 #576347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
576347 2022-06-13T04:08:02 Z timmyfeng Paint (COI20_paint) C++17
40 / 100
3000 ms 72704 KB
#include <bits/stdc++.h>
using namespace std;

const int D[] = {0, 1, 0, -1};
const int MAGIC = 1e9;

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_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 + D[dir], j_new = j + D[(dir + 1) % 4];
				if (0 <= i_new && i_new < n && 0 <= j_new && j_new < m) {
                    neighbors[id].insert(i_new * m + j_new);
				}
			}
		}
	}

	unordered_map<int, unordered_map<int, unordered_set<int>>> neighbors_by_color;
    unordered_set<int> big_groups;

	auto update = [&](int a) {
        if ((int) members[a].size() >= MAGIC) {
            for (auto b : big_groups) {
                if (a != b && neighbors_by_color[color[b]].count(b))
                    neighbors_by_color[b][color[a]].insert(a);
            }
        } else {
            for (auto b : big_groups) {
                if (neighbors[a].count(b))
                    neighbors_by_color[b][color[a]].insert(a);
            }
        }
	};

    auto normalize = [&](int a) {
        unordered_set<int> neighbors_new;
        for (auto b : neighbors[a]) {
            if (leader[b] != a)
                neighbors_new.insert(leader[b]);
        }
        neighbors[a] = neighbors_new;
    };

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

        if ((int) members[a].size() < MAGIC && (int) (members[a].size() + members[b].size()) >= MAGIC) {
            normalize(a);
            for (auto u : neighbors[a])
                neighbors_by_color[a][color[u]].insert(u);
            big_groups.insert(a);
        }

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

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

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

        if ((int) members[a].size() >= MAGIC) {
            normalize(b);
            for (auto u : neighbors[b])
                neighbors_by_color[a][color[u]].insert(u);
            neighbors_by_color.erase(b);
            big_groups.erase(b);
        }
	};

	auto fill = [&](int a, int c) {
		a = leader[a];

        while (true) {
            if ((int) members[a].size() < MAGIC) {
                normalize(a);
                bool merged = false;
                auto initial_neighbors = neighbors[a];
                for (auto b : initial_neighbors) {
                    if (color[b] == c) {
                        merged = true;
                        merge(a, b);
                        a = leader[a];
                    }
                }
                if (!merged) break;
            } else {
                if (neighbors_by_color[a][c].empty()) break;
                int b = *neighbors_by_color[a][c].begin();
                neighbors_by_color[a][c].erase(b);
                if (a != b && leader[b] == b && color[b] == c) {
                    merge(a, b);
                    a = leader[a];
                }
            }
        }

		color[a] = c;
		update(a);
	};

	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";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 18 ms 3748 KB Output is correct
4 Correct 29 ms 3148 KB Output is correct
5 Execution timed out 3041 ms 3996 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 14960 KB Output is correct
2 Correct 259 ms 31032 KB Output is correct
3 Correct 272 ms 60768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 504 ms 72532 KB Output is correct
2 Correct 472 ms 72704 KB Output is correct
3 Correct 536 ms 72564 KB Output is correct
4 Correct 537 ms 71356 KB Output is correct
5 Correct 407 ms 67700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 782 ms 53088 KB Output is correct
2 Execution timed out 3068 ms 72280 KB Time limit exceeded
3 Halted 0 ms 0 KB -