답안 #777339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
777339 2023-07-09T06:11:58 Z dozer Paint (COI20_paint) C++14
0 / 100
3000 ms 385352 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 = 450;
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];
vector<int> done[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){
		edges[a].resize(N);
		done[a].resize(N, 0);

		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 gh = find(ind[w][z]);
				if (done[a][gh]) continue;
				done[a][gh] = 1;

				int tmp = col[gh];
				if (gh == a) continue;

				edges[a][tmp].pb(gh);  
				large[gh].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				
			}
		}
	}

	
	else if (sz[a] >= S && sz[b] < S){
		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 gh = find(ind[w][z]);
				if (done[a][gh]) continue;
				done[a][gh] = 1;

				int tmp = col[gh];
				if (gh == a) continue;

				edges[a][tmp].pb(gh);  
				large[gh].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				
			}
		}
	}
	
	else if (sz[b] >= S){
		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);
	sort(large[a].begin(), large[a].end());
	unique(large[a].begin(), large[a].end());
	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){
			int gh = find(j);
			if (col[gh] != c) 
			{
				continue;
			}
			uni(node, gh);
		}
		node = find(node);
		edges[node][c].clear();
	}

	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 gh = find(ind[w][z]);
				int tmp = col[gh];
				if (gh == node) continue;

				if (tmp == c) {
					uni(gh, node);
				}
			}
		}

	}
	node = find(node);
	for (auto i : large[node])
			edges[find(i)][c].pb(node);
	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);
	}

	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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
3 Correct 14 ms 24448 KB Output is correct
4 Correct 19 ms 29872 KB Output is correct
5 Correct 268 ms 106664 KB Output is correct
6 Incorrect 130 ms 41152 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 28304 KB Output is correct
2 Correct 306 ms 385352 KB Output is correct
3 Execution timed out 3066 ms 47560 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 70552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3048 ms 52272 KB Time limit exceeded
2 Halted 0 ms 0 KB -