#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
#define mp make_pair
const int N = 1e5 + 5, M = 54, mod = 1e9 + 7; //!
int t, ok[N][M],up[N][M], down[N][M];
pii p[N][M];
char c[N][M];
pii find(int x,int y) {
return (p[x][y] == mp(x, y) ? p[x][y] : p[x][y] = find(p[x][y].f, p[x][y].s));
}
void merge(int x, int y, int i, int j) {
pii p1 = find(x, y), p2 = find(i, j);
p[p2.f][p2.s] = p1;
ok[p1.f][p1.s] &= ok[p2.f][p2.s];
up[p1.f][p1.s] = min(up[p1.f][p1.s], up[p2.f][p2.s]);
down[p1.f][p1.s] = max(down[p1.f][p1.s], down[p2.f][p2.s]);
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n, m;
long long ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> c[i][j];
if(c[i][j] == '1') ans += (long long)i * (n - i + 1);
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(c[i][j] == '1') {
if(c[i + 1][j] == '1') {
ans -= (long long)i * (n - i);
}
if(c[i][j + 1] == '1') {
ans -= (long long)i * (n - i + 1);
}
}
}
}
for(int i = 0; i <= n + 1; i++) {
for(int j = 0; j <= m + 1; j++) {
p[i][j] = {i, j}, up[i][j] = down[i][j] = i;
ok[i][j] = (i && j && (i < n + 1) && (j < m + 1));
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(c[i][j] != '1' || c[i + 1][j] != '1') merge(i + 1, j, i + 1, j + 1);
if(c[i][j] != '1' || c[i][j + 1] != '1') merge(i, j + 1, i + 1, j + 1);
if(c[i][j] != '1' || c[i - 1][j] != '1') merge(i, j, i, j + 1);
if(c[i][j] != '1' || c[i][j - 1] != '1') merge(i, j, i + 1, j);
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(find(i, j) == make_pair(i, j)) {
if(ok[i][j]) {
ans += (long long)(up[i][j] - 1) * (n - down[i][j] + 1);
}
}
}
}
cout << ans;
}
Compilation message
raspad.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
22 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
7 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
836 KB |
Output is correct |
9 |
Correct |
7 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
7 ms |
1492 KB |
Output is correct |
12 |
Correct |
5 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
56548 KB |
Output is correct |
2 |
Correct |
240 ms |
112964 KB |
Output is correct |
3 |
Correct |
212 ms |
113216 KB |
Output is correct |
4 |
Correct |
205 ms |
101608 KB |
Output is correct |
5 |
Correct |
75 ms |
34096 KB |
Output is correct |
6 |
Correct |
226 ms |
113136 KB |
Output is correct |
7 |
Correct |
197 ms |
112812 KB |
Output is correct |
8 |
Correct |
193 ms |
90480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
7 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
836 KB |
Output is correct |
9 |
Correct |
7 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
7 ms |
1492 KB |
Output is correct |
12 |
Correct |
5 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1408 KB |
Output is correct |
14 |
Correct |
50 ms |
56548 KB |
Output is correct |
15 |
Correct |
240 ms |
112964 KB |
Output is correct |
16 |
Correct |
212 ms |
113216 KB |
Output is correct |
17 |
Correct |
205 ms |
101608 KB |
Output is correct |
18 |
Correct |
75 ms |
34096 KB |
Output is correct |
19 |
Correct |
226 ms |
113136 KB |
Output is correct |
20 |
Correct |
197 ms |
112812 KB |
Output is correct |
21 |
Correct |
193 ms |
90480 KB |
Output is correct |
22 |
Correct |
470 ms |
93120 KB |
Output is correct |
23 |
Correct |
610 ms |
116848 KB |
Output is correct |
24 |
Correct |
482 ms |
116412 KB |
Output is correct |
25 |
Correct |
660 ms |
117232 KB |
Output is correct |
26 |
Correct |
532 ms |
116248 KB |
Output is correct |
27 |
Correct |
551 ms |
116140 KB |
Output is correct |
28 |
Correct |
508 ms |
116016 KB |
Output is correct |
29 |
Correct |
704 ms |
117524 KB |
Output is correct |
30 |
Correct |
473 ms |
116248 KB |
Output is correct |
31 |
Correct |
521 ms |
116180 KB |
Output is correct |
32 |
Correct |
566 ms |
116244 KB |
Output is correct |
33 |
Correct |
525 ms |
104984 KB |
Output is correct |
34 |
Correct |
557 ms |
105340 KB |
Output is correct |