#include <bits/stdc++.h>
using namespace std;
#define ar array
#define si set<int>
#define vi vector<int>
#define ER erase
#define IN insert
const int mxN=2e5, B=700;
const int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};
int p[mxN];
struct CP {
int sz, color;
bool lg;
si sn, bn;
unordered_map<int, si> bc;
} cp[mxN];
int find(int i) {
return i^p[i]?p[i]=find(p[i]):i;
}
void de(int u) {
cp[u].lg=1;
for (int v : cp[u].sn) {
cp[u].bc[cp[v].color].IN(v);
cp[v].bn.IN(u);
}
si().swap(cp[u].sn);
}
void mrg(int u, int v) {
CP& X=cp[u];
CP& Y=cp[v];
int color=X.color;
if (X.sz<Y.sz)
swap(u, v);
if (X.sz+Y.sz>=B) {
if (!X.lg)
de(u);
if (!Y.lg)
de(v);
for (int rep=0; rep<2; ++rep) {
auto it=X.bc.find(color);
auto it2=it->second.find(v);
it->second.ER(it2);
if (it->second.empty())
X.bc.ER(color);
swap(u, v);
}
for (auto& x : Y.bc)
for (int y : x.second) {
if (cp[y].lg) {
cp[y].bc[color].ER(v);
cp[y].bc[color].IN(u);
} else {
cp[y].sn.ER(v);
cp[y].sn.IN(u);
}
cp[y].bn.ER(v);
cp[y].bn.IN(u);
X.bc[x.first].IN(y);
}
unordered_map<int, si>().swap(Y.bc);
} else {
for (int rep=0; rep<2; ++rep) {
auto it=X.sn.find(v);
X.sn.ER(it);
swap(u, v);
}
for (int x : Y.sn) {
if (cp[x].lg) {
cp[x].bc[color].ER(v);
cp[x].bc[color].IN(u);
} else {
cp[x].sn.ER(v);
cp[x].sn.IN(u);
}
X.sn.IN(x);
}
si().swap(Y.sn);
}
p[v]=u;
for (int x : Y.bn)
X.bn.IN(x);
si().swap(Y.bn);
X.bn.ER(u);
X.bn.ER(v);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vi> grid(n, vi(m));
for (vi& v : grid)
for (int& i : v)
cin >> i;
vector<vi> vis(n, vi(m, -1));
int cc=0;
for (int i=0; i<n; ++i)
for (int j=0; j<m; ++j) {
if (vis[i][j]!=-1)
continue;
queue<ar<int, 2>> q;
q.push({i, j});
vis[i][j]=cc;
int sz=0;
while(q.size()) {
int i=q.front()[0], j=q.front()[1];
q.pop();
++sz;
for (int k=0; k<4; ++k) {
int ni=i+dx[k], nj=j+dy[k];
if (0<=ni&&ni<n&&0<=nj&&nj<m&&vis[ni][nj]==-1&&grid[i][j]==grid[ni][nj]) {
q.push({ni, nj});
vis[ni][nj]=cc;
}
}
}
cp[cc].sz=sz;
cp[cc].color=grid[i][j];
p[cc]=cc;
++cc;
}
for (int i=0; i<n; ++i)
for (int j=0; j<m; ++j) {
if (i+1<n&&grid[i][j]!=grid[i+1][j]) {
cp[vis[i][j]].sn.IN(vis[i+1][j]);
cp[vis[i+1][j]].sn.IN(vis[i][j]);
}
if (j+1<m&&grid[i][j]!=grid[i][j+1]) {
cp[vis[i][j]].sn.IN(vis[i][j+1]);
cp[vis[i][j+1]].sn.IN(vis[i][j]);
}
}
for (int i=0; i<cc; ++i)
if (cp[i].sz>=B)
de(i);
int q;
cin >> q;
while(q--) {
int i, j, c;
cin >> i >> j >> c, --i, --j;
int u=find(vis[i][j]);
int lc=cp[u].color;
cp[u].color=c;
for (int v : cp[u].bn) {
cp[v].bc[lc].ER(u);
if (cp[v].bc[lc].empty())
cp[v].bc.ER(lc);
cp[v].bc[c].IN(u);
}
vi cands;
if (!cp[u].lg) { // small
for (int i : cp[u].sn)
cands.push_back(i);
} else {
if (cp[u].bc.find(c)!=cp[u].bc.end())
for (int i : cp[u].bc[c])
cands.push_back(i);
}
for (int v : cands) {
if (find(v)!=u&&cp[find(v)].color==c) {
mrg(u, find(v));
u=find(u);
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j)
cout << cp[find(vis[i][j])].color << " ";
cout << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
44 ms |
67360 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
76824 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
141 ms |
146012 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
102 ms |
118884 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |