#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
const int S = 450;
int root[N], col[N], sz[N];
vector<vector<int>> edges[N];
vector<int> p[N];
vector<pii> dir, inv;
vector<int> large[N];
vector<int> ind[N];
int find(int node){
if (root[node] == node) return node;
return root[node] = find(root[node]);
}
void uni(int a,int b){
a = find(a), b = find(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
root[b] = a;
for (auto i : p[b]) p[a].pb(i);
if (sz[a] < S && sz[a] + sz[b] >= S){
edges[a].resize(N);
for (auto i : p[a]){
int x = inv[i].st, y = inv[i].nd;
for (auto k : dir){
int w = x + k.st, z = y + k.nd;
if (ind[w][z] == 0) continue;
int gh = find(ind[w][z]);
int tmp = col[gh];
if (gh == a) continue;
edges[a][tmp].pb(gh);
large[gh].pb(a);
}
}
}
else if (sz[a] >= S && sz[b] < S){
for (auto i : p[b]){
int x = inv[i].st, y = inv[i].nd;
for (auto k : dir){
int w = x + k.st, z = y + k.nd;
if (ind[w][z] == 0) continue;
int gh = find(ind[w][z]);
int tmp = col[gh];
if (gh == a) continue;
edges[a][tmp].pb(gh);
large[gh].pb(a);
}
}
}
else if (sz[b] >= S){
for (int i = 1; i < N; i++){
if (edges[a][i].size() < edges[b][i].size()) edges[a][i].swap(edges[b][i]);
for (auto j : edges[b][i])
edges[a][i].pb(j);
}
edges[b].clear();
}
for (auto i : large[b]) large[a].pb(i);
sort(large[a].begin(), large[a].end());
unique(large[a].begin(), large[a].end());
sz[a] += sz[b];
}
void change_color(int node, int c){
node = find(node);
if (sz[node] >= S){
vector<int> tmp = edges[node][c];
for (auto j : tmp){
int gh = find(j);
if (col[gh] != c) continue;
uni(node, gh);
}
node = find(node);
edges[node][c].clear();
for (auto i : large[node])
edges[find(i)][c].pb(node);
}
else{
vector<int> tmp = p[node];
for (auto i : tmp){
int x = inv[i].st, y = inv[i].nd;
for (auto k : dir){
int w = x + k.st, z = y + k.nd;
if (ind[w][z] == 0) continue;
int gh = find(ind[w][z]);
int tmp = col[gh];
if (gh == node) continue;
if (tmp == c)
uni(gh, node);
else if (sz[gh] >= S){
edges[gh][c].pb(find(node));
}
}
}
}
col[find(node)] = c;
}
int32_t main(){
//fileio();
fastio();
dir.pb({0, 1});
dir.pb({1, 0});
dir.pb({0, -1});
dir.pb({-1, 0});
int cntr = 1;
int r, s;
cin>>r>>s;
int arr[r + 5][s + 5];
memset(arr, -1, sizeof(arr));
inv.pb({0, 0});
ind[0].resize(s + 5, 0);
for (int i = 1; i <= r; i++){
ind[i].resize(s + 5, 0);
for (int j = 1; j <= s; j++)
{
cin>>arr[i][j];
root[cntr] = cntr;
sz[cntr] = 1;
col[cntr] = arr[i][j];
ind[i][j] = cntr;
p[cntr].pb(cntr);
cntr++;
inv.pb({i, j});
}
}
ind[r + 1].resize(s + 5, 0);
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= s; j++){
for (auto k : dir){
int a = i + k.st, b = j + k.nd;
if (ind[a][b] == 0) continue;
if (arr[a][b] == arr[i][j]) uni(ind[a][b], ind[i][j]);
}
}
}
int q;
cin>>q;
while(q--){
int r, c, x;
cin>>r>>c>>x;
change_color(ind[r][c], x);
}
for (int i = 1; i <= r; i++){
for(int j = 1; j <= s; j++)
cout<<col[find(ind[i][j])]<<sp;
cout<<endl;
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19220 KB |
Output is correct |
2 |
Correct |
10 ms |
19156 KB |
Output is correct |
3 |
Correct |
12 ms |
19796 KB |
Output is correct |
4 |
Correct |
17 ms |
24504 KB |
Output is correct |
5 |
Correct |
280 ms |
104208 KB |
Output is correct |
6 |
Incorrect |
382 ms |
34680 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
23752 KB |
Output is correct |
2 |
Correct |
264 ms |
330328 KB |
Output is correct |
3 |
Execution timed out |
3069 ms |
42156 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3067 ms |
56664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3082 ms |
46004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |