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;
const int M = (1 << 13);
int t[2 * M];
void update(int pos, int val) {
for(t[pos += M] += val; pos > 1; pos >>= 1)
t[pos >> 1] = t[pos] + t[(pos ^ 1)];
}
int get(int ql, int qr) {
int sum = 0;
for(ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
if(ql & 1) sum += t[ql++];
if(qr & 1) sum += t[--qr];
}
return sum;
}
void compress(vector<int> &a) {
map<int, int> mp;
int n = (int)a.size();
for(int i = 0; i < n; i++)
mp[a[i]] = 1;
int k = 0;
for(auto &c : mp) {
c.second = k++;
}
for(int i = 0; i < n; i++)
a[i] = mp[a[i]];
}
// O(nlogn)
vector<pair<int, int>> get_segments(vector<int> a) {
compress(a);
int n = (int)a.size();
vector<int> b[n];
set<pair<int, int>> res;
for(int i = 0; i < n; i++) {
b[a[i]].push_back(i);
}
set<int> st;
for(int x = n - 1; x >= 0; x--) {
for(int pos : b[x])
st.insert(pos);
for(int pos : b[x]) {
auto it = st.lower_bound(pos);
if(it != st.begin()) {
auto lb = it; lb--;
if(*it - *lb > 1)
res.insert({*lb + 1, *it - 1});
}
if(it != (--st.end())) {
auto rb = it; rb++;
if(*rb - *it > 1)
res.insert({*it + 1, *rb - 1});
}
}
}
vector<pair<int, int>> res1;
for(auto [l, r] : res)
res1.push_back({l, r});
return res1;
}
template<typename T>
void intersect(set<T> &st, set<T> st1) {
set<T> st2;
for(auto x : st1) {
if(st.count(x))
st2.insert(x);
}
st = st2;
}
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++) {
map<int, int> mp;
for(int j = 0; j < m; j++) {
map<int, int> mp1;
for(auto &c : h[i][j]) {
if(mp.count(c.l)) mp1[c.l] = mp[c.l] + 1;
else mp1[c.l] = 1;
}
mp = mp1;
for(auto &c : h[i][j])
c.d = mp[c.l];
}
}
for(int i = 0; i < m; i++) {
map<int, int> mp;
for(int j = 0; j < n; j++) {
map<int, int> mp1;
for(auto &c : g[j][i]) {
if(mp.count(c.l)) mp1[c.l] = mp[c.l] + 1;
else mp1[c.l] = 1;
}
mp = mp1;
for(auto &c : g[j][i])
c.d = mp[c.l];
}
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < m; j++) {
// cout << "point: " << i << ' ' << j << '\n';
// cout << "g: ";
// for(auto s : g[i][j])
// cout << '{' << s.l << ' ' << s.d << '}';
// cout << '\n';
// cout << "h: ";
// for(auto s : h[i][j])
// cout << '{' << s.l << ' ' << s.d << '}';
// cout << '\n';
// }
// }
ll ans = 0;
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) {
update(h[i][j][h_l++].l, +1);
}
ans += get(i - cur.d + 1, i);
}
// cout << ans << ' ';
while(h_l > 0)
update(h[i][j][--h_l].l, -1);
}
// cout << '\n';
}
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... |