#include <bits/stdc++.h>
using namespace std;
const int N = 50001;
const int M = 10000001;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int h, w;
struct Table {
int a[N];
int *operator[](int i) {
return a + i * w;
}
Table() {
memset(a, 0, sizeof a);
}
} a, b, c[4], d[4];
int rmin[N], rmax[N];
long long rsum[N], rsumup[N], rsumdown[N];
int rcnt[N], rcntup[N], rcntdown[N];
int get_delta(int x, int y, int z) {
int mn = M;
for (int i = 0; i < 4; ++i) {
if ((z >> i) & 1) continue;
int xx = x + dx[i], yy = y + dy[i];
if (xx >= 0 && xx < h && yy >= 0 && yy < w && a[xx][yy] > a[x][y] && a[xx][yy] < mn) mn = a[xx][yy];
}
return mn == M ? 0 : mn - a[x][y];
}
int main() {
scanf("%d%d", &h, &w);
if (w > h) {
swap(w, h);
for (int x = 0; x < w; ++x) {
for (int y = 0; y < h; ++y) {
scanf("%d", &a[y][x]);
}
}
} else {
for (int x = 0; x < h; ++x) {
for (int y = 0; y < w; ++y) {
scanf("%d", &a[x][y]);
}
}
}
for (int x = 0; x < h; ++x) {
for (int y = 0; y < w; ++y) {
b[x][y] = get_delta(x, y, 0);
}
}
for (int i = 0; i < 4; ++i) {
for (int x = 0; x < h; ++x) {
for (int y = 0; y < w; ++y) {
c[i][x][y] = get_delta(x, y, 1 << i);
}
}
}
for (int i = 0; i < 4; ++i) {
for (int x = 0; x < h; ++x) {
for (int y = 0; y < w; ++y) {
d[i][x][y] = get_delta(x, y, (1 << i) | (1 << ((i + 1) % 4)));
}
}
}
long long ans = h * w;
for (int x = 0; x < h; ++x) {
int len = 0;
for (int y = 1; y < w; ++y) {
++len;
if (y == w - 1 || (a[x][y] < a[x][y + 1]) != (a[x][y - 1] < a[x][y])) {
ans += 1ll * len * (len + 1) / 2;
len = 0;
}
}
}
for (int y = 0; y < w; ++y) {
int len = 0;
for (int x = 1; x < h; ++x) {
++len;
if (x == h - 1 || (a[x][y] < a[x + 1][y]) != (a[x - 1][y] < a[x][y])) {
ans += 1ll * len * (len + 1) / 2;
len = 0;
}
}
}
for (int y1 = 0; y1 < w; ++y1) {
for (int x = 0; x < h; ++x) {
rmin[x] = rmax[x] = a[x][y1];
}
for (int x = 0; x < h; ++x) {
rcntup[x] = d[3][x][y1] == 0;
rsumup[x] = d[3][x][y1];
rcntdown[x] = d[2][x][y1] == 0;
rsumdown[x] = d[2][x][y1];
rcnt[x] = c[3][x][y1] == 0;
rsum[x] = c[3][x][y1];
}
for (int y2 = y1 + 1; y2 < w; ++y2) {
for (int x = 0; x < h; ++x) {
rmin[x] = min(rmin[x], a[x][y2]);
rmax[x] = max(rmax[x], a[x][y2]);
if (y2 - 1 != y1) {
rcntup[x] += c[0][x][y2 - 1] == 0;
rsumup[x] += c[0][x][y2 - 1];
rcntdown[x] += c[2][x][y2 - 1] == 0;
rsumdown[x] += c[2][x][y2 - 1];
rcnt[x] += b[x][y2 - 1] == 0;
rsum[x] += b[x][y2 - 1];
}
}
for (int x = 0; x < h; ++x) {
rcntup[x] += d[0][x][y2] == 0;
rsumup[x] += d[0][x][y2];
rcntdown[x] += d[1][x][y2] == 0;
rsumdown[x] += d[1][x][y2];
rcnt[x] += c[1][x][y2] == 0;
rsum[x] += c[1][x][y2];
}
for (int x1 = 0; x1 < h; ++x1) {
long long sum = rsumup[x1];
int cnt = rcntup[x1];
int cmin = rmin[x1], cmax = rmax[x1];
for (int x2 = x1 + 1; x2 < h; ++x2) {
cmin = min(cmin, rmin[x2]);
cmax = max(cmax, rmax[x2]);
ans += sum + rsumdown[x2] == cmax - cmin && cnt + rcntdown[x2] == 1;
cnt += rcnt[x2];
sum += rsum[x2];
if (cnt >= 2 || sum >= M) break;
}
}
for (int x = 0; x < h; ++x) {
rcntup[x] -= d[0][x][y2] == 0;
rsumup[x] -= d[0][x][y2];
rcntdown[x] -= d[1][x][y2] == 0;
rsumdown[x] -= d[1][x][y2];
rcnt[x] -= c[1][x][y2] == 0;
rsum[x] -= c[1][x][y2];
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &h, &w);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &a[y][x]);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d", &a[x][y]);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
12 ms |
4308 KB |
Output is correct |
3 |
Correct |
12 ms |
4692 KB |
Output is correct |
4 |
Correct |
12 ms |
4684 KB |
Output is correct |
5 |
Correct |
12 ms |
4684 KB |
Output is correct |
6 |
Correct |
14 ms |
4808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2260 KB |
Output is correct |
9 |
Correct |
2 ms |
2260 KB |
Output is correct |
10 |
Correct |
3 ms |
2260 KB |
Output is correct |
11 |
Correct |
3 ms |
2260 KB |
Output is correct |
12 |
Correct |
2 ms |
2260 KB |
Output is correct |
13 |
Correct |
3 ms |
2260 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
2 ms |
2260 KB |
Output is correct |
16 |
Correct |
3 ms |
2276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2260 KB |
Output is correct |
9 |
Correct |
2 ms |
2260 KB |
Output is correct |
10 |
Correct |
3 ms |
2260 KB |
Output is correct |
11 |
Correct |
3 ms |
2260 KB |
Output is correct |
12 |
Correct |
2 ms |
2260 KB |
Output is correct |
13 |
Correct |
3 ms |
2260 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
2 ms |
2260 KB |
Output is correct |
16 |
Correct |
3 ms |
2276 KB |
Output is correct |
17 |
Correct |
3 ms |
2516 KB |
Output is correct |
18 |
Correct |
6 ms |
2260 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
36 ms |
2292 KB |
Output is correct |
21 |
Correct |
37 ms |
2276 KB |
Output is correct |
22 |
Correct |
26 ms |
2296 KB |
Output is correct |
23 |
Correct |
25 ms |
2260 KB |
Output is correct |
24 |
Correct |
8 ms |
2292 KB |
Output is correct |
25 |
Correct |
8 ms |
2268 KB |
Output is correct |
26 |
Correct |
9 ms |
2292 KB |
Output is correct |
27 |
Correct |
8 ms |
2296 KB |
Output is correct |
28 |
Correct |
8 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2260 KB |
Output is correct |
9 |
Correct |
2 ms |
2260 KB |
Output is correct |
10 |
Correct |
3 ms |
2260 KB |
Output is correct |
11 |
Correct |
3 ms |
2260 KB |
Output is correct |
12 |
Correct |
2 ms |
2260 KB |
Output is correct |
13 |
Correct |
3 ms |
2260 KB |
Output is correct |
14 |
Correct |
2 ms |
2260 KB |
Output is correct |
15 |
Correct |
2 ms |
2260 KB |
Output is correct |
16 |
Correct |
3 ms |
2276 KB |
Output is correct |
17 |
Correct |
3 ms |
2516 KB |
Output is correct |
18 |
Correct |
6 ms |
2260 KB |
Output is correct |
19 |
Correct |
4 ms |
2260 KB |
Output is correct |
20 |
Correct |
36 ms |
2292 KB |
Output is correct |
21 |
Correct |
37 ms |
2276 KB |
Output is correct |
22 |
Correct |
26 ms |
2296 KB |
Output is correct |
23 |
Correct |
25 ms |
2260 KB |
Output is correct |
24 |
Correct |
8 ms |
2292 KB |
Output is correct |
25 |
Correct |
8 ms |
2268 KB |
Output is correct |
26 |
Correct |
9 ms |
2292 KB |
Output is correct |
27 |
Correct |
8 ms |
2296 KB |
Output is correct |
28 |
Correct |
8 ms |
2260 KB |
Output is correct |
29 |
Correct |
12 ms |
4308 KB |
Output is correct |
30 |
Correct |
41 ms |
2316 KB |
Output is correct |
31 |
Correct |
104 ms |
2280 KB |
Output is correct |
32 |
Correct |
765 ms |
3344 KB |
Output is correct |
33 |
Correct |
1552 ms |
2668 KB |
Output is correct |
34 |
Correct |
1190 ms |
2672 KB |
Output is correct |
35 |
Correct |
59 ms |
2516 KB |
Output is correct |
36 |
Correct |
99 ms |
2636 KB |
Output is correct |
37 |
Correct |
114 ms |
2604 KB |
Output is correct |
38 |
Correct |
121 ms |
2516 KB |
Output is correct |
39 |
Correct |
132 ms |
2608 KB |
Output is correct |
40 |
Correct |
125 ms |
2620 KB |
Output is correct |
41 |
Correct |
106 ms |
2624 KB |
Output is correct |
42 |
Correct |
122 ms |
2624 KB |
Output is correct |
43 |
Correct |
109 ms |
2624 KB |
Output is correct |
44 |
Correct |
110 ms |
2608 KB |
Output is correct |