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 <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define N 2501
map<int, int> cnt[2][N][N];
int arr[N][N], bit[N], temp[N], p[N], lc[N], rc[N], n, m;
vector<int> v;
vector<int> t;
inline void buildtree(int sz){
v = {0};
for(int i = 1; i <= sz; i++){
t.clear();
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);
}
inline 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);
}
}
inline void update(int x, int v){
for(int i = x; i < N; i += i & -i)
bit[i] += v;
}
inline int query(int x){
int res = 0;
for(int i = x; i > 0; i -= i & -i)
res += bit[i];
return res;
}
vector<pair<int, int> > sad;
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++){
sad.clear();
for(auto& x : cnt[0][i][j]){
auto it = cnt[0][i - 1][j].find(x.ff);
if(it != cnt[0][i - 1][j].end())
x.ss = 1 + it -> ss;
sad.push_back({x.ss, x.ff});
update(x.ff, 1);
}
sort(sad.rbegin(), sad.rend());
int i1 = sad.size() - 1;
for(auto& x : cnt[1][i][j]){
auto it = cnt[1][i][j - 1].find(x.ff);
if(it != cnt[1][i][j - 1].end())
x.ss = 1 + it -> ss;
while(i1 >= 0 && sad[i1].ff < x.ff)
update(sad[i1--].ss, -1);
res += query(x.ss);
}
while(i1 >= 0)
update(sad[i1--].ss, -1);
cnt[0][i - 1][j].clear();
cnt[1][i - 1][j].clear();
}
}
return res;
}
# | 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... |