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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ordered_set tree<long long, null_type, greater<long long>, rb_tree_tag,tree_order_statistics_node_update>
#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)
const int N = 2505;
set<pair<short, short>> hp[N][N], vs[N][N];
long long count_rectangles(vector<vector<int>> a) {
int m = a.front().size();
int n = a.size();
auto pos = [&] (vector<int> cur) {
int s = cur.size();
vector<pair<int, int>> res;
vector<int> st;
range(i, 0, s) {
while(st.size() && cur[st.back()] <= cur[i]) {
if(st.back() < i - 1) res.emplace_back(st.back() + 1, i - 1);
int b = st.back();
st.pop_back();
if(cur[b] == cur[i]) goto A;
}
if(st.size() && st.back() < i - 1) res.emplace_back(st.back() + 1, i - 1);
A:;
st.push_back(i);
}
return res;
};
range(i, 0, n) {
vector<pair<int, int>> foo = pos(a[i]);
for(auto u : foo) {
int l = u.first, r = u.second;
pair<short, short> p = make_pair(l, i);
if(i && hp[i - 1][r].lower_bound(make_pair(l, -1))->first == l) p.second = hp[i - 1][r].lower_bound(make_pair(l, -1))->second;
hp[i][r].insert(p);
}
}
range(i, 0, m) {
vector<int> ss(n);
range(j, 0, n) ss[j] = a[j][i];
vector<pair<int, int>> foo = pos(ss);
for(auto u : foo) {
int l = u.first, r = u.second;
pair<short, short> p = make_pair(l, i);
if(i && vs[r][i - 1].lower_bound(make_pair(l, -1))->first == l) p.second = vs[r][i - 1].lower_bound(make_pair(l, -1))->second;
vs[r][i].insert(p);
}
}
a.clear();
long long res = 0;
auto cnt = [&](set<pair<short, short>> &x, set<pair<short, short>> &y) {
vector<pair<int, int>> sy;
for(auto i : y) {
swap(i.first, i.second);
sy.push_back(i);
}
sort(sy.begin(), sy.end());
reverse(sy.begin(), sy.end());
ordered_set cur;
auto it = x.begin();
int p = sy.size() - 1;
while(it != x.end()) {
while(~p && sy[p].first <= it->first) {
cur.insert(sy[p].second);
p--;
}
res += cur.order_of_key(it->second - 1);
it++;
}
};
range(i, 0, n) {
range(j, 0, m) {
cnt(hp[i][j], vs[i][j]);
}
}
return res;
}
# | 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... |