Submission #576999

# Submission time Handle Problem Language Result Execution time Memory
576999 2022-06-13T21:22:31 Z penguinhacker Paint (COI20_paint) C++14
100 / 100
669 ms 84092 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) {
	components[u].large=1;
	for (int v : components[u].small_neighbors) {
		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;
	if (components[u].size()<components[v].size())
		swap(u, v);
	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);
			auto it2=it->second.find(v);
			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) {
				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);
			components[u].small_neighbors.erase(it);
			swap(u, v);
		}
		for (int x : components[v].small_neighbors) {
			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)
				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])
					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 21 ms 36436 KB Output is correct
2 Correct 22 ms 36524 KB Output is correct
3 Correct 26 ms 38616 KB Output is correct
4 Correct 29 ms 38080 KB Output is correct
5 Correct 28 ms 38448 KB Output is correct
6 Correct 28 ms 37728 KB Output is correct
7 Correct 18 ms 36320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 43092 KB Output is correct
2 Correct 147 ms 48000 KB Output is correct
3 Correct 178 ms 56852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 367 ms 83784 KB Output is correct
2 Correct 334 ms 83416 KB Output is correct
3 Correct 372 ms 84092 KB Output is correct
4 Correct 407 ms 82040 KB Output is correct
5 Correct 323 ms 78800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 66444 KB Output is correct
2 Correct 436 ms 77568 KB Output is correct
3 Correct 605 ms 65544 KB Output is correct
4 Correct 544 ms 69876 KB Output is correct
5 Correct 669 ms 69376 KB Output is correct
6 Correct 168 ms 67404 KB Output is correct
7 Correct 182 ms 60508 KB Output is correct
8 Correct 139 ms 55676 KB Output is correct
9 Correct 651 ms 70908 KB Output is correct