QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328539#833. Cells Blockingyz_lyAC ✓733ms164856kbC++143.4kb2024-02-15 20:48:212024-02-15 20:48:22

Judging History

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

  • [2024-02-15 20:48:22]
  • 评测
  • 测评结果:AC
  • 用时:733ms
  • 内存:164856kb
  • [2024-02-15 20:48:21]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-f;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}
inline void work(ll k){
	if(k<0){
		putchar('-');
		k=-k;
	}
	if(k>9)
		work(k/10);
	putchar(k%10+'0');
}
/*
找到最靠上最靠右和最靠下最靠左的路径
如果只堵塞一个点相当于就堵塞两条路径的交
堵塞两个就相当于堵塞了一个点之后,另一个再堵塞交集就行了
当然如果一开始堵塞了两条路径的交,另一个就随便堵塞,这个需要单独计算
所以只考虑第一种情况(这里假设堵塞最靠上最靠右的路径上一点),我们对于每一个(x,y)找到(x',y')满足x'+y'=x+y且y'最小的一个点使得(x',y')能被(1,1)到达且能到达(n,m)
这个点就是新的最靠上最靠右的路径,再计算交就行了
时间复杂度似乎是O((n+m)^2)的?
*/
int n,m,vis[3005][3005],cnt,vis1[3005][3005],vis2[3005][3005],vis3[3005][3005];
ll ans;
char c[3005][3005];
vector<pair<int,int> > p[6005];
int main(){
	n=read();
	m=read();
	for(int i=1;i<=n;i++){
		scanf("%s",c[i]+1);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cnt+=(c[i][j]=='.');
		}
	}
	queue<pair<int,int> > q;
	if(c[1][1]!='*'){
		q.emplace(make_pair(1,1));
		vis[1][1]=1;
	}
	while(!q.empty()){
		int ex=q.front().first,ey=q.front().second;
		q.pop();
		if(ex+1<=n&&c[ex+1][ey]=='.'&&!vis[ex+1][ey]){
			vis[ex+1][ey]=1;
			q.emplace(make_pair(ex+1,ey));
		}
		if(ey+1<=m&&c[ex][ey+1]=='.'&&!vis[ex][ey+1]){
			vis[ex][ey+1]=1;
			q.emplace(make_pair(ex,ey+1));
		}
	}
	if(!vis[n][m]){
		work(1ll*cnt*(cnt-1)/2);
		return 0;
	}
	q.emplace(make_pair(n,m));
	vis[n][m]++;
	while(!q.empty()){
		int ex=q.front().first,ey=q.front().second;
		q.pop();
		if(ex>1&&c[ex-1][ey]=='.'&&vis[ex-1][ey]==1){
			vis[ex-1][ey]++;
			q.emplace(make_pair(ex-1,ey));
		}
		if(ey>1&&c[ex][ey-1]=='.'&&vis[ex][ey-1]==1){
			vis[ex][ey-1]++;
			q.emplace(make_pair(ex,ey-1));
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(vis[i][j]==2)
				p[i+j].emplace_back(make_pair(i,j));
		}
	}
	for(int i=2;i<=n+m;i++){
		sort(p[i].begin(),p[i].end());
	}
	int now=1,now1=1;
	vis1[now][now1]=1;
	while(now!=n||now1!=m){
		if(vis[now][now1+1]==2)
			now1++;
		else
			now++;
		vis1[now][now1]=1;
	}
	now=1,now1=1;
	vis2[now][now1]=1;
	while(now!=n||now1!=m){
		if(vis[now+1][now1]==2)
			now++;
		else
			now1++;
		vis2[now][now1]=1;
	}
	int cnt1=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(vis1[i][j]&&!vis2[i][j]){
				cnt1++;
				int now=p[i+j][0].first,now1=p[i+j][0].second,x=1,y=1;
				if(now==i&&now1==j)
					now=p[i+j][1].first,now1=p[i+j][1].second;
				if(vis2[now][now1])
					ans++;
				x=now,y=now1;
				while(x!=1||y!=1){
					if(x>1&&vis[x-1][y]==2)
						x--;
					else
						y--;
					if(vis2[x][y])
						ans++;
				}
				x=now,y=now1;
				while(x!=n||y!=m){
					if(vis[x][y+1]==2)
						y++;
					else
						x++;
					if(vis2[x][y])
						ans++;
				}
			}
		}
	}
	int cnt2=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(vis1[i][j]&&vis2[i][j]){
				cnt2++;
				ans+=cnt-cnt1-1;
			}
		}
	}
	work(ans-1ll*cnt2*(cnt2-1)/2);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4060kb

input:

3 3
...
...
...

output:

17

result:

ok 1 number(s): "17"

Test #2:

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

input:

3 3
.**
.*.
...

output:

15

result:

ok 1 number(s): "15"

