Submission #777556

# Submission time Handle Problem Language Result Execution time Memory
777556 2023-07-09T10:32:56 Z dozer Paint (COI20_paint) C++14
48 / 100
393 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
 
 
const int S = 400;
 
 
vector<vector<int>> edges[N];
int sz[N], root[N], col[N];
vector<int> large[N], p[N];
vector<pii> dir;
pii rev[N];
vector<int> done[N];
vector<int> ind[N];
int all[N];
int donee[N];
int cntr;
 
inline int find(int node){
	if (node == root[node]) return node;
	return root[node] = find(root[node]);
}
 
inline void uni(int a, int b){
	a = find(a), b = find(b);
	if (a == b) return;
 
	if (sz[a] < sz[b]) swap(a, b);
	root[b] = a;
	for (auto i : p[b]) p[a].pb(i);
		
	if (sz[a] < S && sz[a] + sz[b] >= S){
		edges[a].resize(N);
		done[a].resize(N, -1);
		done[a][a] = 1;
 
		for (auto i : p[a]){
			int x = rev[i].st, y = rev[i].nd;
			int cnt = 0;
			for (auto j : dir){
				int k = x + j.st, l = y + j.nd;
				int tmp = find(ind[k][l]);
				if (tmp == 0 || tmp == a || done[a][tmp] == col[tmp]) continue;
				cnt++;
				edges[a][col[tmp]].pb(tmp);
				done[a][tmp] = col[tmp];
				large[tmp].pb(a);
			}
		}
	}
 
	else if (sz[a] >= S && sz[b] < S){
		for (auto i : p[b]){
			int cnt = 0;
			int x = rev[i].st, y = rev[i].nd;
			for (auto j : dir){
				int k = x + j.st, l = y + j.nd;
				int tmp = find(ind[k][l]);
				if (tmp == 0 || done[a][tmp] == col[tmp]) continue;
				edges[a][col[tmp]].pb(tmp);
				large[tmp].pb(a);
				done[a][tmp] = col[tmp];
				cnt++;
			}
		}
	}
 
	else if (sz[b] >= S && sz[a] >= S) {
		for (int i = 0; i < N; i++){
			if (edges[a][i].size() < edges[b][i].size()) edges[a][i].swap(edges[b][i]);
			for (auto j : edges[b][i]) 
			{
				if (done[a][find(j)] == col[find(j)]); 
				done[a][find(j)] = col[find(j)], edges[a][i].pb(j);
			}
			/*
			vector<int> tmp;
			for (auto j : edges[b][i]) tmp.pb(find(j));
			for (auto j : edges[a][i]) tmp.pb(find(j));
			sort(tmp.begin(), tmp.end());
			unique(tmp.begin(), tmp.end());
			edges[a][i] = tmp;*/
		}
		edges[b].clear();
		done[b].clear();
	}

	vector<int> tmp;
	cntr++;
	for (auto i : large[a]) if (donee[find(i)] != cntr) tmp.pb(find(i)), donee[find(i)] = cntr;
	for (auto i : large[b]) if (donee[find(i)] != cntr) tmp.pb(find(i)), donee[find(i)] = cntr;
	
	large[a] = tmp;
	large[b].clear();
	sz[a] += sz[b];/*
	if (large[a].size() < large[b].size()) large[a].swap(large[b]);
	for (auto i : large[b]) large[a].pb(i);*/
}
 
 
inline void change_color(int node, int c){
	node = find(node);
	col[node] = c;
	if (sz[node] >= S){
		vector<int> tmp = edges[node][c];
		
		for (auto i : tmp){
			int gh = find(i);
			if (col[gh] == c) uni(gh, node);
			node = find(node);
		}
		edges[node][c].clear();
		col[node] = c;
	}
 
	else{
		vector<int> tmp = p[node];
		for (auto i : tmp){
			int cnt = 0;
			int x = rev[i].st, y = rev[i].nd;
			for (auto j : dir){
				int k = x + j.st, l = y + j.nd;
				int tmp = find(ind[k][l]);
				if (tmp != node) cnt++;
				if (tmp != 0 && col[tmp] == c){
					uni(tmp, node);
				}
				node = find(node);
			}
		}
	}
 
	for (auto i : large[node]){
		if (find(i) != node && done[find(i)][node] != c) edges[find(i)][c].pb(node);
	}
	col[find(node)] = c;
}
 
int32_t main(){
	#ifndef ONLINE_JUDGE
	//fileio();
	#endif
	fastio();
 
	dir.pb({0, 1});
	dir.pb({1, 0});
	dir.pb({0, -1});
	dir.pb({-1, 0});
 
	int r, s;
	cin>>r>>s;
	for (int i = 0; i <= r + 5; i++) ind[i].resize(s + 5, 0);
	int arr[r + 5][s + 5];
	int cntr = 1;
	for (int i  = 1; i <= r; i++){
		for (int j = 1; j <= s; j++){
			cin>>arr[i][j];
			ind[i][j] = cntr;
			sz[cntr] = 1;
			p[cntr].pb(cntr);
			col[cntr] = arr[i][j];
			root[cntr] = cntr;
			rev[cntr] = {i, j};
			cntr++;
		}
	}
 
	for (int i = 1; i <= r; i++){
		for (int j  = 1; j <= s; j++){
			for (auto k : dir){
				int x = i + k.st, y = j + k.nd;
				if (ind[x][y] == 0) continue;
				if (arr[x][y] == arr[i][j]) uni(ind[i][j], ind[x][y]);
			}
		}
	}
 
	int q;
	cin>>q;
	while(q--){
		int x, y, c;
		cin>>x>>y>>c;
		change_color(ind[x][y], c);
	}
 
 
	for (int i = 1; i <= r; i++){
		for(int j = 1; j <= s; j++)
			cout<<col[find(ind[i][j])]<<sp;
		cout<<endl;
	}
 
 
 
 
	cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23828 KB Output is correct
2 Correct 14 ms 23840 KB Output is correct
3 Correct 19 ms 24464 KB Output is correct
4 Correct 21 ms 29908 KB Output is correct
5 Correct 20 ms 30548 KB Output is correct
6 Correct 33 ms 46624 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 29948 KB Output is correct
2 Correct 334 ms 453860 KB Output is correct
3 Correct 106 ms 59608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 75324 KB Output is correct
2 Correct 117 ms 78692 KB Output is correct
3 Correct 150 ms 124640 KB Output is correct
4 Correct 367 ms 425840 KB Output is correct
5 Correct 262 ms 342936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 62328 KB Output is correct
2 Correct 188 ms 59180 KB Output is correct
3 Runtime error 393 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -