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>
#define fs first
#define sc second
using namespace std;
typedef pair<int,int> pii;
struct BIT{
int n;
vector<int> bt;
BIT(int x){
n=x;
bt.resize(x);
}
void update(int x,int y){
for(;x<=n;x+=(x&-x)){
bt[x]+=y;
}
}
int query(int x){
int ans=0;
for(;x>0;x-=(x&-x)){
ans+=bt[x];
}
return ans;
}
};
long long count_rectangles(vector<vector<int> > v) {
int n=v.size();
int m=v[0].size();
if(n<=2 || m<=2){
return 0;
}
vector<vector<pii> > az(n),bz(m);
for(int i=1;i<n-1;i++){
vector<pair<int,int> > gg;
vector<int> vis(m),up(m),down(m);
for(int j=0;j<m;j++){
gg.push_back({v[i][j],j});
}
sort(gg.begin(),gg.end());
for(auto h:gg){
int x=h.sc-1,y=h.sc+1;
int l=h.sc,r=h.sc;
if(x<0 || !vis[x]){
}
else{
l=up[x];
}
if(y>=m || !vis[y]){
}
else{
r=down[y];
}
vis[h.sc]=1;
up[h.sc]=l;
down[h.sc]=r;
down[l]=r;
up[r]=l;
if(l!=0 && r!=m-1 && (v[i][r+1]>h.fs && v[i][l-1]>h.fs)){
az[i].push_back({l,r});
}
}
}
for(int i=1;i<m-1;i++){
vector<pair<int,int> > gg;
vector<int> vis(n),up(n),down(n);
for(int j=0;j<n;j++){
gg.push_back({v[j][i],j});
}
sort(gg.begin(),gg.end());
for(auto h:gg){
int x=h.sc-1,y=h.sc+1;
int l=h.sc,r=h.sc;
if(x<0 || !vis[x]){
}
else{
l=up[x];
}
if(y>=n || !vis[y]){
}
else{
r=down[y];
}
vis[h.sc]=1;
up[h.sc]=l;
down[h.sc]=r;
down[l]=r;
up[r]=l;
if(l!=0 && r!=n-1 && (v[r+1][i]>h.fs && v[l-1][i]>h.fs)){
bz[i].push_back({l,r});
}
}
}
vector<pair<pii,pii> > c,d;
map<pii,int> mp;
for(int i=1;i<n-1;i++){
map<pii,int> tmp;
for(auto h:az[i]){
tmp[h]=1;
if(mp.find(h)==mp.end()){
mp[h]=i;
}
}
if(i>1){
for(auto h:az[i-1]){
if(tmp.find(h)==tmp.end()){
c.push_back({h,{mp[h],i-1}});
mp.erase(h);
}
}
}
}
for(auto h:mp){
c.push_back({h.fs,{h.sc,n-2}});
}
mp.clear();
for(int i=1;i<m-1;i++){
map<pii,int> tmp;
for(auto h:bz[i]){
tmp[h]=1;
if(mp.find(h)==mp.end()){
mp[h]=i;
}
}
if(i>1){
for(auto h:bz[i-1]){
if(tmp.find(h)==tmp.end()){
d.push_back({h,{mp[h],i-1}});
mp.erase(h);
}
}
}
}
for(auto h:mp){
d.push_back({h.fs,{h.sc,m-2}});
}
vector<vector<pair<int,pii> > > f(m),e(m);
for(auto h:d){
f[h.sc.fs].push_back({h.sc.sc,h.fs});
}
for(auto h:c){
e[h.fs.fs].push_back({h.fs.sc,h.sc});
}
vector<pair<pii,pii> > x;
vector<BIT> vb(n,BIT(n));
long long ans=0;
for(int i=1;i<m-1;i++){
for(auto h:f[i]){
x.push_back({{-h.fs,0},h.sc});
}
auto ev=x;
for(auto h:e[i]){
ev.push_back({{-h.fs,1},h.sc});
}
sort(ev.begin(),ev.end());
for(auto h:ev){
if(h.fs.sc==1){
for(int i=h.sc.fs;i<=h.sc.sc;i++){
ans+=vb[i].query(h.sc.sc);
}
}
else{
vb[h.sc.fs].update(h.sc.sc,1);
}
}
for(auto h:ev){
if(h.fs.sc==0){
vb[h.sc.fs].update(h.sc.sc,-1);
}
}
}
return ans;
}
# | 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... |