Test #3:

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

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #4:

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

input:

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

output:

66

result:

ok 1 number(s): "66"

Test #5:

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

input:

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

output:

1378

result:

ok 1 number(s): "1378"

Test #6:

score: 0
Accepted
time: 666ms
memory: 163888kb

input:

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

output:

17999999

result:

ok 1 number(s): "17999999"

Test #7:

score: 0
Accepted
time: 675ms
memory: 164140kb

input:

3000 3000
...................................................................................................................*......................................................................................................................................................................*..........

output:

17981671

result:

ok 1 number(s): "17981671"

Test #8:

score: 0
Accepted
time: 660ms
memory: 163620kb

input:

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

output:

17963615

result:

ok 1 number(s): "17963615"

Test #9:

score: 0
Accepted
time: 681ms
memory: 163584kb

input:

3000 3000
.........................................................................................................*...........................................................................................................................................................................................

output:

17945165

result:

ok 1 number(s): "17945165"

Test #10:

score: 0
Accepted
time: 679ms
memory: 163640kb

input:

3000 3000
......................................................................................................................................*........................................................................................................................................*.....................

output:

17928211

result:

ok 1 number(s): "17928211"

Test #11:

score: 0
Accepted
time: 677ms
memory: 162984kb

input:

3000 3000
...........................................*.........................................................................................................................................................................................................................................................

output:

17911522

result:

ok 1 number(s): "17911522"

Test #12:

score: 0
Accepted
time: 663ms
memory: 163032kb

input:

3000 3000
..............................*................................................................................................................*.....................................................................................................................................................

output:

17892283

result:

ok 1 number(s): "17892283"

Test #13:

score: 0
Accepted
time: 733ms
memory: 162600kb

input:

3000 3000
................................................................*....*................................................................................................................................................................................*..............................................

output:

17873837

result:

ok 1 number(s): "17873837"

Test #14:

score: 0
Accepted
time: 680ms
memory: 162228kb

input:

3000 3000
............................................................................................*.............................................................................*.....................................................................................................*....................

output:

17856701

result:

ok 1 number(s): "17856701"

Test #15:

score: 0
Accepted
time: 670ms
memory: 162244kb

input:

3000 3000
......................................*..........................................................................................................................................................*...................................................................................................

output:

17837857

result:

ok 1 number(s): "17837857"

Test #16:

score: 0
Accepted
time: 692ms
memory: 161468kb

input:

3000 3000
.................................................................................................................................................................................................................................*...................................................................

output:

17819731

result:

ok 1 number(s): "17819731"

Test #17:

score: 0
Accepted
time: 599ms
memory: 132496kb

input:

3000 3000
......**.....*.......*.*..........*..*...............**.............*.......*......*........*...*.....*.*.................*......*....*.........*....................*.................*.......................*.......*..*.*.......*.......................*..........*..*......................*...

output:

16202000

result:

ok 1 number(s): "16202000"

Test #18:

score: 0
Accepted
time: 437ms
memory: 100104kb

input:

3000 3000
..................*....*....*...*.*.............*.............*....*.*..*...*...*...*....*.................*...*.*.***...*....*......*.......**...*.......*.*...**...*...*...**.........*..........*.....*.*....*..*.......*.........*..*.....*...............**.......*.....*.*..*.*.*........*.....

output:

21600132

result:

ok 1 number(s): "21600132"

Test #19:

score: 0
Accepted
time: 21ms
memory: 12944kb

input:

3000 3000
..*.**...*...............*........*.*..*.*.....*........*.*..........***..*..*..*..*.*....*...*.*.....***.*...*........*..*.****..*.*....**.......*......*....*..*......*......*..*..*.*..*....*..**.*.......**.*...*....**.....**..*......*...*....*..*.**.*..***...*.....*....***.*........*.......

output:

19862779430431

result:

ok 1 number(s): "19862779430431"

Test #20:

score: 0
Accepted
time: 18ms
memory: 12628kb

input:

3000 3000
.**.**..***....*.*....*..*...*.**.**.**.......*...*........*.**.*...*...**..*...*.*.**.*.*.*.*..*...*.....*.*.**.*.*....*.**.....*..**.**.*....**.**.**..*..**...*...***.**.*.*......**.**.*...****.....***.*..*.**.*......*..**.**.**.....**...*.*..***.******...**....****..***..**.*........*.....

output:

14601805246666

result:

ok 1 number(s): "14601805246666"

Test #21:

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

input:

1 1
*

output:

0

result:

ok 1 number(s): "0"

Test #22:

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

input:

1 1
.

output:

0

result:

ok 1 number(s): "0"

Test #23:

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

input:

2 2
..
..

output:

6

result:

ok 1 number(s): "6"

Test #24:

score: 0
Accepted
time: 17ms
memory: 12564kb

input:

