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 "rect.h"
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
//#define int long long
#define ll long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
long long count_naieve(vvi a) {
int n = a.size();
int m = a[0].size();
vector<set<ii>> validRows(n);
for (int i = 1; i < n - 1; i++) {
map<int, vi, greater<int>> values;
loop(j, m) {
values[a[i][j]].push_back(j);
}
map<int, int> rec;
for (auto [val, vec] : values) {
loop(idx, vec.size()) {
if (rec.size() == 0) {
rec[vec[idx]] = val;
continue;
}
auto next = rec.upper_bound(vec[idx]);
if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
validRows[i].insert({vec[idx], next->first});
}
next--;
if (vec[idx] - next->first > 1)
validRows[i].insert({next->first, vec[idx]});
rec[vec[idx]] = val;
}
}
}
vector<set<ii>> validCols(m);
for (int j = 1; j < m - 1; j++) {
map<int, vi, greater<int>> values;
loop(i, n) {
values[a[i][j]].push_back(i);
}
map<int, int> rec;
for (auto [val, vec] : values) {
loop(idx, vec.size()) {
if (rec.size() == 0) {
rec[vec[idx]] = val;
continue;
}
auto next = rec.upper_bound(vec[idx]);
if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
validCols[j].insert({vec[idx], next->first});
}
next--;
if (vec[idx] - next->first > 1)
validCols[j].insert({next->first, vec[idx]});
rec[vec[idx]] = val;
}
}
}
ll result = 0;
for (int i = 1; i < n - 1; i++) {
for (auto [x, y] : validRows[i]) {
int furthest = i;
while (validRows[furthest + 1].count({x, y})) furthest++;
vi freq(furthest - i + 1);
for (int j = x + 1; j <= y - 1; j++) {
for (int i2 = i; i2 <= furthest; i2++) {
if (validCols[j].count({i - 1, i2 + 1}))
freq[i2 - i]++;
}
}
ll extra = 0;
for (int k = 0; k < furthest - i + 1; k++) {
if (freq[k] == y - x - 1)
extra++;
}
result += extra;
}
}
return result;
}
long long count_rectangles(vvi a) {
int n = a.size();
int m = a[0].size();
ll result = 0;
for (int r1 = 1; r1 < n - 1; r1++) {
for (int r2 = r1; r2 < n - 1; r2++) {
for (int c1 = 1; c1 < m - 1; c1++) {
for (int c2 = c1; c2 < m - 1; c2++) {
bool success = true;
for (int i = r1; i <= r2; i++) {
for (int j = c1; j <= c2; j++) {
if (a[i][j] >= a[i][c1 - 1] || a[i][j] >= a[i][c2 + 1] || a[i][j] >= a[r1 - 1][j] || a[i][j] >= a[r2 + 1][j]) {
success = false;
break;
}
}
if (!success) {
break;
}
}
if (success) {
result++;
}
}
}
}
}
return result;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_naieve(vvi)':
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define loop(X, N) for(int X = 0; X < (N); X++)
| ^
rect.cpp:47:13: note: in expansion of macro 'loop'
47 | loop(idx, vec.size()) {
| ^~~~
rect.cpp:54:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
| ~~~~^~~~~~~~~~~~~~~~~
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | #define loop(X, N) for(int X = 0; X < (N); X++)
| ^
rect.cpp:75:13: note: in expansion of macro 'loop'
75 | loop(idx, vec.size()) {
| ^~~~
rect.cpp:82:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
| ~~~~^~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |