#include <stdio.h>
#include <string.h>
#define NM 50000
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int min(int a, int b) { return a < b ? a : b; }
int aa[NM], bb[NM], dd[NM], bad[3][3][3][3][NM], cc[3][3][NM], n, m;
int count(int il, int ir, int j, int y, int z) {
int i, w, x, cnt;
if (ir - il < 3) {
cnt = 0;
for (i = il; i <= ir; i++) {
w = min(i - il, 2), x = min(ir - i, 2);
if (bad[w][x][y][z][i * m + j])
cnt++;
}
} else {
cnt = 0;
for (i = il; i < il + 2; i++)
if (bad[i - il][2][y][z][i * m + j])
cnt++;
for (i = ir - 1; i <= ir; i++)
if (bad[2][ir - i][y][z][i * m + j])
cnt++;
cnt += cc[y][z][(ir - 2) * m + j] - cc[y][z][(il + 1) * m + j];
}
return cnt;
}
int main() {
static int dp[3][3][2], dq[3][3][2];
int h, h_, i, j, i_, j_, i1, j1, il, jl, ir, jr, ij, ij_, ij1, k, w, x, y, y_, z, z_, c, c_, tmp, ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &aa[i * m + j]);
if (n > m) {
for (ij = 0; ij < n * m; ij++) {
i = ij / m, j = ij % m;
bb[j * n + i] = aa[i * m + j];
}
tmp = n, n = m, m = tmp;
memcpy(aa, bb, n * m * sizeof *bb);
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
ij = i * m + j;
for (w = 0; w < 3 && (il = i - w) >= 0; w++)
for (x = 0; x < 3 && (ir = i + x) < n; x++)
for (y = 0; y < 3 && (jl = j - y) >= 0; y++)
for (z = 0; z < 3 && (jr = j + z) < m; z++) {
k = 0;
for (h = 0; h < 4; h++) {
i_ = i + di[h], j_ = j + dj[h], ij_ = i_ * m + j_;
if (i_ >= il && i_ <= ir && j_ >= jl && j_ <= jr && aa[ij_] > aa[ij]) {
k++;
for (h_ = 0; h_ < 4; h_++) {
i1 = i_ + di[h_], j1 = j_ + dj[h_], ij1 = i1 * m + j1;
if (i1 >= il && i1 <= ir && j1 >= jl && j1 <= jr && aa[ij_] > aa[ij1] && aa[ij1] > aa[ij]) {
k--;
break;
}
}
}
}
if (k != 1) {
bad[w][x][y][z][ij] = 1;
if (w == 2 && x == 2)
cc[y][z][ij] = 1;
}
}
}
for (y = 0; y < 3; y++)
for (z = 0; z < 3; z++)
for (ij = m; ij < n * m; ij++)
cc[y][z][ij] += cc[y][z][ij - m];
ans = 0;
for (il = 0; il < n; il++)
for (ir = il; ir < n; ir++) {
memset(dp, 0, sizeof dp);
for (j = 0; j < m; j++) {
memset(dq, 0, sizeof dq);
c = count(il, ir, j, 0, 0);
if (c < 2)
dq[0][0][c]++;
c = count(il, ir, j, 0, 1);
if (c < 2)
dq[0][1][c]++;
c = count(il, ir, j, 0, 2);
if (c < 2)
dq[0][2][c]++;
for (y = 0; y < 3; y++)
for (z = 0; z < 3; z++)
for (c = 0; c < 2; c++) {
int v = dp[y][z][c];
if (v == 0)
continue;
if (z > 0) {
y_ = min(y + 1, 2), z_ = z - 1, c_ = c + count(il, ir, j, y_, z_);
if (c_ < 2)
dq[y_][z_][c_] += v;
}
if (z == 2) {
y_ = min(y + 1, 2), z_ = z, c_ = c + count(il, ir, j, y_, z_);
if (c_ < 2)
dq[y_][z_][c_] += v;
}
}
memcpy(dp, dq, sizeof dq);
ans += dp[0][0][1] + dp[1][0][1] + dp[2][0][1];
}
}
printf("%d\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:43:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &aa[i * m + j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
20 ms |
1084 KB |
Output is correct |
3 |
Correct |
20 ms |
2544 KB |
Output is correct |
4 |
Correct |
21 ms |
1480 KB |
Output is correct |
5 |
Correct |
22 ms |
2240 KB |
Output is correct |
6 |
Correct |
25 ms |
2624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
1128 KB |
Output is correct |
10 |
Correct |
7 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
832 KB |
Output is correct |
15 |
Correct |
8 ms |
1176 KB |
Output is correct |
16 |
Correct |
8 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
1128 KB |
Output is correct |
10 |
Correct |
7 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
832 KB |
Output is correct |
15 |
Correct |
8 ms |
1176 KB |
Output is correct |
16 |
Correct |
8 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
38 ms |
3072 KB |
Output is correct |
19 |
Correct |
29 ms |
2676 KB |
Output is correct |
20 |
Correct |
72 ms |
1056 KB |
Output is correct |
21 |
Correct |
30 ms |
1600 KB |
Output is correct |
22 |
Correct |
33 ms |
2912 KB |
Output is correct |
23 |
Correct |
31 ms |
2900 KB |
Output is correct |
24 |
Correct |
40 ms |
2436 KB |
Output is correct |
25 |
Correct |
46 ms |
3096 KB |
Output is correct |
26 |
Correct |
44 ms |
2996 KB |
Output is correct |
27 |
Correct |
43 ms |
3000 KB |
Output is correct |
28 |
Correct |
44 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
8 ms |
1128 KB |
Output is correct |
10 |
Correct |
7 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
6 ms |
1108 KB |
Output is correct |
14 |
Correct |
6 ms |
832 KB |
Output is correct |
15 |
Correct |
8 ms |
1176 KB |
Output is correct |
16 |
Correct |
8 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
340 KB |
Output is correct |
18 |
Correct |
38 ms |
3072 KB |
Output is correct |
19 |
Correct |
29 ms |
2676 KB |
Output is correct |
20 |
Correct |
72 ms |
1056 KB |
Output is correct |
21 |
Correct |
30 ms |
1600 KB |
Output is correct |
22 |
Correct |
33 ms |
2912 KB |
Output is correct |
23 |
Correct |
31 ms |
2900 KB |
Output is correct |
24 |
Correct |
40 ms |
2436 KB |
Output is correct |
25 |
Correct |
46 ms |
3096 KB |
Output is correct |
26 |
Correct |
44 ms |
2996 KB |
Output is correct |
27 |
Correct |
43 ms |
3000 KB |
Output is correct |
28 |
Correct |
44 ms |
3028 KB |
Output is correct |
29 |
Correct |
23 ms |
1236 KB |
Output is correct |
30 |
Correct |
263 ms |
18260 KB |
Output is correct |
31 |
Correct |
462 ms |
18536 KB |
Output is correct |
32 |
Correct |
37 ms |
2688 KB |
Output is correct |
33 |
Correct |
1100 ms |
5592 KB |
Output is correct |
34 |
Correct |
387 ms |
9164 KB |
Output is correct |
35 |
Correct |
281 ms |
10268 KB |
Output is correct |
36 |
Correct |
443 ms |
15220 KB |
Output is correct |
37 |
Correct |
447 ms |
18388 KB |
Output is correct |
38 |
Correct |
504 ms |
18508 KB |
Output is correct |
39 |
Correct |
437 ms |
18420 KB |
Output is correct |
40 |
Correct |
476 ms |
18580 KB |
Output is correct |
41 |
Correct |
446 ms |
18480 KB |
Output is correct |
42 |
Correct |
481 ms |
18576 KB |
Output is correct |
43 |
Correct |
445 ms |
18436 KB |
Output is correct |
44 |
Correct |
459 ms |
18576 KB |
Output is correct |