#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int DELTA_I[] = {0, 1, 0, -1}, DELTA_J[] = {-1, 0, 1, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<int> color(n * m);
for (auto &i : color) cin >> i;
vector<gp_hash_table<int, gp_hash_table<int, null_type>>> neighbors(n * m);
vector<vector<int>> members(n * m);
vector<int> leader(n * m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int id = i * m + j;
leader[id] = id;
members[id].push_back(id);
for (int dir = 0; dir < 4; ++dir) {
int i_new = i + DELTA_I[dir], j_new = j + DELTA_J[dir];
if (0 <= i_new && i_new < n && 0 <= j_new && j_new < m) {
neighbors[id][color[i_new * m + j_new]].insert(i_new * m + j_new);
}
}
}
}
auto update = [&](int a) {
for (auto &[c, c_neighbors] : neighbors[a]) {
for (auto b : c_neighbors) {
neighbors[b][color[a]].insert(a);
}
}
};
auto merge = [&](int a, int b) {
if (members[a].size() < members[b].size()) swap(a, b);
for (auto i : members[b]) leader[i] = a;
members[a].insert(members[a].end(), members[b].begin(), members[b].end());
members[b].clear();
for (auto &[c, c_neighbors] : neighbors[b]) {
for (auto i : c_neighbors) {
if (leader[i] == i && color[i] == c)
neighbors[a][c].insert(i);
}
}
neighbors[b].clear();
};
auto fill = [&](int a, int c) {
a = leader[a];
while (!neighbors[a][c].empty()) {
int b = *neighbors[a][c].begin();
neighbors[a][c].erase(b);
if (a != b && leader[b] == b && color[b] == c) {
merge(a, b);
a = leader[a];
}
}
color[a] = c;
neighbors[a].erase(c);
update(a);
};
for (int i = 0; i < n * m; ++i)
fill(i, color[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 << color[leader[i * m + j]] << " ";
}
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2004 KB |
Output is correct |
2 |
Correct |
6 ms |
2884 KB |
Output is correct |
3 |
Correct |
85 ms |
29292 KB |
Output is correct |
4 |
Correct |
90 ms |
24056 KB |
Output is correct |
5 |
Execution timed out |
3034 ms |
169060 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
513 ms |
114292 KB |
Output is correct |
2 |
Correct |
457 ms |
126760 KB |
Output is correct |
3 |
Execution timed out |
3087 ms |
257492 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
557 ms |
254424 KB |
Output is correct |
2 |
Correct |
564 ms |
254288 KB |
Output is correct |
3 |
Correct |
576 ms |
252264 KB |
Output is correct |
4 |
Correct |
640 ms |
247932 KB |
Output is correct |
5 |
Correct |
570 ms |
237040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1252 ms |
341684 KB |
Output is correct |
2 |
Execution timed out |
3088 ms |
293572 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |