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>
#include "rect.h"
using namespace std;
using ll = long long;
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> const &a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
// O(nlogn)
vector<pair<int, int>> get_segments(vector<int> a) {
int n = (int)a.size();
vector<pair<int, int>> res;
vector<int> st;
for(int i = 0; i < n; i++) {
while(!st.empty() && a[st.back()] < a[i]) {
if(i - st.back() > 1) res.push_back({st.back() + 1, i - 1});
st.pop_back();
}
if(!st.empty()) {
if(i - st.back() > 1) res.push_back({st.back() + 1, i - 1});
if(a[st.back()] == a[i]) st.pop_back();
}
st.push_back(i);
}
return res;
}
const int N = 2505;
struct segment {
int l, d;
};
vector<segment> h[N][N], g[N][N];
ll count_rectangles(vector<vector<int>> a) {
int n = (int)a.size(), m = (int)a[0].size();
if(n < 3 || m < 3) return 0;
for(int i = 0; i < n; i++) {
vector<pair<int, int>> s = get_segments(a[i]);
for(auto [l, r] : s)
g[i][r].push_back({l, 0});
}
for(int i = 0; i < m; i++) {
vector<int> b;
for(int j = 0; j < n; j++)
b.push_back(a[j][i]);
vector<pair<int, int>> s = get_segments(b);
for(auto [l, r] : s)
h[r][i].push_back({l, 0});
}
for(int i = 0; i < n; i++) {
int cnt[n] = {};
bool us[n] = {};
for(int j = 0; j < m; j++) {
if(!j) {
for(auto c : h[i][j])
cnt[c.l]++;
} else {
for(auto c : h[i][j])
us[c.l] = 1;
for(auto c : h[i][j - 1])
if(!us[c.l]) cnt[c.l] = 0;
for(auto c : h[i][j]) {
cnt[c.l]++;
us[c.l] = 0;
}
}
for(auto &c : h[i][j]) {
c.d = cnt[c.l];
}
}
}
for(int i = 0; i < m; i++) {
int cnt[m] = {};
bool us[m] = {};
for(int j = 0; j < n; j++) {
if(!j) {
for(auto c : g[j][i])
cnt[c.l]++;
} else {
for(auto c : g[j][i])
us[c.l] = 1;
for(auto c : g[j - 1][i])
if(!us[c.l])
cnt[c.l] = 0;
for(auto c : g[j][i]) {
cnt[c.l]++;
us[c.l] = 0;
}
}
for(auto &c : g[j][i])
c.d = cnt[c.l];
}
}
ll ans = 0;
FenwickTree st(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
sort(g[i][j].begin(), g[i][j].end(), [](segment x, segment y) {return x.l < y.l;});
sort(h[i][j].begin(), h[i][j].end(), [](segment x, segment y) {return x.d > y.d;});
int h_l = 0;
for(segment cur : g[i][j]) {
while(h_l < (int)h[i][j].size() && j - h[i][j][h_l].d < cur.l) {
st.add(h[i][j][h_l++].l, +1);
}
ans += st.sum(i - cur.d + 1, i);
}
while(h_l > 0)
st.add(h[i][j][--h_l].l, -1);
}
}
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... |