#include <stdio.h>
#define NM 200000
int aa[NM], n, m;
void dfs(int i, int j, int a, int c) {
if (i < 0 || i >= n || j < 0 || j >= m || aa[i * m + j] != a)
return;
aa[i * m + j] = c;
dfs(i - 1, j, a, c);
dfs(i + 1, j, a, c);
dfs(i, j - 1, a, c);
dfs(i, j + 1, a, c);
}
int ds[NM], ll[NM], rr[NM];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j, ll[j] = ll[i];
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, rr[i] = rr[j];
}
}
int main() {
int q, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &aa[i * m + j]);
scanf("%d", &q);
if (n == 1) {
for (j = 0; j < m; j++)
ds[j] = -1, ll[j] = rr[j] = j;
for (j = 1; j < m; j++)
if (aa[j] == aa[j - 1])
join(j - 1, j);
while (q--) {
int a, l, r;
scanf("%*d%d%d", &j, &a), j--;
aa[find(j)] = a;
l = ll[find(j)], r = rr[find(j)];
if (l > 0 && aa[find(l - 1)] == a)
join(l - 1, j);
if (r + 1 < m && aa[find(r + 1)] == a)
join(j, r + 1);
}
for (j = 0; j < m; j++)
printf("%d ", aa[find(j)]);
printf("\n");
} else {
while (q--) {
int c;
scanf("%d%d%d", &i, &j, &c), i--, j--;
if (aa[i * m + j] != c)
dfs(i, j, aa[i * m + j], c);
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
printf("%d ", aa[i * m + j]);
printf("\n");
}
}
return 0;
}
Compilation message
paint.c: In function 'main':
paint.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
paint.c:43:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &aa[i * m + j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
paint.c:44:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
paint.c:54:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%*d%d%d", &j, &a), j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~
paint.c:69:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d%d", &i, &j, &c), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
4 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
332 KB |
Output is correct |
5 |
Correct |
245 ms |
604 KB |
Output is correct |
6 |
Correct |
506 ms |
812 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2124 KB |
Output is correct |
2 |
Correct |
50 ms |
3108 KB |
Output is correct |
3 |
Correct |
76 ms |
6524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2596 ms |
10916 KB |
Output is correct |
2 |
Correct |
422 ms |
7540 KB |
Output is correct |
3 |
Correct |
543 ms |
8424 KB |
Output is correct |
4 |
Execution timed out |
3051 ms |
7272 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
3392 KB |
Output is correct |
2 |
Execution timed out |
3059 ms |
6792 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |