이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 2502;
template<bool returnMax, bool returnIndex>
struct SparseTable {
int n, k;
vector<int> a;
vector<vector<int>> st;
void init(vector<int> a) {
this->a = a;
n = (int) a.size();
k = lg(n);
st.assign(n + 1, vector<int>(k + 1));
build();
}
int lg(int x) { return 31 - __builtin_clz(x); }
int merge(int x, int y) {
if (returnMax) return a[x] >= a[y] ? x : y;
return a[x] < a[y] ? x : y;
}
void build() {
for (int i = 0; i < n; i++) st[i][0] = i;
for (int j = 1; j <= k; j++)
for (int i = 0; i + (1 << j) - 1 < n; i++)
st[i][j] = merge(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
l--; r--;
int j = lg(r - l + 1);
int idx = merge(st[l][j], st[r - (1 << j) + 1][j]);
return (returnIndex ? idx : a[idx]);
}
};
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
vector<SparseTable<1, 0>> rows(n);
for(int i = 0; i < n; i++) {
rows[i].init(a[i]);
}
vector<SparseTable<1, 0>> columns(m);
for(int j = 0; j < m; j++) {
vector<int> t;
for(int i = 0; i < n; i++)
t.push_back(a[i][j]);
columns[j].init(t);
}
long long ans = 0;
for(int r1 = 1; r1 < n - 1; r1++) {
for(int c1 = 1; c1 < m - 1; c1++) {
for(int r2 = r1; r2 < n - 1; r2++) {
for(int c2 = c1; c2 < m - 1; c2++) {
//if(r1 == r2 && c1 == c2) continue;
bool flag = 1;
for(int i = r1; i <= r2; i++)
flag &= (rows[i].query(c1 + 1, c2 + 1) < min(a[i][c1 - 1], a[i][c2 + 1]));
for(int i = c1; i <= c2; i++)
flag &= (columns[i].query(r1 + 1, r2 + 1) < min(a[r1 - 1][i], a[r2 + 1][i]));
ans += flag;
}
}
}
}
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... |