답안 #456375

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
456375 2021-08-06T14:52:53 Z grt Paint (COI20_paint) C++17
31 / 100
312 ms 79428 KB
#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back

using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

int n, m, q;
vector<vi>t;
map<int, vi>nbh[200 * 1000 + 10];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int p[200 * 1000 + 10];
vi occ[200 * 1000 + 10];

void merge(int x, int y) {
	x = p[x]; y = p[y];
	if(x == y) return;
	if((int)occ[x].size() < (int)occ[y].size()) {
		swap(x, y);
	}
	for(int el : occ[y]) {
		occ[x].PB(el);
		p[el] = x;
	}
	occ[y].clear();
	if((int)nbh[x].size() < (int)nbh[y].size()) {
		nbh[x].swap(nbh[y]);
	}
	for(auto &it : nbh[y]) {
		for(int el : it.ND) {
			nbh[x][it.ST].PB(el);
		}	
	}
	nbh[y].clear();
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	t.resize(n);
	for(int i = 0; i < n; ++i) {
		t[i].resize(m);
		for(int j = 0; j < m; ++j) {
			cin >> t[i][j];
			p[i * m + j] = i * m + j;
			occ[i * m + j].PB(i * m + j);
		}
	}
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			for(int d = 0; d < 4; ++d) {
				int x = i + dx[d];
				int y = j + dy[d];
				if(x >= 0 && y >= 0 && x < n && y < m) nbh[i * m + j][t[x][y]].PB(p[x * m + y]);
			}
		}
	}
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			while((int)nbh[p[i * m + j]][t[i][j]].size() > 0) {
				int x = nbh[p[i * m + j]][t[i][j]].back();
				nbh[p[i * m + j]][t[i][j]].pop_back();
				merge(x, i * m + j);
			}	
		}
	}
	cin >> q;
	while(q--) {
		int x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		int z = p[x * m + y];
		t[z / m][z % m] = c;
		while((int)nbh[z][c].size() > 0) {
			int el = nbh[z][c].back();
			nbh[z][c].pop_back();
			el = p[el];
			if(t[el / m][el % m] != c) {
				continue;
			}
			merge(el, z);
			z = p[x * m + y];
		}
		z = p[x * m + y];
		t[z / m][z % m] = c;
	}
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			cout << t[p[i * m + j] / m][p[i * m + j] % m] << " ";
		}
		cout << "\n";
	}
	
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14668 KB Output is correct
2 Correct 10 ms 14796 KB Output is correct
3 Incorrect 22 ms 20212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 126 ms 33640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 61320 KB Output is correct
2 Correct 269 ms 61280 KB Output is correct
3 Correct 269 ms 61364 KB Output is correct
4 Correct 312 ms 61076 KB Output is correct
5 Correct 265 ms 59136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 306 ms 79428 KB Output isn't correct
2 Halted 0 ms 0 KB -