Submission #576998

# Submission time Handle Problem Language Result Execution time Memory
576998 2022-06-13T21:19:44 Z penguinhacker Paint (COI20_paint) C++14
100 / 100
679 ms 84040 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array
const int mxN=2e5, B=700; // component is considered large if size>=B
const int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};

int p[mxN];
struct Component {
	int color;
	bool large;
	vector<ar<int, 2>> pixels;
	set<int> small_neighbors, big_neighbors;
	unordered_map<int, set<int>> by_color;
	int size() {
		return pixels.size();
	}
} components[mxN];

int find(int i) {
	return i^p[i]?p[i]=find(p[i]):i;
}

void declare_large(int u) {
	assert(u==find(u)&&!components[u].large);
	components[u].large=1;
	for (int v : components[u].small_neighbors) {
		assert(v==find(v));
		components[u].by_color[components[v].color].insert(v);
		components[v].big_neighbors.insert(u);
	}
	set<int>().swap(components[u].small_neighbors);
}

void mrg(int u, int v) {
	int color=components[u].color;
	assert(u==find(u)&&v==find(v)&&color==components[v].color);
	if (components[u].size()<components[v].size())
		swap(u, v);
	assert(components[u].large>=components[v].large);

	for (ar<int, 2> x : components[v].pixels)
		components[u].pixels.push_back(x);
	vector<ar<int, 2>>().swap(components[v].pixels);

	if (components[u].size()+components[v].size()>=B) { // new component is large, merge them
		if (!components[u].large)
			declare_large(u);
		if (!components[v].large)
			declare_large(v);
		for (int rep=0; rep<2; ++rep) {
			auto it=components[u].by_color.find(color);
			assert(it!=components[u].by_color.end());
			auto it2=it->second.find(v);
			assert(it2!=it->second.end());
			it->second.erase(it2);
			if (it->second.empty())
				components[u].by_color.erase(color);
			swap(u, v);
		}
		for (auto& x : components[v].by_color) // move by_color
			for (int y : x.second) {
				assert(y==find(y));
				if (components[y].large) {
					components[y].by_color[color].erase(v);
					components[y].by_color[color].insert(u);
				} else {
					components[y].small_neighbors.erase(v);
					components[y].small_neighbors.insert(u);
				}
				components[y].big_neighbors.erase(v);
				components[y].big_neighbors.insert(u);

				components[u].by_color[x.first].insert(y);
			}
		unordered_map<int, set<int>>().swap(components[v].by_color);
	} else { // both are still small
		for (int rep=0; rep<2; ++rep) {
			auto it=components[u].small_neighbors.find(v);
			assert(it!=components[u].small_neighbors.end());
			components[u].small_neighbors.erase(it);
			swap(u, v);
		}
		for (int x : components[v].small_neighbors) {
			assert(x==find(x));
			if (components[x].large) {
				components[x].by_color[color].erase(v);
				components[x].by_color[color].insert(u);
			} else {
				components[x].small_neighbors.erase(v);
				components[x].small_neighbors.insert(u);
			}
			components[u].small_neighbors.insert(x);
		}
		set<int>().swap(components[v].small_neighbors);
	}

	p[v]=u;
	for (int x : components[v].big_neighbors)
		components[u].big_neighbors.insert(x);
	set<int>().swap(components[v].big_neighbors);
	components[u].big_neighbors.erase(u);
	components[u].big_neighbors.erase(v);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<vector<int>> grid(n, vector<int>(m));
	for (vector<int>& v : grid)
		for (int& i : v)
			cin >> i;
	vector<vector<int>> vis(n, vector<int>(m, -1));
	int cc=0; // component counter
	for (int i=0; i<n; ++i)
		for (int j=0; j<m; ++j) {
			if (vis[i][j]!=-1)
				continue;
			queue<ar<int, 2>> q;
			q.push({i, j});
			vis[i][j]=cc;
			vector<ar<int, 2>> pixels;
			while(q.size()) {
				int i=q.front()[0], j=q.front()[1];
				q.pop();
				pixels.push_back({i, j});
				for (int k=0; k<4; ++k) {
					int ni=i+dx[k], nj=j+dy[k];
					if (0<=ni&&ni<n&&0<=nj&&nj<m&&vis[ni][nj]==-1&&grid[i][j]==grid[ni][nj]) {
						q.push({ni, nj});
						vis[ni][nj]=cc;
					}
				}
			}
			components[cc].color=grid[i][j];
			components[cc].pixels=pixels;
			p[cc]=cc;
			++cc;
		}
	for (int i=0; i<n; ++i)
		for (int j=0; j<m; ++j) {
			if (i+1<n&&grid[i][j]!=grid[i+1][j]) {
				components[vis[i][j]].small_neighbors.insert(vis[i+1][j]);
				components[vis[i+1][j]].small_neighbors.insert(vis[i][j]);
			}
			if (j+1<m&&grid[i][j]!=grid[i][j+1]) {
				components[vis[i][j]].small_neighbors.insert(vis[i][j+1]);
				components[vis[i][j+1]].small_neighbors.insert(vis[i][j]);
			}
		}
	for (int i=0; i<cc; ++i)
		if (components[i].size()>=B)
			declare_large(i);
	int q;
	cin >> q;
	while(q--) {
		int i, j, c;
		cin >> i >> j >> c, --i, --j;
		int u=find(vis[i][j]); // what is the actual component
		int last_color=components[u].color;
		components[u].color=c;
		for (int v : components[u].big_neighbors) { // they need to know that color of this component has changed
			components[v].by_color[last_color].erase(u);
			if (components[v].by_color[last_color].empty())
				components[v].by_color.erase(last_color);
			components[v].by_color[c].insert(u);
		}

		vector<int> cands;
		if (!components[u].large) { // small
			for (int i : components[u].small_neighbors) {
				assert(i==find(i));
				cands.push_back(i);
			}
		} else {
			if (components[u].by_color.find(c)!=components[u].by_color.end()) {
				for (int i : components[u].by_color[c]) {
					assert(i==find(i));
					cands.push_back(i);
				}
			}
		}
		for (int v : cands) {
			if (find(v)!=u&&components[find(v)].color==c) {
				mrg(u, find(v));
				u=find(u);
			}
		}
	}
	vector<vector<int>> ans(n, vector<int>(m));
	for (int i=0; i<cc; ++i)
		if (i==find(i))
			for (ar<int, 2> x : components[i].pixels)
				ans[x[0]][x[1]]=components[i].color;
	for (int i=0; i<n; ++i) {
		for (int j=0; j<m; ++j)
			cout << ans[i][j] << " ";
		cout << "\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 22 ms 36436 KB Output is correct
2 Correct 20 ms 36548 KB Output is correct
3 Correct 26 ms 38676 KB Output is correct
4 Correct 29 ms 37972 KB Output is correct
5 Correct 30 ms 38404 KB Output is correct
6 Correct 29 ms 37784 KB Output is correct
7 Correct 19 ms 36308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 43052 KB Output is correct
2 Correct 151 ms 48224 KB Output is correct
3 Correct 177 ms 56828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 359 ms 83776 KB Output is correct
2 Correct 340 ms 83452 KB Output is correct
3 Correct 374 ms 84040 KB Output is correct
4 Correct 410 ms 82080 KB Output is correct
5 Correct 328 ms 78756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 66492 KB Output is correct
2 Correct 437 ms 77536 KB Output is correct
3 Correct 617 ms 65424 KB Output is correct
4 Correct 546 ms 69756 KB Output is correct
5 Correct 679 ms 69196 KB Output is correct
6 Correct 167 ms 67404 KB Output is correct
7 Correct 159 ms 60568 KB Output is correct
8 Correct 147 ms 55788 KB Output is correct
9 Correct 636 ms 70992 KB Output is correct