#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define vec vector
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=2e5+1;
const int K=200;
const int tx[4]={1,-1,0,0};
const int ty[4]={0,0,1,-1};
int who[K];
struct dsu{
int p[N],cnt[N],id[N];
bitset<K> ust[N];
dsu(){
iota(p,p+N,0);
fill(cnt,cnt+N,1);
}
int get(int v){
return p[v]=(p[v]==v?v:get(p[v]));
}
bool mg(int a,int b,bool need=0){
a=get(a),b=get(b);
if(a==b) return 0;
if(cnt[a]>cnt[b]) swap(a,b);
if(id[a]!=-1) id[b]=id[a];
// ust[b]|=ust[a];/// n*k/64;
p[a]=b;cnt[b]+=cnt[a];
// cout<<"MG "<<a<<' '<<b<<endl;
if(need){
ust[b]|=ust[a];/// n*k/64;
if(id[b]!=-1){
for(int j=0;j<K;j++){
if(ust[b][j] && who[j]!=-1){
ust[get(who[j])][id[b]]=1;
}
}
}
}
return 1;
}
}ds;
int n,m;
int id(int i,int j){
return i*m+j;
}
vec<int> go[K][N];
vec<int> have[K];
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
vec<vec<int>>a(n,vec<int>(m));
vec<int>b(n*m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
b[i*m+j]=a[i][j];
}
}
int q;
cin>>q;
vec<int> ii(q),jj(q),c(q);
for(int i=0;i<q;i++){
cin>>ii[i]>>jj[i]>>c[i];--ii[i];--jj[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i && a[i-1][j]==a[i][j]) ds.mg(id(i,j),id(i-1,j));
if(j && a[i][j-1]==a[i][j]) ds.mg(id(i,j),id(i,j-1));
}
}
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++)
// cout<<ds.get(id(i,j))<<' ';
// cout<<'\n';
// }
// return 0;
vec<int>used(n*m,-1);
for(int t=0;t<q;t+=K){
int l=t,r=min(q,t+K-1);
for(int i=0;i<n*m;i++) ds.id[i]=-1;
for(int i=l;i<r;i++){
int v=ds.get(id(ii[i],jj[i]));
who[i-l]=-1;
if(used[v]!=t){
ds.id[v]=i-l;
used[v]=t;
who[i-l]=v;
for(auto &u : have[i-l])
go[i-l][u].clear();
have[i-l].clear();
}
}
for(int i=0;i<n*m;i++){
if(ds.get(i)==i)
ds.ust[i].reset();
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int v=ds.get(id(i,j));
if(used[v]==t){
for(int x=0;x<4;x++){
int ni=tx[x]+i,nj=ty[x]+j;
if(ni>=0&&ni<n&&nj>=0&&nj<m){
int u=ds.get(id(ni,nj));
ds.ust[u][ds.id[v]]=1;
go[ds.id[v]][b[u]].pb(u);
have[ds.id[v]].pb(b[u]);
}
}
}
}
}
for(int i=l;i<r;i++){
int v=ds.get(id(ii[i],jj[i]));
b[v]=c[i];
for(auto &z : go[ds.id[v]][b[v]]){
if(b[ds.get(z)]==b[v])
ds.mg(v,z,1);
}
go[ds.id[v]][b[v]].clear();
for(int j=0;j<K;j++){
if(who[j]!=-1 && b[ds.get(who[j])]==b[v])
ds.mg(ds.get(who[j]),v);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
cout<<b[ds.get(id(i,j))]<<' ';
cout<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
267 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
219 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
216 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
212 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |