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;
typedef long long ll;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, m, mx, Mx, e[100010][55][4], vis[100010][55], ts;
char b[100010][55];
ll ans;
void ff(int x, int y){
if(x < 0 || y < 0 || x > n || y > m || vis[x][y]) return;
vis[x][y] = 1;
mx = min(mx, x); Mx = max(Mx, x);
for(int i = 0; i < 4; i++) if(!e[x][y][i]) ff(x + dx[i], y + dy[i]);
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%s", b[i] + 1);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
b[i][j] -= '0';
if(b[i][j]) ans += 1LL * i * (n + 1 - i);
if(b[i][j] && b[i - 1][j]) ans -= 1LL * (i - 1) * (n + 1 - i);
if(b[i][j] && b[i][j - 1]) ans -= 1LL * i * (n + 1 - i);
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
e[i][j][0] = b[i + 1][j] && b[i + 1][j + 1];
e[i][j][1] = b[i][j + 1] && b[i + 1][j + 1];
e[i][j][2] = b[i][j] && b[i][j + 1];
e[i][j][3] = b[i][j] && b[i + 1][j];
}
}
ff(0, 0);
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
if(!vis[i][j]){
mx = n; Mx = 0;
ff(i, j);
ans += 1LL * mx * (n - Mx);
}
}
}
printf("%lld\n", ans);
}
Compilation message (stderr)
raspad.cpp: In function 'int main()':
raspad.cpp:18:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
raspad.cpp:19:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; i++) scanf("%s", b[i] + 1);
^
# | 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... |