Submission #577002

#TimeUsernameProblemLanguageResultExecution timeMemory
577002penguinhackerPaint (COI20_paint)C++17
100 / 100
638 ms72524 KiB
#include <bits/stdc++.h> using namespace std; #define ar array #define si set<int> #define vi vector<int> const int mxN=2e5, B=700; // component is considered lg if size>=B const int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1}; int p[mxN]; struct Component { 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].insert(v); cp[v].bn.insert(u); } si().swap(cp[u].sn); } void mrg(int u, int v) { int color=cp[u].color; if (cp[u].sz<cp[v].sz) swap(u, v); if (cp[u].sz+cp[v].sz>=B) { // new component is lg, merge them if (!cp[u].lg) de(u); if (!cp[v].lg) de(v); for (int rep=0; rep<2; ++rep) { auto it=cp[u].bc.find(color); auto it2=it->second.find(v); it->second.erase(it2); if (it->second.empty()) cp[u].bc.erase(color); swap(u, v); } for (auto& x : cp[v].bc) // move bc for (int y : x.second) { if (cp[y].lg) { cp[y].bc[color].erase(v); cp[y].bc[color].insert(u); } else { cp[y].sn.erase(v); cp[y].sn.insert(u); } cp[y].bn.erase(v); cp[y].bn.insert(u); cp[u].bc[x.first].insert(y); } unordered_map<int, si>().swap(cp[v].bc); } else { // both are still small for (int rep=0; rep<2; ++rep) { auto it=cp[u].sn.find(v); cp[u].sn.erase(it); swap(u, v); } for (int x : cp[v].sn) { if (cp[x].lg) { cp[x].bc[color].erase(v); cp[x].bc[color].insert(u); } else { cp[x].sn.erase(v); cp[x].sn.insert(u); } cp[u].sn.insert(x); } si().swap(cp[v].sn); } p[v]=u; for (int x : cp[v].bn) cp[u].bn.insert(x); si().swap(cp[v].bn); cp[u].bn.erase(u); cp[u].bn.erase(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; // component counter 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.insert(vis[i+1][j]); cp[vis[i+1][j]].sn.insert(vis[i][j]); } if (j+1<m&&grid[i][j]!=grid[i][j+1]) { cp[vis[i][j]].sn.insert(vis[i][j+1]); cp[vis[i][j+1]].sn.insert(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]); // what is the actual component int lc=cp[u].color; cp[u].color=c; for (int v : cp[u].bn) { // they need to know that color of this component has changed cp[v].bc[lc].erase(u); if (cp[v].bc[lc].empty()) cp[v].bc.erase(lc); cp[v].bc[c].insert(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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...