이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
namespace Sub7{
vector<vector<pair<short, short>>> generate_pair(vector<vector<int>> a){
int n = a.size(), m = a[0].size();
vector<vector<pair<short, short>>> what(n);
for(int i = 1; i+1<n; ++i){
vector<int> st; st.reserve(m);
for(int j = m-1; j>=0; --j){
int best = -1;
while(st.size() && a[i][st.back()] <= a[i][j]){
int r = st.back();
maximize(best, a[i][r]);
if (r != j + 1) what[i].push_back({j, r});
st.pop_back();
}
if (st.size() && best < a[i][j] && st.back() != j + 1){
what[i].push_back({j, st.back()});
}
st.push_back(j);
}
// sort(ALL(what[i]));
}
return what;
}
ll solve(vector<vector<int>> a){
int n, m;
n = a.size(), m = a[0].size();
ll ans = 0;
vector<vector<int>> b(m, vector<int> (n));
for(int i = 0; i<n; ++i) for(int j = 0; j < m; ++j) b[j][i] = a[i][j];
vector<vector<pair<short, short>>> pair_row = generate_pair(a), pair_col = generate_pair(b);
vector<short> candidate_row[n][m], candidate_col[n][m];
vector<short> idx_col[n][n], idx_row[m][m];
for(int i = 1; i+1 < n; ++i){
for(pair<short, short> j: pair_row[i]){
candidate_row[i][j.first + 1].push_back(j.second - 1);
idx_row[j.first][j.second].push_back(i);
}
}
for(int j = 1; j+1 < m; ++j){
for(pair<short, short> i: pair_col[j]){
candidate_col[i.first + 1][j].push_back(i.second - 1);
idx_col[i.first][i.second].push_back(j);
}
}
for(int i = 1; i+1<n; ++i)
for(int j = 1; j+1<m; ++j) if (candidate_row[i][j].size() && candidate_col[i][j].size()){
for(short y: candidate_row[i][j]) for(short x: candidate_col[i][j]){
// check the rectangle (i, j, x, y)
// printArr(idx_row[j-1][y+1]);
int cnt1 = upper_bound(ALL(idx_row[j-1][y+1]), x) - lower_bound(ALL(idx_row[j-1][y+1]), i);
if (cnt1 != x - i + 1) continue;
int cnt2 = upper_bound(ALL(idx_col[i-1][x+1]), y) - lower_bound(ALL(idx_col[i-1][x+1]), j);
if (cnt2 != y - j + 1) continue;
ans++;
}
}
return ans;
}
}
ll count_rectangles(vector<vector<int>> a) {
return Sub7::solve(a);
}
# | 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... |