#include <bits/stdc++.h>
using namespace std;
const int inf = (int) 1.001e9;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
vector<vector<long long>> f[1 << 4];
long long GetSum(int x1, int y1, int x2, int y2, int mask) {
if (x1 > x2 || y1 > y2) {
return 0LL;
}
long long res = f[mask][x2][y2];
if (x1 > 0) {
res -= f[mask][x1 - 1][y2];
}
if (y1 > 0) {
res -= f[mask][x2][y1 - 1];
}
if (x1 > 0 && y1 > 0) {
res += f[mask][x1 - 1][y1 - 1];
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
if (n > m) {
vector<vector<int>> b(m, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
b[j][i] = a[i][j];
}
}
a = b;
swap(n, m);
}
for (int mask = 0; mask < (1 << 4); mask++) {
f[mask] = vector<vector<long long>>(n, vector<long long>(m));
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int mask = 0; mask < (1 << 4); mask++) {
if (i > 0) {
f[mask][i][j] += f[mask][i - 1][j];
}
if (j > 0) {
f[mask][i][j] += f[mask][i][j - 1];
}
if (i > 0 && j > 0) {
f[mask][i][j] -= f[mask][i - 1][j - 1];
}
f[mask][i][j] -= a[i][j];
int mx = 0;
int cnt = 0;
for (int d = 0; d < 4; d++) {
if (mask >> d & 1) {
int ni = i + di[d];
int nj = j + dj[d];
if (0 <= ni && ni < n && 0 <= nj && nj < m && a[ni][nj] < a[i][j]) {
mx = max(mx, a[ni][nj]);
}
if (0 <= ni && ni < n && 0 <= nj && nj < m && a[ni][nj] > a[i][j]) {
cnt += 1;
}
}
}
if (cnt == 0) {
mx += inf + a[i][j];
}
f[mask][i][j] += mx;
}
}
}
long long res = n * m;
for (int x = 0; x < n; x++) {
map<long long, int> cnt;
for (int y = 0; y < m; y++) {
if (y > 0) {
res += cnt[-(GetSum(x, y, x, y, 1 << 3) - inf + GetSum(x, 0, x, y - 1, (1 << 2) + (1 << 3)))];
}
cnt[GetSum(x, y, x, y, 1 << 2) - GetSum(x, 0, x, y, (1 << 2) + (1 << 3))] += 1;
}
}
for (int l = 0; l < n; l++) {
for (int r = l + 1; r < n; r++) {
map<long long, int> cnt;
for (int i = 0; i < m; i++) {
if (GetSum(l, i, l, i, 1 << 0) + GetSum(r, i, r, i, 1 << 1) + GetSum(l + 1, i, r - 1, i, (1 << 0) + (1 << 1)) == inf) {
res += 1;
}
if (i > 0) {
res += cnt[-(-inf + GetSum(l, i, l, i, (1 << 0) + (1 << 3)) + GetSum(r, i, r, i, (1 << 1) + (1 << 3)) + GetSum(l + 1, i, r - 1, i, (1 << 0) + (1 << 1) + (1 << 3)) + GetSum(l + 1, 0, r - 1, i - 1, (1 << 0) + (1 << 1) + (1 << 2) + (1 << 3))
+ GetSum(l, 0, l, i - 1, (1 << 0) + (1 << 2) + (1 << 3)) + GetSum(r, 0, r, i - 1, (1 << 1) + (1 << 2) + (1 << 3)))];
}
cnt[GetSum(l, i, l, i, (1 << 0) + (1 << 2)) + GetSum(r, i, r, i, (1 << 1) + (1 << 2)) + GetSum(l + 1, i, r - 1, i, (1 << 0) + (1 << 1) + (1 << 2)) - GetSum(l + 1, 0, r - 1, i, (1 << 0) + (1 << 1) + (1 << 2) + (1 << 3))
- GetSum(l, 0, l, i, (1 << 0) + (1 << 2) + (1 << 3)) - GetSum(r, 0, r, i, (1 << 1) + (1 << 2) + (1 << 3))] += 1;
}
}
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
7184 KB |
Output is correct |
3 |
Correct |
20 ms |
10256 KB |
Output is correct |
4 |
Correct |
10 ms |
7692 KB |
Output is correct |
5 |
Correct |
12 ms |
7696 KB |
Output is correct |
6 |
Correct |
19 ms |
9220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
472 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
472 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
24 ms |
1316 KB |
Output is correct |
19 |
Correct |
7 ms |
1372 KB |
Output is correct |
20 |
Correct |
10 ms |
1444 KB |
Output is correct |
21 |
Correct |
20 ms |
1452 KB |
Output is correct |
22 |
Correct |
27 ms |
1372 KB |
Output is correct |
23 |
Correct |
27 ms |
1308 KB |
Output is correct |
24 |
Correct |
26 ms |
1372 KB |
Output is correct |
25 |
Correct |
32 ms |
1372 KB |
Output is correct |
26 |
Correct |
33 ms |
1372 KB |
Output is correct |
27 |
Correct |
31 ms |
1464 KB |
Output is correct |
28 |
Correct |
33 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
472 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
24 ms |
1316 KB |
Output is correct |
19 |
Correct |
7 ms |
1372 KB |
Output is correct |
20 |
Correct |
10 ms |
1444 KB |
Output is correct |
21 |
Correct |
20 ms |
1452 KB |
Output is correct |
22 |
Correct |
27 ms |
1372 KB |
Output is correct |
23 |
Correct |
27 ms |
1308 KB |
Output is correct |
24 |
Correct |
26 ms |
1372 KB |
Output is correct |
25 |
Correct |
32 ms |
1372 KB |
Output is correct |
26 |
Correct |
33 ms |
1372 KB |
Output is correct |
27 |
Correct |
31 ms |
1464 KB |
Output is correct |
28 |
Correct |
33 ms |
1368 KB |
Output is correct |
29 |
Correct |
13 ms |
9172 KB |
Output is correct |
30 |
Correct |
165 ms |
7260 KB |
Output is correct |
31 |
Correct |
535 ms |
7444 KB |
Output is correct |
32 |
Correct |
12 ms |
7576 KB |
Output is correct |
33 |
Correct |
171 ms |
7368 KB |
Output is correct |
34 |
Correct |
392 ms |
7508 KB |
Output is correct |
35 |
Correct |
304 ms |
5004 KB |
Output is correct |
36 |
Correct |
471 ms |
7260 KB |
Output is correct |
37 |
Correct |
675 ms |
7260 KB |
Output is correct |
38 |
Correct |
742 ms |
7388 KB |
Output is correct |
39 |
Correct |
692 ms |
7316 KB |
Output is correct |
40 |
Correct |
712 ms |
7260 KB |
Output is correct |
41 |
Correct |
689 ms |
7260 KB |
Output is correct |
42 |
Correct |
730 ms |
7320 KB |
Output is correct |
43 |
Correct |
691 ms |
7304 KB |
Output is correct |
44 |
Correct |
723 ms |
7336 KB |
Output is correct |