#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
bool swapped = false;
if (n > m) {
swapped = true;
swap(n, m);
}
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < (swapped ? m : n); ++i) {
for (int j = 0; j < (swapped ? n : m); ++j) {
if (!swapped) {
cin >> a[i][j];
} else {
cin >> a[j][i];
}
}
}
auto adj = [](int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2) < 2;
};
ll ans = 0;
if (n == 1) {
for (int i = 0; i < m;) {
int j = i + 1;
while (j < m && a[0][j] < a[0][j - 1]) {
j += 1;
}
int len = j - i;
ans += len * ((ll)len - 1) / 2;
i = j;
}
for (int i = m - 1; i > -1;) {
int j = i - 1;
while (j > -1 && a[0][j] < a[0][j + 1]) {
j -= 1;
}
int len = i - j;
ans += len * ((ll)len - 1) / 2;
i = j;
}
ans += m;
cout << ans;
return 0;
}
for (int x1 = 0; x1 < n; ++x1) {
for (int y1 = 0; y1 < m; ++y1) {
for (int x2 = x1; x2 < n; ++x2) {
set<array<int, 3>> st;
int cnt = 0;
for (int y2 = y1; y2 < m; ++y2) {
for (int x = x1; x <= x2; ++x) {
auto it = st.lower_bound({a[x][y2], -1, -1});
if (it != st.end()) {
if (it != st.begin()) {
cnt -= !adj((*it)[1], (*it)[2], (*prev(it))[1], (*prev(it))[2]);
}
cnt += !adj(x, y2, (*it)[1], (*it)[2]);
}
if (it != st.begin()) {
it = prev(it);
cnt += !adj(x, y2, (*it)[1], (*it)[2]);
}
st.insert({a[x][y2], x, y2});
}
ans += cnt == 0;
}
}
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
724 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1316 ms |
400 KB |
Output is correct |
10 |
Correct |
1065 ms |
404 KB |
Output is correct |
11 |
Correct |
221 ms |
396 KB |
Output is correct |
12 |
Correct |
174 ms |
396 KB |
Output is correct |
13 |
Correct |
1120 ms |
396 KB |
Output is correct |
14 |
Correct |
545 ms |
384 KB |
Output is correct |
15 |
Correct |
1103 ms |
396 KB |
Output is correct |
16 |
Correct |
1218 ms |
404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1316 ms |
400 KB |
Output is correct |
10 |
Correct |
1065 ms |
404 KB |
Output is correct |
11 |
Correct |
221 ms |
396 KB |
Output is correct |
12 |
Correct |
174 ms |
396 KB |
Output is correct |
13 |
Correct |
1120 ms |
396 KB |
Output is correct |
14 |
Correct |
545 ms |
384 KB |
Output is correct |
15 |
Correct |
1103 ms |
396 KB |
Output is correct |
16 |
Correct |
1218 ms |
404 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Execution timed out |
5091 ms |
716 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1316 ms |
400 KB |
Output is correct |
10 |
Correct |
1065 ms |
404 KB |
Output is correct |
11 |
Correct |
221 ms |
396 KB |
Output is correct |
12 |
Correct |
174 ms |
396 KB |
Output is correct |
13 |
Correct |
1120 ms |
396 KB |
Output is correct |
14 |
Correct |
545 ms |
384 KB |
Output is correct |
15 |
Correct |
1103 ms |
396 KB |
Output is correct |
16 |
Correct |
1218 ms |
404 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Execution timed out |
5091 ms |
716 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |