#include <bits/stdc++.h>
using namespace std;
#define V vector
const int D[] = {0, 1, 0, -1}, M = 512, C = 1e5;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
V<int> co(n * m);
for (auto &i : co) cin >> i;
V<V<int>> ne(n * m), me(n * m);
V<int> le(n * m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int id = i * m + j;
le[id] = id;
me[id].push_back(id);
for (int d = 0; d < 4; ++d) {
int in = i + D[d], jn = j + D[(d + 1) % 4];
if (0 <= in && in < n && 0 <= jn && jn < m) {
ne[id].push_back(in * m + jn);
}
}
}
}
V<V<set<int>>> nc(n * m);
V<bool> iv(n * m);
set<int> bg;
auto normalize = [&](int a) {
V<int> nn;
for (auto b : ne[a]) {
b = le[b];
if (le[b] != a && !iv[b]) {
nn.push_back(b);
iv[b] = true;
}
}
swap(ne[a], nn);
};
auto update = [&](int a) {
if ((int) me[a].size() >= M) {
for (auto b : bg) {
if (a != b && nc[a][co[b]].count(b))
nc[b][co[a]].insert(a);
}
} else {
normalize(a);
for (auto b : bg) {
if (iv[b])
nc[b][co[a]].insert(a);
}
for (auto b : ne[a]) iv[b] = false;
}
};
auto merge = [&](int a, int b) {
if (me[a].size() < me[b].size()) swap(a, b);
if ((int) me[a].size() < M && (int) (me[a].size() + me[b].size()) >= M) {
nc[a].resize(C);
for (auto u : ne[a])
nc[a][co[le[u]]].insert(le[u]);
bg.insert(a);
}
if ((int) (me[a].size() + me[b].size()) >= M) {
for (auto u : ne[b])
nc[a][co[le[u]]].insert(le[u]);
nc[b].clear();
bg.erase(b);
}
for (auto i : me[b]) le[i] = a;
me[a].insert(me[a].end(), me[b].begin(), me[b].end());
me[b].clear();
ne[a].insert(ne[a].end(), ne[b].begin(), ne[b].end());
ne[b].clear();
};
auto fill = [&](int a, int c) {
a = le[a];
while (true) {
if ((int) me[a].size() < M) {
bool merged = false;
auto in = ne[a];
for (auto b : in) {
b = le[b];
if (b != a && co[b] == c) {
merged = true;
merge(a, b);
a = le[a];
}
}
if (!merged) break;
} else {
if (nc[a][c].empty()) break;
int b = *nc[a][c].begin();
nc[a][c].erase(b);
b = le[b];
if (b != a && co[b] == c) {
merge(a, b);
a = le[a];
}
}
}
co[a] = c;
update(a);
};
for (int i = 0; i < n * m; ++i)
fill(i, co[i]);
int q; cin >> q;
while (q--) {
int i, j, c; cin >> i >> j >> c; --i, --j;
fill(i * m + j, c);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << co[le[i * m + j]] << " ";
}
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
1836 KB |
Output is correct |
4 |
Correct |
10 ms |
6356 KB |
Output is correct |
5 |
Correct |
12 ms |
6872 KB |
Output is correct |
6 |
Correct |
30 ms |
16116 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
7748 KB |
Output is correct |
2 |
Correct |
250 ms |
279564 KB |
Output is correct |
3 |
Correct |
131 ms |
36924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
62988 KB |
Output is correct |
2 |
Correct |
214 ms |
56508 KB |
Output is correct |
3 |
Correct |
219 ms |
92592 KB |
Output is correct |
4 |
Correct |
328 ms |
272920 KB |
Output is correct |
5 |
Correct |
294 ms |
238456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
38344 KB |
Output is correct |
2 |
Correct |
305 ms |
43376 KB |
Output is correct |
3 |
Correct |
931 ms |
492444 KB |
Output is correct |
4 |
Correct |
697 ms |
457072 KB |
Output is correct |
5 |
Correct |
482 ms |
271664 KB |
Output is correct |
6 |
Correct |
310 ms |
156720 KB |
Output is correct |
7 |
Correct |
246 ms |
119704 KB |
Output is correct |
8 |
Correct |
227 ms |
91880 KB |
Output is correct |
9 |
Correct |
339 ms |
49032 KB |
Output is correct |