#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define N 2501
map<int, int> cnt[2][N][N], tmpmp;
int arr[N][N], bit[N][N], temp[N], p[N], lc[N], rc[N], n, m;
void buildtree(int sz){
vector<int> v = {0};
for(int i = 1; i <= sz; i++){
vector<int> t;
while(temp[v.back()] < temp[i]){
t.push_back(v.back());
v.pop_back();
}
v.push_back(i);
for(int j = t.size() - 1; j > 0; j--){
p[t[j - 1]] = t[j];
}
if(!t.empty())p[t.back()] = i;
}
for(int i = v.size() - 1; i > 0; i--)
p[v[i]] = v[i - 1];
for(int i = 1; i <= sz; i++){
if(p[i] > i){
lc[p[i]] = i;
} else {
rc[p[i]] = i;
}
}
}
void travtree(int i1, int i2, int v, int l, int r){
if(l != 0 && r != (i1 == 1 ? n + 1 : m + 1) && r - l > 1 && temp[l] != temp[v] && temp[r] != temp[v]){
if(i1 == 0)
cnt[i1][i2][r - 1][r - l - 1]++;
else
cnt[i1][r - 1][i2][r - l - 1]++;
}
if(lc[v] != 0)
travtree(i1, i2, lc[v], l, v);
if(rc[v] != 0)
travtree(i1, i2, rc[v], v, r);
}
void build(){
for(int j = 1; j <= m; j++){
memset(lc, 0, sizeof lc);
memset(rc, 0, sizeof rc);
for(int i = 0; i <= n; i++)
temp[i] = arr[i][j];
buildtree(n);
travtree(1, j, 0, 0, n + 1);
}
}
void update(int x, int y, int v){
for(int i = x; i < N; i += i & -i){
for(int j = y; j < N; j += j & -j){
bit[i][j] += v;
}
}
}
int query(int x, int y){
int res = 0;
for(int i = x; i > 0; i -= i & -i){
for(int j = y; j > 0; j -= j & -j){
res += bit[i][j];
}
}
return res;
}
long long count_rectangles(vector<vector<int> > a){
n = a.size();
m = a[0].size();
memset(arr, 0x3f, sizeof arr);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
arr[i + 1][j + 1] = a[i][j];
a.clear();
build();
int res = 0;
for(int i = 1; i <= n; i++){
memset(lc, 0, sizeof lc);
memset(rc, 0, sizeof rc);
for(int j = 0; j <= m; j++)
temp[j] = arr[i][j];
buildtree(m);
travtree(0, i, 0, 0, m + 1);
for(int j = 1; j <= m; j++){
tmpmp = cnt[0][i][j];
for(auto& x : tmpmp){
auto it = cnt[0][i - 1][j].find(x.ff);
if(it != cnt[0][i - 1][j].end())
x.ss = 1 + it -> ss;
update(x.ff, x.ss, 1);
}
tmpmp = cnt[1][i][j];
for(auto& x : tmpmp){
auto it = cnt[1][i][j - 1].find(x.ff);
if(it != cnt[1][i][j - 1].end())
x.ss = 1 + it -> ss;
res += query(x.ss, N - 1);
res -= query(x.ss, x.ff - 1);
}
tmpmp = cnt[0][i][j];
for(auto& x : tmpmp)
update(x.ff, x.ss, -1);
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
553 ms |
612776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
553 ms |
612776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
553 ms |
612776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
553 ms |
612776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
582 ms |
637540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
551 ms |
612636 KB |
Output is correct |
2 |
Incorrect |
1833 ms |
691072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
553 ms |
612776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |