이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mp make_pair
#define pb push_back
#define eb emplace_back
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using ll = long long;
ll count_rectangles(vector<vector<int> > a) {
ll ans = 0;
int n = (int)a.size(), m = (int)a[0].size();
vector<vector<vector<ii> > > horiz(n, vector<vector<ii> >(m)), vert(n, vector<vector<ii> >(m));
for (int i = 1; i + 1 < n; i++) {
vector<int> st_max;
for (int j = 0; j < m; j++) {
while (!st_max.empty() && mp(a[i][st_max.back()], st_max.back()) < mp(a[i][j], j)) {
if (j - 1 > 0 && st_max.back() + 1 <= j - 1 && st_max.back() + 2 < m) {
horiz[i][j - 1].eb(st_max.back() + 1, 1);
}
st_max.pop_back();
}
st_max.pb(j);
}
}
for (int i = 1; i + 1 < n; i++) {
vector<int> st_max;
for (int j = m - 1; j >= 0; j--) {
while (!st_max.empty() && mp(a[i][st_max.back()], st_max.back()) < mp(a[i][j], j)) {
if (st_max.back() < m && j + 1 <= st_max.back() - 1 && j + 1 > 0) {
horiz[i][st_max.back() - 1].eb(j + 1, 1);
}
st_max.pop_back();
}
st_max.pb(j);
}
}
for (int i = 1; i + 1 < m; i++) {
vector<int> st_max;
for (int j = 0; j < n; j++) {
while (!st_max.empty() && mp(a[st_max.back()][i], st_max.back()) < mp(a[j][i], j)) {
if (j - 1 > 0 && st_max.back() + 1 <= j - 1 && st_max.back() + 2 < n) {
vert[j - 1][i].eb(st_max.back() + 1, 1);
}
st_max.pop_back();
}
st_max.pb(j);
}
}
for (int i = 1; i + 1 < m; i++) {
vector<int> st_max;
for (int j = n - 1; j >= 0; j--) {
while (!st_max.empty() && mp(a[st_max.back()][i], st_max.back()) < mp(a[j][i], j)) {
if (st_max.back() < n && j + 1 <= st_max.back() - 1 && j + 1 > 0) {
vert[st_max.back() - 1][i].eb(j + 1, 1);
}
st_max.pop_back();
}
st_max.pb(j);
}
}
for (int i = 1; i + 1 < n; i++) {
for (int j = 1; j + 1 < m; j++) {
reverse(horiz[i][j].begin(), horiz[i][j].end());
reverse(vert[i][j].begin(), vert[i][j].end());
for (auto &[k, l] : horiz[i][j]) {
auto it = lower_bound(horiz[i - 1][j].begin(), horiz[i - 1][j].end(), mp(k, 0));
if (it != horiz[i - 1][j].end() && it->first == k) {
l = it->second + 1;
}
}
for (auto &[k, l] : vert[i][j]) {
auto it = lower_bound(vert[i][j - 1].begin(), vert[i][j - 1].end(), mp(k, 0));
if (it != vert[i][j - 1].end() && it->first == k) {
l = it->second + 1;
}
}
vector<iii> events;
for (auto [k1, l1] : horiz[i][j]) {
events.eb(j - k1 + 1, 0, l1);
}
for (auto [k2, l2] : vert[i][j]) {
events.eb(l2, 1, i - k2 + 1);
}
sort(events.begin(), events.end());
tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ss;
int idx = 0;
for (auto [_, type, x] : events) {
if (type == 0) {
ss.insert(mp(x, idx++));
} else {
ans += (int)ss.size() - ss.order_of_key(mp(x, -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... |