#include <bits/stdc++.h>
using namespace std;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
//vector<vector<long long>> f[1 << 4];
long long f[1 << 4][50005][1];
int logs[50005];
int mx[50005][250];
/* int Query(int x1, int y1, int x2, int y2) {
int kx = logs[x2 - x1 + 1];
int ky = logs[y2 - y1 + 1];
return max({st[kx][ky][x1][y1], st[kx][ky][x2 - (1 << kx) + 1][y1], st[kx][ky][x1][y2 - (1 << ky) + 1],
st[kx][ky][x2 - (1 << kx) + 1][y2 - (1 << ky) + 1]});
} */
long long GetSum(int x1, int y1, int x2, int y2, int mask) {
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;
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]);
}
}
}
f[mask][i][j] += mx;
}
}
}
int res = 0;
for (int x1 = 0; x1 < n; ++x1) {
for (int y1 = 0; y1 < m; ++y1) {
for (int x2 = x1; x2 < n; ++x2) {
for (int y2 = y1; y2 < m; ++y2) {
mx[x2][y2] = a[x2][y2];
if (x1 == x2 && y1 == y2) {
res += 1;
continue;
}
if (x1 < x2) {
mx[x2][y2] = max(mx[x2][y2], mx[x2 - 1][y2]);
}
if (y1 < y2) {
mx[x2][y2] = max(mx[x2][y2], mx[x2][y2 - 1]);
}
if (x1 < x2 && y1 < y2) {
mx[x2][y2] = max(mx[x2][y2], mx[x2 - 1][y2 - 1]);
}
long long s = 0;
if (x1 == x2) {
s += GetSum(x1, y1, x1, y1, 1 << 2);
s += GetSum(x1, y2, x1, y2, 1 << 3);
if (y1 + 1 < y2) {
s += GetSum(x1, y1 + 1, x1, y2 - 1, (1 << 2) + (1 << 3));
}
} else if (y1 == y2) {
s += GetSum(x1, y1, x1, y1, 1 << 0);
s += GetSum(x2, y1, x2, y1, 1 << 1);
if (x1 + 1 < x2) {
s += GetSum(x1 + 1, y1, x2 - 1, y1, (1 << 0) + (1 << 1));
}
} else {
s += GetSum(x1, y1, x1, y1, (1 << 0) + (1 << 2));
s += GetSum(x1, y2, x1, y2, (1 << 0) + (1 << 3));
s += GetSum(x2, y1, x2, y1, (1 << 1) + (1 << 2));
s += GetSum(x2, y2, x2, y2, (1 << 1) + (1 << 3));
if (x1 + 1 < x2) {
s += GetSum(x1 + 1, y1, x2 - 1, y1, (1 << 0) + (1 << 1) + (1 << 2));
s += GetSum(x1 + 1, y2, x2 - 1, y2, (1 << 0) + (1 << 1) + (1 << 3));
}
if (y1 + 1 < y2) {
s += GetSum(x1, y1 + 1, x1, y2 - 1, (1 << 2) + (1 << 3) + (1 << 0));
s += GetSum(x2, y1 + 1, x2, y2 - 1, (1 << 2) + (1 << 3) + (1 << 1));
}
if (x1 + 1 < x2 && y1 + 1 < y2) {
s += GetSum(x1 + 1, y1 + 1, x2 - 1, y2 - 1, (1 << 0) + (1 << 1) + (1 << 2) + (1 << 3));
}
}
if (s == -mx[x2][y2]) {
res += 1;
}
}
}
}
}
cout << res << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:67:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
67 | f[mask][i][j] += f[mask][i][j - 1];
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:67:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
Main.cpp:70:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
70 | f[mask][i][j] -= f[mask][i - 1][j - 1];
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:70:25: warning: array subscript 1 is above array bounds of 'long long int [1]' [-Warray-bounds]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5100 ms |
60092 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |