이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 2505;
const int lim = 50;
vector<ii> valA[maxn][maxn] , valB[maxn][maxn];
int m , n;
int h[maxn][maxn];
int L[maxn][maxn] , R[maxn][maxn] , U[maxn][maxn] , D[maxn][maxn];
void init(int key){
vector<int> s(maxn , 0);
int sz = 0;
auto add = [&](vector<ii> & val , int j){
if(val.size() && val.back().first + val.back().second == j)val.back().second++;
else {
if(val.size() && val.back().second <= lim)val.back() = mp(j , 1);
else val.pb(mp(j,1));
}
};
for(int i = 1 ; i <= m ; ++i){
sz = 0;
for(int j = 1 ; j <= n ; ++j){
while(sz > 0 && h[i][s[sz - 1]] + key <= h[i][j])sz--;
if(sz > 0)L[i][j] = s[sz - 1];
else L[i][j] = 0;
s[sz++] = j;
}
sz = 0;
for(int j = n ; j >= 1 ; --j){
while(sz > 0 && h[i][s[sz - 1]] + key <= h[i][j])sz--;
if(sz > 0)R[i][j] = s[sz - 1];
else R[i][j] = n + 1;
s[sz++] = j;
if(key){
if(L[i][j] != 0)add(valA[L[i][j]][j] , i);
if(R[i][j] != n + 1 && L[i][R[i][j]] != j)add(valA[j][R[i][j]] , i);
}
}
}
for(int j = 1 ; j <= n ; ++j){
sz = 0;
for(int i = 1 ; i <= m ; ++i){
while(sz > 0 && h[s[sz - 1]][j] + key <= h[i][j])sz--;
if(sz > 0)U[i][j] = s[sz - 1];
else U[i][j] = 0;
s[sz++] = i;
}
sz = 0;
for(int i = m ; i >= 1 ; --i){
while(sz > 0 && h[s[sz - 1]][j] + key <= h[i][j])sz--;
if(sz > 0)D[i][j] = s[sz - 1];
else D[i][j] = m + 1;
s[sz++] = i;
if(key){
if(U[i][j] != 0)add(valB[U[i][j]][i], j);
if(D[i][j] != m + 1 && U[D[i][j]][j] != i)add(valB[i][D[i][j]] , j);
}
}
}
}
vector<ii> val[maxn][maxn];
int cnt[maxn][maxn];
long long count_rectangles(std::vector<std::vector<int> > a) {
m = a.size();n = a[0].size();
for(int i = 1 ; i <= m ; ++i)for(int j = 1 ; j <= n ; ++j)h[i][j] = a[i - 1][j - 1];
init(0);
for(int i = 2 ; i < m ; ++i){
for(int j = 2 ; j < n ; ++j){
if(L[i][j] && U[i][j] && R[i][j] <= n && D[i][j] <= m){
val[L[i][j]][R[i][j]].pb(mp(U[i][j] , D[i][j]));
}
}
}
init(1);
int res = 0;
for(int L = 1 ; L <= m ; ++L){
for(int R = L + 2 ; R <= m ; ++R){
for(auto & [U , D] : val[L][R]){
if(cnt[U][D]++ == 0){
auto checkA = [&](int L , int R , int U , int D)->bool{
if(::R[U][L] < R || ::L[U][R] > L)return 0;
if(D - U + 1 <= lim){
for(int i = U ; i <= D ; ++i){
if(::R[i][L] < R || ::L[i][R] > L){
return 0;
}
}
return 1;
}
auto it = upper_bound(valA[L][R].begin(),valA[L][R].end(),mp(U + 1 , 0))-valA[L][R].begin()-1;
if(it < 0 || valA[L][R][it].first + valA[L][R][it].second <= D)return 0;
return 1;
};
auto checkB = [&](int L , int R , int U , int D)->bool{
if(::D[L][U] < R || ::U[R][U] > L)return 0;
if(D - U + 1 <= lim){
for(int i = U ; i <= D ; ++i){
if(::D[L][i] < R || ::U[R][i] > L){
return 0;
}
}
return 1;
}
auto it = lower_bound(valB[L][R].begin(),valB[L][R].end(),mp(U+1,0))-valB[L][R].begin()-1;
if(it < 0 || valB[L][R][it].first + valB[L][R][it].second <= D)return 0;
return 1;
};
if(checkA(L , R , U + 1 , D - 1) && checkB(U , D , L + 1 , R - 1)){
res++;
}
}
}
for(auto & [U , D] : val[L][R]){
cnt[U][D] = 0;
}
}
}
return res;
}
#ifdef LOCAL
#include "rect.h"
#include <cstdio>
#include <unistd.h>
#include <cassert>
#include <string>
using namespace std;
class InputReader {
private:
static const int SIZE = 4096;
int inputFileDescriptor;
char buf[SIZE];
int curChar;
int numChars;
public:
inline InputReader(int _inputFileDescriptor):
inputFileDescriptor(_inputFileDescriptor),
curChar(0),
numChars(0) {
}
inline void close() {
::close(inputFileDescriptor);
}
inline char read() {
assert(numChars != -1);
if (curChar >= numChars) {
curChar = 0;
numChars = ::read(inputFileDescriptor, buf, SIZE);
if (numChars == -1)
return -1;
}
return buf[curChar++];
}
inline int readInt() {
int c = eatWhite();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
assert(c >= '0' && c <= '9');
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
inline string readString() {
char c = eatWhite();
string res;
do {
res += c;
c = read();
} while (!isSpaceChar(c));
return res;
}
inline string readLine() {
string res;
while (true) {
char c = read();
if (c == '\n' || c == '\r' || c == -1)
break;
res += c;
}
return res;
}
inline char eatWhite() {
char c = read();
while (isSpaceChar(c))
c = read();
return c;
}
static inline bool isSpaceChar(char c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
InputReader inputReader(STDIN_FILENO);
int n, m;
n = inputReader.readInt();
m = inputReader.readInt();
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = inputReader.readInt();
}
}
inputReader.close();
long long result = count_rectangles(a);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
#endif // LOCAL
# | 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... |