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