Submission #777470

# Submission time Handle Problem Language Result Execution time Memory
777470 2023-07-09T09:21:39 Z dozer Paint (COI20_paint) C++14
8 / 100
3000 ms 386992 KB
    #include <bits/stdc++.h>
    using namespace std;
    #define sp " "
    #define endl "\n"
    #define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
    #define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
    #define pb push_back
    #define pii pair<int, int>
    #define st first
    #define nd second
    #define N 200005
     
     
    const int S = 400;
    int root[N], col[N], sz[N];
    vector<vector<int>> edges[N];
    vector<int> p[N];
    vector<pii> dir, inv;
    vector<int> large[N];
    vector<int> ind[N];
     
     
    int find(int node){
    	if (root[node] == node) return node;
    	return root[node] = find(root[node]);
    }
     
     
    void uni(int a,int b){
    	a = find(a), b = find(b);
    	if (a == b) return;
    	if (sz[a] < sz[b]) swap(a, b);
    	root[b] = a;
    	for (auto i : p[b]) p[a].pb(i);
    	
    	if (sz[a] < S && sz[a] + sz[b] >= S){
    		//cout<<1<<endl;
    		edges[a].resize(N);
    		for (auto i : p[a]){
    			int x = inv[i].st, y = inv[i].nd;
    			for (auto k : dir){
    				int w = x + k.st, z = y + k.nd;
    				if (ind[w][z] == 0) continue;
    				int tmp = col[find(ind[w][z])];
    				if (find(ind[w][z]) == a) continue;
    				edges[a][tmp].pb(find(ind[w][z]));  
    				large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
    				
    			}
    		}
    	}
     
    	
    	else if (sz[a] >= S && sz[b] < S){
    		//cout<<2<<endl;
    		for (auto i : p[b]){
    			int x = inv[i].st, y = inv[i].nd;
    			for (auto k : dir){
    				int w = x + k.st, z = y + k.nd;
    				if (ind[w][z] == 0) continue;
    				int tmp = col[find(ind[w][z])];
    				if (find(ind[w][z]) == a) continue;
     
    				edges[a][tmp].pb(find(ind[w][z]));  
    				large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
    				
    			}
    		}
    	}
    	
    	else if (sz[b] >= S){
    		//cout<<3<<endl;
    		for (int i = 0; i < N; i++){
    			if (edges[a][i].size() < edges[b][i].size()) edges[a][i].swap(edges[b][i]);
    			for (auto j : edges[b][i])
    				edges[a][i].pb(j);
    		}
    		edges[b].clear();
    	}
     
    	for (auto i : large[b]) large[a].pb(i);
    	sort(large[a].begin(), large[a].end());
    	unique(large[a].begin(), large[a].end());
    	sz[a] += sz[b];
    }
     
     
    void change_color(int node, int c){
    	node = find(node);
    	if (sz[node] >= S){
    		vector<int> tmp = edges[node][c];
    		for (auto j : tmp){
    			if (col[find(j)] != c) continue;
    			uni(node, j);
    		}
    		edges[node][c].clear();
    		for (auto i : large[find(node)])
    			edges[find(i)][c].pb(find(node));
    	}
    	else{
    		vector<int> tmp = p[node];
    		for (auto i : tmp){
    			int x = inv[i].st, y = inv[i].nd;
    			for (auto k : dir){
    				int w = x + k.st, z = y + k.nd;
    				if (ind[w][z] == 0) continue;
    				int tmp = col[find(ind[w][z])];
    				if (find(ind[w][z]) == node) continue;
    				if (tmp == c) 
    				{
    					uni(find(ind[w][z]), node);
    				}
    				else if (sz[find(ind[w][z])] >= S){
    					edges[find(ind[w][z])][c].pb(find(node));
    				}
    			}
    		}
     
    	}
    	col[find(node)] = c;
    }
     
     
    int32_t main(){
    	//fileio();
    	fastio();
     
    	dir.pb({0, 1});
    	dir.pb({1, 0});
    	dir.pb({0, -1});
    	dir.pb({-1, 0});
    	int cntr = 1;
    	int r, s;
    	cin>>r>>s;
    	int arr[r + 5][s + 5];
    	memset(arr, -1, sizeof(arr));
    	inv.pb({0, 0});
    	ind[0].resize(s + 5, 0);
    	for (int i = 1; i <= r; i++){
    		ind[i].resize(s + 5, 0);
    		for (int j = 1; j <= s; j++)
    		{
    			cin>>arr[i][j];
    			root[cntr] = cntr;
    			sz[cntr] = 1;
    			col[cntr] = arr[i][j];
    			ind[i][j] = cntr;
    			p[cntr].pb(cntr);
    			cntr++;
    			inv.pb({i, j});
    		}
    	}
     
    	ind[r + 1].resize(s + 5, 0);
    	for (int i = 1; i <= r; i++)
    	{
    		for (int j = 1; j <= s; j++){
    			for (auto k : dir){
    				int a = i + k.st, b = j + k.nd;
    				if (ind[a][b] == 0) continue;
    				if (arr[a][b] == arr[i][j]) uni(ind[a][b], ind[i][j]);
    			}
    		}
    	}
     
     
    	int q;
    	cin>>q;
    	while(q--){
    		int r, c, x;
    		cin>>r>>c>>x;
    		change_color(ind[r][c], x);
    	}
     
    //	cout<<r<<sp<<s<<endl;
    	for (int i = 1; i <= r; i++){
    		for(int j = 1; j <= s; j++)
    			cout<<col[find(ind[i][j])]<<sp;
    		cout<<endl;
    	}
     
    	cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
    }
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19112 KB Output is correct
2 Correct 11 ms 19156 KB Output is correct
3 Correct 13 ms 19900 KB Output is correct
4 Correct 18 ms 24532 KB Output is correct
5 Correct 298 ms 104548 KB Output is correct
6 Correct 375 ms 39440 KB Output is correct
7 Correct 10 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 23944 KB Output is correct
2 Correct 318 ms 386992 KB Output is correct
3 Execution timed out 3041 ms 42480 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3029 ms 56812 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3046 ms 55480 KB Time limit exceeded
2 Halted 0 ms 0 KB -