# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
922272 | ttamx | Raspad (COI17_raspad) | C++14 | 119 ms | 26528 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
const int M=55;
int n,m,cnt;
string s[N];
long long ans;
bool vis[N][M];
pair<int,int> dfs(int i,int j){
if(i<0||n<=i||j<0||m<=j||vis[i][j]||s[i][j]=='1')return {n-1,0};
vis[i][j]=true;
if(j==0||j==m-1)return {0,n-1};
pair<int,int> res(i,i);
for(int di=-1;di<=1;di++)for(int dj=-1;dj<=1;dj++){
auto [x,y]=dfs(i+di,j+dj);
res.first=min(res.first,x);
res.second=max(res.second,y);
}
return res;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=0;i<n;i++)cin >> s[i];
for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(s[i][j]=='1')ans+=1LL*(i+1)*(n-i);
for(int i=1;i<n;i++)for(int j=0;j<m;j++)if(s[i][j]=='1'&&s[i-1][j]=='1')ans-=1LL*i*(n-i);
for(int i=0;i<n;i++)for(int j=1;j<m;j++)if(s[i][j]=='1'&&s[i][j-1]=='1')ans-=1LL*(i+1)*(n-i);
for(int i=1;i<n;i++)for(int j=1;j<m;j++)if(s[i][j]=='1'&&s[i][j-1]=='1'&&s[i-1][j]=='1'&&s[i-1][j-1]=='1')ans+=1LL*i*(n-i);
for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(s[i][j]=='0'&&!vis[i][j]){
auto [x,y]=dfs(i,j);
ans+=1LL*x*(n-y-1);
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |