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_rectangles(vvi a) {
int n = a.size();
int m = a[0].size();
vector<vvi> validRows(m, vvi(m));
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[vec[idx]][next->first].push_back(i);
}
if (next != rec.begin()) {
next--;
if (vec[idx] - next->first > 1) {
validRows[next->first][vec[idx]].push_back(i);
}
}
rec[vec[idx]] = val;
}
}
}
vector<vvi> validCols(n, vvi(n));
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[vec[idx]][next->first].push_back(j);
}
if (next != rec.begin()) {
next--;
if (vec[idx] - next->first > 1) {
validCols[next->first][vec[idx]].push_back(j);
}
}
rec[vec[idx]] = val;
}
}
}
ll result = 0;
for (int j1 = 0; j1 < m; j1++) {
for (int j2 = j1 + 2; j2 < m; j2++) {
const auto& valid = validRows[j1][j2];
loop(idx, valid.size()) {
int furthest = idx;
while (furthest < valid.size() - 1 && valid[furthest + 1] == valid[furthest] + 1) {
furthest++;
}
for (int idx2 = idx; idx2 <= furthest; idx2++) {
const auto& relevantCols = validCols[valid[idx] - 1][valid[idx2] + 1];
auto start = lower_bound(all(relevantCols), j1 + 1);
auto end = lower_bound(all(relevantCols), j2 - 1);
if (start == relevantCols.end() || end == relevantCols.end() || *start != j1 + 1 || *end != j2 - 1 || (end - start) != j2 - j1 - 2) {
}
else {
result++;
}
}
}
}
}
// 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;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(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:78:13: note: in expansion of macro 'loop'
78 | loop(idx, vec.size()) {
| ^~~~
rect.cpp:85:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | 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:105:13: note: in expansion of macro 'loop'
105 | loop(idx, valid.size()) {
| ^~~~
rect.cpp:107:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | while (furthest < valid.size() - 1 && valid[furthest + 1] == valid[furthest] + 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... |