Submission #1017078

# Submission time Handle Problem Language Result Execution time Memory
1017078 2024-07-08T20:22:32 Z CodeAssignment Paint (COI20_paint) C++17
48 / 100
3000 ms 224700 KB
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
         
using namespace std;
using namespace __gnu_pbds;
 
/* typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set; */
         
#define el "\n"
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define sq(x) (x)*(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)(x).size()
#define setcontains(set, x) (set.find(x) != set.end())
         
#define umap unordered_map
#define uset unordered_set

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
         
#define all(x) (x).begin(), (x).end()

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b-1); i >= (a); --i)
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define trav(a, x) for (auto &a : x)
         
struct chash { // large odd number for C
      const uint64_t C = ll(4e18 * acos(0)) | 71;
      ll operator()(ll x) const { return __builtin_bswap64(x * C); }
};
 
template<typename T> using fast_set = __gnu_pbds::gp_hash_table<T, null_type, chash>;
template<typename T, typename H> using fast_set_h = __gnu_pbds::gp_hash_table<T, null_type, H>;
template<typename T, typename U> using fast_map = __gnu_pbds::gp_hash_table<T, U, chash>;
 
#ifdef DEBUg
#include "/home/redkar/kod/cp/lib/debug.cpp"
#else
#define dbg(...)
#define dbgArr(...)
#endif

const ll inf = 1e11;
const ll heavy = 30; // TODO: try 30 

vpll dirs = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

ll r, c, q;

ll get_ind(ll R, ll C) {
	if (0 <= R && R < r && 0 <= C && C < c) {
		return R*c + C;
	}
	else return -1;
}

struct UF {
	vll col, parent;
	vector<uset<ll>> edges;
	vector<umap<ll, uset<ll>>> buddy;
	vector<uset<ll>> heavybuddy;
	uset<ll> bigboys;
	
	UF(ll n, vll grid): col(n), parent(n), edges(n), buddy(n), heavybuddy(n) {
		rep(i, 0, n) {
			parent[i] = i;
			col[i] = grid[i];
		}
	}

	ll find(ll x) {
		if (x != parent[x]) parent[x] = find(parent[x]);
		return parent[x];
	}
	void reorganize() {
		// reorganize into heavy and not heavy
		rep(i, 0, r) rep(j, 0, c) {
			ll component = find(get_ind(i, j));
			if (sz(edges[component]) >= heavy) {
				bigboys.insert(component);
				trav(COL, buddy[component]) {
					trav(nb, COL.S) {
						// TODO: what about adding the other way? Edit: I am not sure what I meant here
						buddy[nb][col[component]].erase(component);		
						heavybuddy[nb].insert(component);
						//edges[nb].erase(component);
					}
				}
				trav(nb, heavybuddy[component]) {
					buddy[nb][col[component]].erase(component); /* hopefully this doesnt cause runtime error */
					heavybuddy[nb].insert(component);
					//edges[nb].erase(component);
				}
			}
		}
	}
	void merge(ll a, ll b) {
		a = find(a); b = find(b);
		if (a == b) return;
		//if (mass[a] < mass[b]) swap(a, b);
		//mass[a] += mass[b];
		parent[b] = a;
	}
	void bucket(ll node, ll C) {
		node = find(node);
		col[node] = C;
		ll bst = node;

		uset<ll> compnodes;
		for(auto it = buddy[node][C].begin(); it != buddy[node][C].end();) {
			ll e = find(*it);
			if (col[e] == C) {
				compnodes.insert(*it);
				if (sz(edges[e]) > sz(edges[bst])) {
					bst = e;
				}
				it = next(it);
			}
			else {
				it = buddy[node][C].erase(it);
			}
		}
		buddy[node][C].clear();
		
		trav(edge, heavybuddy[node]) {
			ll e = find(edge);
			if (col[e] == C) {
				compnodes.insert(e);
				if (sz(edges[e]) > sz(edges[bst])) {
					bst = e;
				}
			}
		}
		compnodes.insert(node);
		trav(cur, compnodes) {
			ll cnode = find(cur);
			if (cnode == bst) continue; 
			parent[cnode] = bst;
			trav(edge, edges[cnode]) {
				heavybuddy[edge].erase(cur);	
				heavybuddy[edge].erase(cnode);	
				ll e = find(edge);
				if (setcontains(bigboys, bst)) {
					heavybuddy[e].insert(bst);
				}
				edges[bst].insert(e);
			}
			trav(edge, heavybuddy[cnode]) {
				ll e = find(edge);
				heavybuddy[bst].insert(e);
			}
			trav(edge, buddy[cnode]) {
				trav(nb, edge.S) {
					buddy[bst][edge.F].insert(nb);
				}
			}
		}
		node = find(node);
		if (!setcontains(bigboys, node)) {
			if (sz(edges[node]) > heavy) {
				bigboys.insert(node);
				trav(edge, edges[node]) {
					ll e = find(edge);
					heavybuddy[e].insert(node);
				}
			}
			else {
				trav(edge, edges[node]) {
					ll e = find(edge);
					buddy[e][C].insert(node);
				}
			}
		}
	}
};

