QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591181 | #833. Cells Blocking | xwh_Marvelous | ML | 2ms | 13840kb | C++14 | 2.5kb | 2024-09-26 14:39:09 | 2024-09-26 14:39:10 |
Judging History
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];
int t[N][N];
int num[N][N];
int tot=0;
int c=0;
namespace subqwq{
bool vis[N][N];
int fa[N*N][24],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=23;~i;i--){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i=23;~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<=23;i++)fa[dfc][i]=fa[fa[dfc][i-1]][i-1];
}
}
int ss[N*10];
int ans;
void work(){
init();
for(int i=num[n][m];i;i=fa[i][0]){ss[dep[i]+1]=i;}
// cout<<dfc<<endl;
// for(int i=1;i<=dfc;i++)cout<<fa[i][0]<<' ';cout<<endl;
// for(int i=1;i<=dfc;i++)cout<<dep[i]<<' ';cout<<endl;
// cout<<lca(3,5)<<endl;
// cout<<dep[num[n][m]]<<endl;
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;
// cout<<ss[i]<<endl;
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]];
// cout<<a<<' '<<b<<endl;
ans+=(a-u)*(b-u)+1;
// cout<<(a-u)*(b-u)+1<<endl;
}
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: 0ms
memory: 13828kb
input:
3 3 ... ... ...
output:
17
result:
ok 1 number(s): "17"
Test #2:
score: 0
Accepted
time: 2ms
memory: 13840kb
input:
3 3 .** .*. ...
output:
15
result:
ok 1 number(s): "15"
Test #3:
score: 0
Accepted
time: 0ms
memory: 7980kb
input:
3 4 **** .... ****
output:
6
result:
ok 1 number(s): "6"
Test #4:
score: 0
Accepted
time: 0ms
memory: 7724kb
input:
5 5 *.... .*.*. ***** *.*** ..*..
output:
66
result:
ok 1 number(s): "66"
Test #5:
score: 0
Accepted
time: 0ms
memory: 7736kb
input:
10 10 ...***.*.. **...*.*** ...***.*.. .**...*.*. .*****..*. ..*.****.* .**...**** ..*..*.*.* *.*.**.... ....**...*
output:
1378
result:
ok 1 number(s): "1378"
Test #6:
score: -100
Memory Limit Exceeded
input:
3000 3000 .....................................................................................................................................................................................................................................................................................................
output:
17999999