This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 28.06.2023 11:43:25 *
*************************************/
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
// index from 0
template <class T = int>
struct Fenwick {
T *bit = nullptr;
int n;
Fenwick(int _n = 0) {
resize(_n);
}
void reset(void) {
fill(bit, bit + 1 + n, T(0));
}
void resize(int _n) {
if (bit != nullptr) delete[] bit;
n = _n;
bit = new T[n + 1];
reset();
}
void update(int i, T val) {
assert(0 <= i);
++i;
for (; i <= n; i += i & -i) bit[i] += val;
}
T get(int i) {
if (i < 0) return T(0);
++i;
i = min(i, n);
T res(0);
for (; i > 0; i -= i & -i)
res += bit[i];
return res;
}
int upper_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
int lower_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
};
long long count_rectangles(vector <vector <int>> a) {
int m = a.size(), n = a[0].size();
vector <stack <int>> st_row(m - 1), st_col(n - 1);
REP(i, m - 1) st_row[i].push(0);
REP(i, n - 1) st_col[i].push(0);
vector <vector <int>> cnt_row(n - 1, vector<int>(n - 1)), last_row(n - 1, vector<int>(n - 1)), cnt_col(m - 1, vector<int>(m - 1)), last_col(m - 1, vector<int>(m - 1));
Fenwick <int> bit(m - 1);
long long res = 0;
REP(i, m - 1) REP(j, n - 1) {
bool ok = true;
vector <pair <int, int>> row, col;
while (!st_row[i].empty()) {
int pj = st_row[i].top();
if (pj < j && ok) {
if (last_row[pj + 1][j] < i - 1) cnt_row[pj + 1][j] = 0;
++cnt_row[pj + 1][j];
last_row[pj + 1][j] = i;
row.emplace_back(j - pj, cnt_row[pj + 1][j]);
}
if (a[i][pj] > a[i][j + 1]) break;
st_row[i].pop();
ok &= a[i][pj] < a[i][j + 1];
}
st_row[i].push(j + 1);
ok = true;
while (!st_col[j].empty()) {
int pi = st_col[j].top();
if (pi < i && ok) {
if (last_col[pi + 1][i] < j - 1) cnt_col[pi + 1][i] = 0;
++cnt_col[pi + 1][i];
last_col[pi + 1][i] = j;
col.emplace_back(cnt_col[pi + 1][i], i - pi);
}
if (a[pi][j] > a[i + 1][j]) break;
st_col[j].pop();
ok &= a[pi][j] < a[i + 1][j];
}
st_col[j].push(i + 1);
reverse(ALL(row));
sort(col.rbegin(), col.rend());
int i = 0;
for (auto [a, b]: row) {
while (i < col.size() && col[i].fi >= a) bit.update(col[i++].se, 1);
res += bit.get(b);
}
while (i--) bit.update(col[i].se, -1);
}
return res;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:151:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | while (i < col.size() && col[i].fi >= a) bit.update(col[i++].se, 1);
| ~~^~~~~~~~~~~~
# | 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... |