QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#591188#833. Cells Blockingxwh_MarvelousML 3ms13856kbC++142.2kb2024-09-26 14:41:262024-09-26 14:41:27

Judging History

你现在查看的是最新测评结果

  • [2024-09-26 14:41:27]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:13856kb
  • [2024-09-26 14:41:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
//#define int long long
//#define mod 1000000007
#define N 3005
#define pii pair<int,int>
#define fi first
#define se second
//#define rep(i,j,k) for(int i=j;i<=k;i++)
int n,m;
char s[N][N];
bool t[N][N];
int num[N][N];
int tot=0;
int c=0;
namespace subqwq{
	bool vis[N][N];
	int fa[N*N][12],dep[N*N];
	int num[N][N],dfc;
	pii act[N*N];
	int lca(int x,int y){
		if(dep[x]<dep[y])swap(x,y);
		for(int i=11;~i;i--){
			if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
		}
		if(x==y)return x;
		for(int i=11;~i;i--){
			if(fa[x][i]==fa[y][i])continue;
			x=fa[x][i],y=fa[y][i];
		}
		return fa[x][0];
	}
	void init(){
		queue<pii>q;
		q.push({1,1});num[1][1]=dfc=1;
		fa[1][0]=0;
		dep[1]=1;
		act[1]={1,1};
		for(int x=1;x<=n;x++)for(int y=1;y<=m;y++){
			if(x==1&&y==1)continue;
			if((num[x-1][y]==0&&num[x][y-1]==0)||s[x][y]=='*')continue;
			num[x][y]=++dfc;
			act[dfc]={x,y};
			if(num[x-1][y]&&num[x][y-1]){
				fa[dfc][0]=lca(num[x-1][y],num[x][y-1]);
			}else if(num[x-1][y]){
				fa[dfc][0]=num[x-1][y];
			}else fa[dfc][0]=num[x][y-1];
			dep[dfc]=dep[fa[dfc][0]]+1;
			for(int i=1;i<=11;i++)fa[dfc][i]=fa[fa[dfc][i-1]][i-1];
		}
	}
	int ss[N*3];
	int ans;
	void work(){
		init();
		for(int i=num[n][m];i;i=fa[i][0]){ss[dep[i]+1]=i;}
		ans=(dep[num[n][m]])*(c-1)-(dep[num[n][m]]*(dep[num[n][m]]-1))/2;
		for(int i=2;i<=dep[num[n][m]]+1;i++){
			int x=act[ss[i]].fi,y=act[ss[i]].se;
			if(!(num[x-1][y]&&num[x][y-1]))continue;
			int u=dep[ss[i-1]];
			int a=dep[num[x-1][y]],b=dep[num[x][y-1]];
			ans+=(a-u)*(b-u)+1;
		}
		cout<<ans;
	}
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>(s[i]+1);
	queue<pii>q;
	q.push({1,1});
	t[1][1]=1;
	while(q.size()){
		int x=q.front().fi,y=q.front().se;
		q.pop();
		if(x<n&&!t[x+1][y]&&s[x+1][y]=='.')q.push({x+1,y}),t[x+1][y]=1;
		if(y<m&&!t[x][y+1]&&s[x][y+1]=='.')q.push({x,y+1}),t[x][y+1]=1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)c+=(s[i][j]=='.');
	if(s[1][1]=='*'||s[n][m]=='*'||!t[n][m]){
		if(c>=2)cout<<c*(c-1)/2;
		else cout<<0;
		return 0;
	}
	subqwq::work();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 13856kb

input:

3 3
...
...
...

output:

17

result:

ok 1 number(s): "17"

Test #2:

score: 0
Accepted
time: 0ms
memory: 13824kb

input:

3 3
.**
.*.
...

output:

15

result:

ok 1 number(s): "15"

Test #3:

score: 0
Accepted
time: 0ms
memory: 7948kb

input:

3 4
****
....
****

output:

6

result:

ok 1 number(s): "6"

Test #4:

score: 0
Accepted
time: 1ms
memory: 7588kb

input:

5 5
*....
.*.*.
*****
*.***
..*..

output:

66

result:

ok 1 number(s): "66"

Test #5:

score: 0
Accepted
time: 0ms
memory: 7948kb

input:

10 10
...***.*..
**...*.***
...***.*..
.**...*.*.
.*****..*.
..*.****.*
.**...****
..*..*.*.*
*.*.**....
....**...*

output:

1378

result:

ok 1 number(s): "1378"

Test #6:

score: -100
Memory Limit Exceeded

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

17999999

result: