103872024-04-01 18:32:57111Az ékszerész játékacpp17Wrong answer 0/100326ms215340 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define NM 1002

int N,x[NM*NM],g[NM*NM][4],s[NM*NM],v[NM*NM],w[NM*NM],p[NM*NM],t[NM*NM],r[NM*NM],ti[NM*NM],to[NM*NM],ta;

void dfs(int i){
	s[i]=1;
	w[i]=i;
	ti[i]=ta++;
	for(int j:g[i]){
		if(x[j]!=x[i]){
			continue;
		}
		if(j==p[i]){
			continue;
		}
		if(v[j]){
			if(v[j]<v[w[i]]){
				w[i]=j;
			}
			continue;
		}
		v[j]=v[i]+1;
		p[j]=i;
		t[j]=t[i];
		dfs(j);
		s[i]+=s[j];
		if(v[w[j]]<v[w[i]]){
			w[i]=w[j];
		}
	}
	to[i]=ta++;
}

int des(int i,int j){
	return ti[i]<=ti[j]&&to[i]>=to[j];
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	for(int i=1;i<=N;i++){
		for(int j=1;j<=N;j++){
			cin>>x[i*NM+j];
		}
	}
	for(int i=1;i<=N;i++){
		for(int j=1;j<=N;j++){
			g[i*NM+j][0]=i*NM+j-1;
			g[i*NM+j][1]=i*NM+j+1;
			g[i*NM+j][2]=(i-1)*NM+j;
			g[i*NM+j][3]=(i+1)*NM+j;
		}
	}
	int o=0;
	for(int i=1;i<=N;i++){
		for(int j=1;j<=N;j++){
			int y=i*NM+j;
			if(v[y]){
				continue;
			}
			o++;
			v[y]=1;
			t[y]=o;
			r[o]=y;
			dfs(y);
		}
	}
	for(int i=1;i<=N;i++){
		for(int j=1;j<N;j++){
			int y1=i*NM+j;
			int y2=i*NM+j+1;
			if(t[y1]==t[y2]){
				cout<<0<<' ';
				continue;
			}
			int c[2]{1,1};
			for(int i=0;i<2;i++){
				for(int z:g[y2]){
					if(z==y1){
						continue;
					}
					if(x[z]!=x[y1]){
						continue;
					}
					if(t[z]==t[y1]&&w[y1]==y1){
						int k=-1;
						for(int kk:g[y1]){
							if(des(kk,z)){
								if(k!=-1)exit(1);
								k=kk;
							}
						}
						c[i]+=k==-1?s[r[t[z]]]-s[y1]:s[k];
					}
					else{
						c[i]+=s[r[t[z]]];
						for(int zz:g[y2]){
							if(zz>z&&z!=y1&&t[zz]==t[z]){
								c[i]-=s[r[t[z]]];
								break;
							}
						}
					}
				}
				swap(y1,y2);
			}
			cout<<c[0]*c[1]<<' ';
		}
		cout<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms2140 KiB
2Wrong answer3ms2368 KiB
subtask20/15
3Wrong answer3ms2388 KiB
4Wrong answer3ms2596 KiB
5Wrong answer3ms2792 KiB
6Wrong answer3ms3008 KiB
subtask30/45
7Runtime error3ms3652 KiB
8Runtime error3ms4676 KiB
9Runtime error4ms7224 KiB
10Runtime error4ms9740 KiB
11Runtime error4ms9800 KiB
subtask40/40
12Runtime error14ms25632 KiB
13Runtime error57ms88156 KiB
14Runtime error209ms196024 KiB
15Runtime error185ms201764 KiB
16Runtime error173ms205792 KiB
17Runtime error167ms209236 KiB
18Runtime error164ms212548 KiB
19Runtime error160ms215340 KiB
20Runtime error326ms206648 KiB
21Runtime error324ms208572 KiB
22Runtime error326ms210784 KiB
23Runtime error275ms213704 KiB
24Runtime error244ms214848 KiB