void solve() {
	cin >> r >> c;
	//assert(r * c <= 10000);
	vll grid(r*c);
	rep(i, 0, r*c) {
		cin >> grid[i];
	}

	UF uf(r*c, grid);

	//dbg(1);
	rep(phase, 0, 2) {
		//dbg(phase);
		rep(i, 0, r) rep(j, 0, c) {
			ll cell = get_ind(i, j);
			ll cell_leader = uf.find(cell);
			//ll cell_color = uf.col[uf.find(cell)];
			trav(d, dirs) {
				ll y = i + d.F, x = j + d.S;		
				if (y < 0 || y >= r || x < 0 || x >= c) continue;
				ll nb_cell = get_ind(y, x);
				ll nb_leader = uf.find(nb_cell);

				if (grid[cell] == grid[nb_cell] && phase == 0) {
					uf.merge(cell, nb_cell);
				}
				else if (grid[cell] != grid[nb_cell] && phase == 1) {
					uf.buddy[cell_leader][uf.col[nb_leader]].insert(nb_leader);
					uf.buddy[nb_leader][uf.col[cell_leader]].insert(cell_leader);
					uf.edges[cell_leader].insert(nb_leader);
					uf.edges[nb_leader].insert(cell_leader);
				}
			}
		}
	}
	//dbg(uf.col);
	//dbg(uf.buddy);
	uf.reorganize();
	//dbg(uf.col);
	//dbg(uf.buddy);

	cin >> q;
	while(q--) {
		dbg(q);
		ll y, x, C;
		cin >> y >> x >> C; y--, x--;
		
		//dbg(y, x, C);
		ll cur = uf.find(get_ind(y, x));

		if (uf.col[cur] != C) {
			uf.bucket(cur, C);
		}
		/*
		rep(i, 0, r) {
			cerr << "temp: ";
			rep(j, 0, c) {
				cerr << uf.col[uf.find(get_ind(i, j))] << " ";
			}
			cerr << el;
		}
		*/
	}

	rep(i, 0, r) {
		rep(j, 0, c) {
			cout << uf.col[uf.find(get_ind(i, j))] << " ";
		}
		cout << el;
	}
}

int main() { 
	faster
        int test = 1; // cin >> test;
	REP(tc, 1, test) {
		solve();
	}
}


# Verdict Execution time Memory Grader output
1 Correct 2 ms 1368 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
3 Correct 33 ms 18664 KB Output is correct
4 Correct 33 ms 16860 KB Output is correct
5 Correct 730 ms 10524 KB Output is correct
6 Correct 36 ms 9040 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 245 ms 78656 KB Output is correct
2 Correct 291 ms 76808 KB Output is correct
3 Correct 358 ms 142936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 727 ms 202984 KB Output is correct
2 Correct 741 ms 195312 KB Output is correct
3 Correct 770 ms 201552 KB Output is correct
4 Correct 834 ms 195160 KB Output is correct
5 Correct 725 ms 180880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1042 ms 224700 KB Output is correct
2 Execution timed out 3060 ms 166208 KB Time limit exceeded
3 Halted 0 ms 0 KB -