3000 3000
.***..**..*.*.*..*...**.*.**...***.....*..***.***.***.*..***.*......*.**.***.***.*...**.*.*..***.*..*.**..***.....*.*...***.*.***.*...*.*.....***.*..**...*.*..*.******.*.*...**.*..**.**.**.*.**..***.**.***..*......**.***.**.*....*..*.....*...*..*.*..*..*.*...**.**...*..**..***.**..*....*.....

output:

10151159625145

result:

ok 1 number(s): "10151159625145"

Test #25:

score: 0
Accepted
time: 11ms
memory: 12664kb

input:

3000 3000
*******************************************************************************************************************************************************************************************************************************************************.******************************************...

output:

39716328

result:

ok 1 number(s): "39716328"

Test #26:

score: 0
Accepted
time: 662ms
memory: 164600kb

input:

3000 3000
..*..................................................................................................................................................................................................................................................................................................

output:

35988321

result:

ok 1 number(s): "35988321"

Test #27:

score: 0
Accepted
time: 647ms
memory: 164856kb

input:

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

output:

35981866

result:

ok 1 number(s): "35981866"

Test #28:

score: 0
Accepted
time: 717ms
memory: 164508kb

input:

3000 3000
...**................................................................................................................................................................................................................................................................................................

output:

17988153

result:

ok 1 number(s): "17988153"

Test #29:

score: 0
Accepted
time: 664ms
memory: 164624kb

input:

3000 3000
...*.*...............................................................................................................................................................................................................................................................................................

output:

35969654

result:

ok 1 number(s): "35969654"

Test #30:

score: 0
Accepted
time: 693ms
memory: 164772kb

input:

3000 3000
..*..................................................................................................................................................................................................................................................................................................

output:

17982216

result:

ok 1 number(s): "17982216"

Test #31:

score: 0
Accepted
time: 659ms
memory: 164200kb

input:

3000 3000
....**.*.............................................................................................................................................................................................................................................................................................

output:

71916090

result:

ok 1 number(s): "71916090"

Test #32:

score: 0
Accepted
time: 681ms
memory: 163820kb

input:

3000 3000
....****.............................................................................................................................................................................................................................................................................................

output:

71903186

result:

ok 1 number(s): "71903186"

Test #33:

score: 0
Accepted
time: 678ms
memory: 164292kb

input:

3000 3000
.........*...........................................................................................................................................................................................................................................................................................

output:

17973051

result:

ok 1 number(s): "17973051"

Test #34:

score: 0
Accepted
time: 669ms
memory: 162540kb

input:

3000 3000
.*.......*.............................................................................................................*................................................................................*............................................................................................

output:

35630636

result:

ok 1 number(s): "35630636"

Test #35:

score: 0
Accepted
time: 694ms
memory: 162792kb

input:

3000 3000
.**...........................*..........................................................................................................................................*...........................................................................................................................

output:

44529907

result:

ok 1 number(s): "44529907"

Test #36:

score: 0
Accepted
time: 649ms
memory: 162888kb

input:

3000 3000
....*................................................................................................................................................................................................................................................................................................

output:

53426863

result:

ok 1 number(s): "53426863"

Test #37:

score: 0
Accepted
time: 680ms
memory: 162644kb

input:

3000 3000
..*.*........................................................................*............................*...............*.............................................................................................................................................*........*...................

output:

53419301

result:

ok 1 number(s): "53419301"

Test #38:

score: 0
Accepted
time: 672ms
memory: 162120kb

input:

3000 3000
......*.........*...................................................................................*.....................................................................................................................*................................................*.........................

output:

26705269

result:

ok 1 number(s): "26705269"

Test #39:

score: 0
Accepted
time: 687ms
memory: 162408kb

input:

3000 3000
....*.**....*................*............................*..........*...............................................................................................................................................................................................................................

output:

17799069

result:

ok 1 number(s): "17799069"

Test #40:

score: 0
Accepted
time: 691ms
memory: 162552kb

input:

3000 3000
...***.......................................*...........*.....................*........*...........................................................................................................................................................*................................................

output:

53393629

result:

ok 1 number(s): "53393629"

Test #41:

score: 0
Accepted
time: 668ms
memory: 164816kb

input:

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

output:

98852811

result:

ok 1 number(s): "98852811"

Test #42:

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

input:

1 3000
........................................................................................................................................................................................................................................................................................................

output:

4498500

result:

ok 1 number(s): "4498500"

Test #43:

score: 0
Accepted
time: 683ms
memory: 162364kb

input:

3000 3000
.*.....*...........................................................................................................*...................................*........................................................................................*............*.......................................

output:

88979547

result:

ok 1 number(s): "88979547"

Test #44:

score: 0
Accepted
time: 663ms
memory: 164792kb

input:

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

output:

35964327

result:

ok 1 number(s): "35964327"