#include "rect.h"
#include <vector>
#include <set>
using namespace std;
typedef long long llong;
const int inf = 1e8;
int n, m;
int L[2501][2501];
int R[2501][2501];
int U[2501][2501];
int D[2501][2501];
int Lm[2501][2501];
int Rm[2501][2501];
int Um[2501][2501];
int Dm[2501][2501];
struct idxtree {
const static int sz = 1 << 12;
int seg[sz << 1];
void init(vector<int> v) {
for (int i = 1; i <= v.size(); ++i) seg[i + sz] = v[i - 1];
for (int i = sz; i--; ) seg[i] = min(seg[i << 1], seg[i << 1 | 1]);
}
int query(int x, int y) {
x += sz;
y += sz;
int ret = inf;
while (x <= y) {
if ((x & 1) == 1) ret = min(ret, seg[x++]);
if ((y & 1) == 0) ret = min(ret, seg[y--]);
x >>= 1;
y >>= 1;
}
return ret;
}
} Li[2501], Ri[2501], Ui[2501], Di[2501];
llong count_rectangles(vector<vector<int>> a) {
n = a.size();
m = a[0].size();
//step 1
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= m; ++j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
st.pop_back();
L[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(m + 1);
for (int j = m; j > 0; --j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
st.pop_back();
R[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= n; ++j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
st.pop_back();
U[j][i] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(n + 1);
for (int j = n; j > 0; --j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
st.pop_back();
D[j][i] = st.back();
st.push_back(j);
}
}
//step 2
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= m; ++j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
st.pop_back();
Lm[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(m + 1);
for (int j = m; j > 0; --j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
st.pop_back();
Rm[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= n; ++j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
st.pop_back();
Um[j][i] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(n + 1);
for (int j = n; j > 0; --j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
st.pop_back();
Dm[j][i] = st.back();
st.push_back(j);
}
}
//step 3
for (int i = 1; i <= m; ++i) {
vector<int> Ls, Rs;
for (int j = 1; j <= n; ++j) {
Ls.push_back(Lm[j][i]);
Rs.push_back(Rm[j][i]);
}
Li[i].init(Ls);
Ri[i].init(Rs);
}
for (int i = 1; i <= n; ++i) {
vector<int> Us, Ds;
for (int j = 1; j <= m; ++j) {
Us.push_back(Um[i][j]);
Ds.push_back(Dm[i][j]);
}
Ui[i].init(Us);
Di[i].init(Ds);
}
int ans = 0;
set<llong> sp;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
int Lx = L[i][j] + 1;
if (Lx == 1) continue;
int Rx = R[i][j] - 1;
if (Rx == m) continue;
int Ux = U[i][j] + 1;
if (Ux == 1) continue;
int Dx = D[i][j] - 1;
if (Dx == n) continue;
llong idx = ((llong)(Lx << 12 | Rx)) << 24 | (Ux << 12 | Dx);
if (sp.count(idx)) continue;
sp.insert(idx);
if (Ri[Lx - 1].query(Ux, Dx) - 1 < Rx) continue;
if (Li[Rx + 1].query(Ux, Dx) + 1 > Lx) continue;
if (Di[Ux - 1].query(Lx, Rx) - 1 < Dx) continue;
if (Ui[Dx + 1].query(Lx, Rx) + 1 > Ux) continue;
++ans;
}
}
return ans;
}
Compilation message
rect.cpp: In member function 'void idxtree::init(std::vector<int>)':
rect.cpp:23:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i <= v.size(); ++i) seg[i + sz] = v[i - 1];
~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Correct |
11 ms |
3832 KB |
Output is correct |
4 |
Correct |
7 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
7 ms |
3704 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Correct |
11 ms |
3832 KB |
Output is correct |
4 |
Correct |
7 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
7 ms |
3704 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Correct |
11 ms |
3832 KB |
Output is correct |
4 |
Correct |
7 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
7 ms |
3704 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Correct |
11 ms |
3832 KB |
Output is correct |
4 |
Correct |
7 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
7 ms |
3704 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
101240 KB |
Output is correct |
2 |
Correct |
114 ms |
86076 KB |
Output is correct |
3 |
Correct |
135 ms |
101252 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Incorrect |
137 ms |
101240 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
8 ms |
3832 KB |
Output is correct |
3 |
Correct |
11 ms |
3832 KB |
Output is correct |
4 |
Correct |
7 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3704 KB |
Output is correct |
6 |
Incorrect |
7 ms |
3704 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |