이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> lg{-1};
struct RMQ {
vector<vector<int>> sp;
void init (vector<int> &a) {
int n = a.size();
sp.resize(lg[n]+1, vector<int>(n));
sp[0] = a;
for (int i = 1; i < lg[n]+1; ++i) {
for (int j = 0; j < n - (1 << i) + 1; ++j) {
sp[i][j] = max(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
}
}
}
int query (int l, int r) {
assert(r >= l);
int k = lg[r-l+1];
return max(sp[k][l], sp[k][r - (1 << k) + 1]);
}
};
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
for (int i = 1; i <= max(n, m); ++i) {
lg.push_back(lg.back()+!(i&(i-1)));
}
vector<RMQ> row(n), col(m);
for (int i = 0; i < n; ++i) {
row[i].init(a[i]);
}
for (int j = 0; j < m; ++j) {
vector<int> p;
for (int i = 0; i < n; ++i) {
p.push_back(a[i][j]);
}
col[j].init(p);
}
long long ans = 0;
for (int x = 1; x < n - 1; ++x) {
for (int y = 1; y < m - 1; ++y) {
for (int i = x; i < n - 1; ++i) {
if (a[i][y] >= a[i][y - 1]) {
break;
}
for (int j = y; j < m - 1; ++j) {
bool good = 1, bad = 0;
for (int l = x; l <= i && !bad; ++l) {
int mx = row[l].query(y, j);
good &= mx < a[l][j + 1];
bad |= mx >= a[l][y - 1];
}
for (int r = y; r <= j && !bad; ++r) {
int mx = col[r].query(x, i);
good &= mx < a[i + 1][r];
bad |= mx >= a[x - 1][r];
}
if (bad) {
break;
}
ans += good;
}
}
}
}
return ans;
}
# | 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... |