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>
#pragma GCC optimize("Ofast,unroll-loops,inline")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fs first
#define sc second
using namespace std;
typedef pair<int,int> pii;
namespace{
struct BIT{
int n;
vector<int> bt;
BIT(int x){
n=x;
bt.resize(x+1);
}
BIT(){
n=0;
}
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;
}
}vb[2502];
}
int tag[2501][2501]={0},tag2[2501][2501]={0};
long long count_rectangles(vector<vector<int> > v) {
int n=v.size();
int m=v[0].size();
vector<vector<int> > tmp(m,vector<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
tmp[j][i]=v[i][j];
}
}
if(n<=2 || m<=2){
return 0;
}
swap(n,m);
v=tmp;
vector<pii> az[2502],bz[2502];
vector<pii> t1,t2;
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});
}
}
}
for(int i=0;i<2501;i++){
for(int j=0;j<2501;j++){
tag[i][j]=-1;
}
}
vector<pair<int,pii> > ev[2505];
for(int i=1;i<n-1;i++){
for(auto h:az[i]){
tag2[h.fs][h.sc]=1;
if(tag[h.fs][h.sc]==-1){
tag[h.fs][h.sc]=i;
}
}
if(i>1){
for(auto h:az[i-1]){
if(tag2[h.fs][h.sc]==0){
ev[h.fs].push_back({-h.sc,{tag[h.fs][h.sc],i-1}});
tag[h.fs][h.sc]=-1;
}
}
}
for(auto h:az[i]){
tag2[h.fs][h.sc]=0;
}
}
for(int i=0;i<2501;i++){
for(int j=0;j<2501;j++){
if(tag[i][j]!=-1){
ev[i].push_back({-j,{tag[i][j],n-2}});
tag[i][j]=-1;
}
}
}
for(int i=1;i<m-1;i++){
for(auto h:bz[i]){
tag2[h.fs][h.sc]=1;
if(tag[h.fs][h.sc]==-1){
tag[h.fs][h.sc]=i;
}
}
if(i>1){
for(auto h:bz[i-1]){
if(tag2[h.fs][h.sc]==0){
for(int j=tag[h.fs][h.sc];j<=i-1;j++){
ev[j].push_back({-(i-1),{-h.fs,-h.sc}});
}
tag[h.fs][h.sc]=-1;
}
}
}
for(auto h:bz[i]){
tag2[h.fs][h.sc]=0;
}
}
for(int i=0;i<2501;i++){
for(int j=0;j<2501;j++){
if(tag[i][j]!=-1){
for(int q=tag[i][j];q<m-1;q++){
ev[q].push_back({-(m-2),{-i,-j}});
}
tag[i][j]=-1;
}
}
}
// BIT vb[2502];
for(int i=0;i<n;i++){
vb[i]=BIT(n);
}
//vector<BIT> vb(n,BIT(n));
long long ans=0;
// for(int i=1;i<m-1;i++){
// sort(ev[i].begin(),ev[i].end());
// }
for(int i=1;i<m-1;i++){
sort(ev[i].begin(),ev[i].end());
for(auto h:ev[i]){
if(h.sc.sc>0){
for(int j=h.sc.fs;j<=h.sc.sc;j++){
ans+=vb[j].query(h.sc.sc);
}
}
else{
vb[-h.sc.fs].update(-h.sc.sc,1);
}
}
for(auto h:ev[i]){
if(h.sc.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... |