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 <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
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;
}
Compilation message (stderr)
rect.cpp:2: warning: ignoring #pragma comment [-Wunknown-pragmas]
2 | #pragma comment(linker, "/stack:200000000")
|
rect.cpp:6: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |