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 = 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 (stderr)
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 |
---|
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |