#include <bits/stdc++.h>
using namespace std;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
const int L1 = 17;
const int L2 = 10;
vector<vector<int>> st[L1][L2];
vector<vector<long long>> f[1 << 4];
int logs[50005];
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) {
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 i = 2; i <= max(n, m); i++) {
logs[i] = logs[i >> 1] + 1;
}
for (int i = 0; i < L1; i++) {
for (int j = 0; j < L2; j++) {
st[i][j] = vector<vector<int>>(n, vector<int>(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++) {
st[0][0][i][j] = a[i][j];
}
}
for (int k = 1; k < L1; k++) {
for (int i = 0; i + (1 << k) <= n; i++) {
for (int j = 0; j < m; j++) {
st[k][0][i][j] = max(st[k - 1][0][i][j], st[k - 1][0][i + (1 << (k - 1))][j]);
}
}
}
for (int k = 1; k < L2; k++) {
for (int t = 0; t < L1; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j + (1 << k) <= m; j++) {
st[t][k][i][j] = max(st[t][k - 1][i][j], st[t][k - 1][i][j + (1 << (k - 1))]);
}
}
}
}
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) {
if (x1 == x2 && y1 == y2) {
res += 1;
continue;
}
long long s = 0;
int mx = Query(x1, y1, x2, y2);
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) {
res += 1;
}
}
}
}
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5069 ms |
513336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
22 ms |
15708 KB |
Output is correct |
8 |
Correct |
22 ms |
15708 KB |
Output is correct |
9 |
Correct |
12 ms |
1884 KB |
Output is correct |
10 |
Correct |
12 ms |
1884 KB |
Output is correct |
11 |
Correct |
17 ms |
8028 KB |
Output is correct |
12 |
Correct |
16 ms |
8024 KB |
Output is correct |
13 |
Correct |
12 ms |
1624 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
13 ms |
1884 KB |
Output is correct |
16 |
Correct |
13 ms |
1980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
22 ms |
15708 KB |
Output is correct |
8 |
Correct |
22 ms |
15708 KB |
Output is correct |
9 |
Correct |
12 ms |
1884 KB |
Output is correct |
10 |
Correct |
12 ms |
1884 KB |
Output is correct |
11 |
Correct |
17 ms |
8028 KB |
Output is correct |
12 |
Correct |
16 ms |
8024 KB |
Output is correct |
13 |
Correct |
12 ms |
1624 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
13 ms |
1884 KB |
Output is correct |
16 |
Correct |
13 ms |
1980 KB |
Output is correct |
17 |
Correct |
308 ms |
73008 KB |
Output is correct |
18 |
Correct |
262 ms |
7000 KB |
Output is correct |
19 |
Correct |
260 ms |
10332 KB |
Output is correct |
20 |
Correct |
255 ms |
6724 KB |
Output is correct |
21 |
Correct |
248 ms |
6492 KB |
Output is correct |
22 |
Correct |
256 ms |
6488 KB |
Output is correct |
23 |
Correct |
237 ms |
6332 KB |
Output is correct |
24 |
Correct |
197 ms |
5976 KB |
Output is correct |
25 |
Correct |
259 ms |
6744 KB |
Output is correct |
26 |
Correct |
265 ms |
6492 KB |
Output is correct |
27 |
Correct |
273 ms |
6752 KB |
Output is correct |
28 |
Correct |
259 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
22 ms |
15708 KB |
Output is correct |
8 |
Correct |
22 ms |
15708 KB |
Output is correct |
9 |
Correct |
12 ms |
1884 KB |
Output is correct |
10 |
Correct |
12 ms |
1884 KB |
Output is correct |
11 |
Correct |
17 ms |
8028 KB |
Output is correct |
12 |
Correct |
16 ms |
8024 KB |
Output is correct |
13 |
Correct |
12 ms |
1624 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
13 ms |
1884 KB |
Output is correct |
16 |
Correct |
13 ms |
1980 KB |
Output is correct |
17 |
Correct |
308 ms |
73008 KB |
Output is correct |
18 |
Correct |
262 ms |
7000 KB |
Output is correct |
19 |
Correct |
260 ms |
10332 KB |
Output is correct |
20 |
Correct |
255 ms |
6724 KB |
Output is correct |
21 |
Correct |
248 ms |
6492 KB |
Output is correct |
22 |
Correct |
256 ms |
6488 KB |
Output is correct |
23 |
Correct |
237 ms |
6332 KB |
Output is correct |
24 |
Correct |
197 ms |
5976 KB |
Output is correct |
25 |
Correct |
259 ms |
6744 KB |
Output is correct |
26 |
Correct |
265 ms |
6492 KB |
Output is correct |
27 |
Correct |
273 ms |
6752 KB |
Output is correct |
28 |
Correct |
259 ms |
6744 KB |
Output is correct |
29 |
Execution timed out |
5049 ms |
513360 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |