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 "rect.h"
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#define ll long long
using namespace std;
vector <ll> U;
vector <array<ll, 2>> V;
vector <array<ll, 2>> tmp;
vector <array<ll, 2>> A[2500][2500], B[2500][2500];
ll bit[2501], upd[2500][2500], nx[2500][2500], n, m, f, l, r, mid, cnt, z;
void add(ll u) {
while (u <= n) {
U.push_back(u);
++bit[u];
u += u & -u;
}
}
ll query(ll u) {
ll res = 0;
while (u) {
res += bit[u];
u -= u & -u;
}
return res;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
n = a.size(), m = a[0].size(), f = 0;
for (int i=0; i<n; ++i) {
V.clear();
for (int j=0; j<m; ++j) {
while (!V.empty()) {
auto [u, id] = V.back();
if (u <= a[i][j]) V.pop_back();
else break;
}
if (j && j != m-1 && a[i][j] < a[i][j+1]) {
z = V.size()-1;
while (z >= 0) {
A[i][j].push_back({V[z][1]+1, i});
if (V[z][0] >= a[i][j+1]) break;
--z;
}
}
if (V.empty() || V.back()[0] > a[i][j]) V.push_back({a[i][j], j});
}
for (int j=0; j<m; ++j) {
reverse(A[i][j].begin(), A[i][j].end());
if (i && !A[i][j].empty()) {
int k = 0;
for (auto [u, v] : A[i-1][j]) {
while (k < A[i][j].size()-1 && A[i][j][k][0] < u) ++k;
if (A[i][j][k][0] == u) A[i][j][k][1] = v;
}
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
upd[i][j] = -2;
}
}
for (int j=0; j<m; ++j) {
V.clear();
for (int i=0; i<n; ++i) {
while (!V.empty()) {
auto [u, id] = V.back();
if (u <= a[i][j]) V.pop_back();
else break;
}
if (i && i != n-1 && a[i][j] < a[i+1][j]) {
z = V.size()-1;
while (z >= 0) {
upd[V[z][1]+1][i] = j;
B[i][j].push_back({j, V[z][1]+1});
if (V[z][0] >= a[i+1][j]) break;
--z;
}
}
if (V.empty() || V.back()[0] > a[i][j]) V.push_back({a[i][j], i});
}
for (int i=0; i<n; ++i) {
if (j && !B[i][j].empty()) {
int k = 0;
tmp.clear();
for (auto [u, v] : B[i][j-1]) {
if (upd[v][i] == j) {
upd[v][i] = -1;
tmp.push_back({u, v});
}
}
for (auto [u, v] : B[i][j]) {
if (upd[v][i] == -1) {
upd[v][i] = j;
}
else tmp.push_back({u, v});
}
swap(B[i][j], tmp);
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
ll l = 0, r = 0;
while (l < A[i][j].size() && r < B[i][j].size()) {
if (B[i][j][r][0] <= A[i][j][l][0]) {
add(n-B[i][j][r][1]);
++r;
}
else {
f += query(n-A[i][j][l][1]);
++l;
}
}
while (l < A[i][j].size()) {
f += query(n-A[i][j][l][1]);
++l;
}
while (!U.empty()) {
auto u = U.back();
U.pop_back();
bit[u] = 0;
}
}
}
return f;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:39:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [u, id] = V.back();
| ^
rect.cpp:57:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for (auto [u, v] : A[i-1][j]) {
| ^
rect.cpp:58:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while (k < A[i][j].size()-1 && A[i][j][k][0] < u) ++k;
| ~~^~~~~~~~~~~~~~~~~~
rect.cpp:73:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | auto [u, id] = V.back();
| ^
rect.cpp:92:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
92 | for (auto [u, v] : B[i][j-1]) {
| ^
rect.cpp:98:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | for (auto [u, v] : B[i][j]) {
| ^
rect.cpp:90:9: warning: unused variable 'k' [-Wunused-variable]
90 | int k = 0;
| ^
rect.cpp:111:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (l < A[i][j].size() && r < B[i][j].size()) {
| ~~^~~~~~~~~~~~~~~~
rect.cpp:111:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (l < A[i][j].size() && r < B[i][j].size()) {
| ~~^~~~~~~~~~~~~~~~
rect.cpp:121:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | while (l < A[i][j].size()) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |