Submission #777184

# Submission time Handle Problem Language Result Execution time Memory
777184 2023-07-08T18:50:09 Z dozer Paint (COI20_paint) C++14
0 / 100
3000 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 = 100;
int root[N], col[N], sz[N];
vector<vector<int>> edges[N];
vector<int> p[N];
vector<pii> dir, inv;
vector<int> large[N];
vector<int> ind[N];


int find(int node){
	if (root[node] == node) return node;
	return root[node] = find(root[node]);
}


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){
		//cout<<1<<endl;
		edges[a].resize(N);
		for (auto i : p[a]){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				if (ind[w][z] == 0) continue;
				int tmp = col[find(ind[w][z])];
				
				edges[a][tmp].pb(find(ind[w][z]));  
				large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				
			}
		}
	}

	
	else if (sz[a] >= S && sz[b] < S){
		//cout<<2<<endl;
		for (auto i : p[b]){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				if (ind[w][z] == 0) continue;
				int tmp = col[find(ind[w][z])];
				
				edges[a][tmp].pb(find(ind[w][z]));  
				large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				
			}
		}
	}
	
	else if (sz[b] >= S){
		//cout<<3<<endl;
		for (int i = 1; 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])
				edges[a][i].pb(j);
		}
		edges[b].clear();
	}
	for (auto i : large[b]) large[a].pb(i);
	
	sz[a] += sz[b];
}


void change_color(int node, int c){
	node = find(node);
	if (sz[node] >= S){
		vector<int> tmp = edges[node][c];
		for (auto j : tmp){
			if (col[find(j)] != c) continue;
			uni(node, j);
			col[find(node)] = c;
			node = find(node);
		}
		edges[node][c].clear();
		for (auto i : large[find(node)])
			edges[find(i)][c].pb(find(node));
	}
	else{
		vector<int> tmp = p[node];
		for (auto i : tmp){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				if (ind[w][z] == 0) continue;
				int tmp = col[find(ind[w][z])];
				if (tmp == c) 
				{
					uni(find(ind[w][z]), node);
				}
				else if (sz[find(ind[w][z])] >= S){
					edges[find(ind[w][z])][c].pb(find(node));
				}
				col[find(node)] = c;
			}
		}

	}
	col[find(node)] = c;
}


int32_t main(){
	//fileio();
	fastio();

	dir.pb({0, 1});
	dir.pb({1, 0});
	dir.pb({0, -1});
	dir.pb({-1, 0});
	int cntr = 1;
	int r, s;
	cin>>r>>s;
	int arr[r + 5][s + 5];
	memset(arr, -1, sizeof(arr));
	inv.pb({0, 0});
	ind[0].resize(s + 5, 0);
	for (int i = 1; i <= r; i++){
		ind[i].resize(s + 5, 0);
		for (int j = 1; j <= s; j++)
		{
			cin>>arr[i][j];
			root[cntr] = cntr;
			sz[cntr] = 1;
			col[cntr] = arr[i][j];
			ind[i][j] = cntr;
			p[cntr].pb(cntr);
			cntr++;
			inv.pb({i, j});
		}
	}

	ind[r + 1].resize(s + 5, 0);
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= s; j++){
			for (auto k : dir){
				int a = i + k.st, b = j + k.nd;
				if (ind[a][b] == 0) continue;
				if (arr[a][b] == arr[i][j]) uni(ind[a][b], ind[i][j]);
			}
		}
	}


	int q;
	cin>>q;
	while(q--){
		int r, c, x;
		cin>>r>>c>>x;
		change_color(ind[r][c], x);
	}

//	cout<<r<<sp<<s<<endl;
	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 11 ms 19068 KB Output is correct
2 Correct 12 ms 19148 KB Output is correct
3 Correct 14 ms 19896 KB Output is correct
4 Correct 24 ms 34132 KB Output is correct
5 Correct 212 ms 196752 KB Output is correct
6 Incorrect 1139 ms 125940 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 59 ms 24728 KB Output is correct
2 Runtime error 359 ms 524288 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3088 ms 235148 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 313 ms 190064 KB Output is correct
2 Runtime error 622 ms 524288 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -