#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 norm = [&](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 upd = [&](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 {
norm(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;
upd(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 |
7 ms |
1748 KB |
Output is correct |
4 |
Correct |
11 ms |
6228 KB |
Output is correct |
5 |
Correct |
12 ms |
6828 KB |
Output is correct |
6 |
Correct |
31 ms |
16204 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
7552 KB |
Output is correct |
2 |
Correct |
243 ms |
279540 KB |
Output is correct |
3 |
Correct |
128 ms |
36924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
63012 KB |
Output is correct |
2 |
Correct |
201 ms |
56544 KB |
Output is correct |
3 |
Correct |
227 ms |
92680 KB |
Output is correct |
4 |
Correct |
332 ms |
273032 KB |
Output is correct |
5 |
Correct |
264 ms |
238516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
38188 KB |
Output is correct |
2 |
Correct |
276 ms |
43432 KB |
Output is correct |
3 |
Correct |
973 ms |
492528 KB |
Output is correct |
4 |
Correct |
676 ms |
457088 KB |
Output is correct |
5 |
Correct |
482 ms |
271736 KB |
Output is correct |
6 |
Correct |
308 ms |
156640 KB |
Output is correct |
7 |
Correct |
246 ms |
119752 KB |
Output is correct |
8 |
Correct |
240 ms |
91848 KB |
Output is correct |
9 |
Correct |
352 ms |
49100 KB |
Output is correct |