#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct pair_hash{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
const int MAXN = 2e5 + 7;
const int MAXK = 1e5 + 7;
const int B = MAXN;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
vector<pii> pos[MAXN];
vi g[MAXN / B + 10][MAXK];
int bigId[MAXN / B + 10];
int col[MAXN];
vector<vi> id;
vector<vi> a;
int n, m, Q;
int k = 1;
vi big = {};
void bfs(int x, int y, int c){
id[x][y] = c;
col[c] = a[x][y];
queue<pii> q;
q.push(mp(x, y));
set<pii> s;
while(sz(q)){
pii curr = q.front();
q.pop();
x = curr.fi;
y = curr.se;
pos[c].pb(curr);
for(int z = 0; z < 4; z++){
int nx = x + dx[z];
int ny = y + dy[z];
if(min(nx, ny) >= 0 && nx < n && ny < m){
if(a[nx][ny] == a[x][y] && id[nx][ny] == 0){
id[nx][ny] = id[x][y];
q.push(mp(nx, ny));
}else if(a[nx][ny] != a[x][y] && id[nx][ny] != 0 && s.find(mp(a[nx][ny], id[nx][ny])) == s.end()){
s.insert(mp(a[nx][ny], id[nx][ny]));
}
}
}
}
if(sz(pos[c]) >= B){
bigId[c] = sz(big);
big.pb(c);
for(auto ele : s){
g[bigId[c]][ele.fi].pb(ele.se);
}
}else{
for(auto ele : s){
if(sz(pos[ele.se]) >= B){
g[bigId[ele.se]][ele.fi].pb(c);
}
}
}
}
void calc(){
id.assign(n, vi(m, 0));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(id[i][j] == 0){
pos[k].clear();
bfs(i, j, k);
k++;
}
}
}
k--;
}
void merge(int id1, int id2){
if(sz(pos[id1]) < sz(pos[id2])){
swap(id1, id2);
}
for(auto ele : pos[id2]){
pos[id1].pb(ele);
id[ele.fi][ele.se] = id1;
}
pos[id2].clear();
}
void createBig(int x, int y, int c){
}
void updBig(int x, int y, int nc){
}
void updSmall(int x, int y, int nc){
vector<pii> curr = pos[id[x][y]];
for(auto ele : curr){
for(int z = 0; z < 4; z++){
int j = id[x][y];
col[j] = nc;
int nx = ele.fi + dx[z];
int ny = ele.se + dy[z];
bool small = false;
if(min(nx, ny) >= 0 && nx < n && ny < m && id[nx][ny] != j && col[id[nx][ny]] == nc){
if(sz(pos[id[nx][ny]]) < B){
small = true;
}
merge(j, id[nx][ny]);
}
if(small && sz(pos[id[x][y]]) >= B){
createBig(x, y, nc);
return;
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
a.assign(n, vi(m));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
}
calc();
cin >> Q;
for(int i = 1; i <= Q; i++){
int x, y, nc;
cin >> x >> y >> nc;
x--;
y--;
if(sz(pos[id[x][y]]) >= B){
updBig(x, y, nc);
}else{
updSmall(x, y, nc);
}
}
vector<vi> ans(n, vi(m));
for(int i = 1; i <= k; i++){
for(auto ele : pos[i]){
ans[ele.fi][ele.se] = col[i];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cout << ans[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |