이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rect.h"
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x,y,z) cerr<< #x <<" = "<< x<<" "<< #y <<" = "<< y<<" "<< #z <<" = "<< z<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N=2505;
int ata[N],mn[N],mx[N],arr[N],c[N],sz,sub[N],vis[N],rc,pos;
vector<int>row[N][N],col[N][N];
bool cmp(int x,int y){
return (arr[x]<arr[y]);
}
int tap(int x){
if(ata[x]==x)
return x;
return ata[x]=tap(ata[x]);
}
void merge(int x,int y){
if((x=tap(x))==(y=tap(y)))
return;
if(sub[x]<sub[y])
swap(x,y);
ata[y]=x;
sub[x]+=sub[y];
umin(mn[x],mn[y]);
umax(mx[x],mx[y]);
}
map<pair<int,PII>,int>pm[2];
//The key idea is there are at most 2*n^2 such pairs
void add(int x,int y){
assert(x+1<y);
//if rc=0, then rectangle on row (pos) in range (arr[pos][x],arr[pos][y]) is good
//if rc=1, then rectangle on column (pos) in range (arr[x][pos],arr[y][pos]) is good
if(pm[rc][mp(pos,mp(x,y))])
return;
pm[rc][mp(pos,mp(x,y))]=1;
if(!rc)
row[x][y].pb(pos);
else
col[x][y].pb(pos);
}
void solve(int x){
if(x>1 and vis[x-1]){
int a=mn[tap(x-1)]-1;
if(a>=1)
add(a,x);
}
if(x<sz and vis[x+1]){
int b=mx[tap(x+1)]+1;
if(b<=sz)
add(x,b);
}
}
void upd(int x){
vis[x]=1;
if(x>1 and vis[x-1])
merge(x-1,x);
if(x<sz and vis[x+1])
merge(x,x+1);
}
void f(){
for(int i=1;i<=sz;i++)
ata[i]=mn[i]=mx[i]=i,sub[i]=1,c[i]=i,vis[i]=0;
sort(c+1,c+sz+1,cmp);
for(int i=1;i<=sz;i++){
int j=i;
while(j<=sz and arr[c[j]]==arr[c[i]])
solve(c[j++]);
for(int k=i;k<j;k++)
upd(c[k]);
i=j-1;
}
}
long long count_rectangles(std::vector<std::vector<int> > a) {
int n=int(a.size()),m=int(a[0].size());
if(n<3 or m<3)
return 0;
{
//precalc
sz=m;
for(int j=1;j<=n;j++){
for(int i=1;i<=m;i++)
arr[i]=a[j-1][i-1];
rc=0;pos=j;f();
}sz=n;
for(int j=1;j<=m;j++){
for(int i=1;i<=n;i++)
arr[i]=a[i-1][j-1];
rc=1;pos=j;f();
}
}
vector<pair<PII,PII> >R,C;
vector<int>cur;int tmp;
for(int i=1;i<=m;i++)
for(int j=i+2;j<=m;j++){
tmp=int(row[i][j].size());
for(int k=0;k<tmp;k++){
cur.pb(row[i][j][k]);
if(k+1==tmp or row[i][j][k]+1!=row[i][j][k+1]){
R.pb(mp(mp(cur[0]-1,cur.back()+1),mp(i,j)));//a,b,x,y
cur.clear();
}
}
}
/*
a,b,x,y (n^2) => R
c,d,i,j (n^2) => C
a<=i<=j<=b
c<=x<=y<=d
*/
for(int i=1;i<=n;i++)
for(int j=i+2;j<=n;j++){
tmp=int(col[i][j].size());
for(int k=0;k<tmp;k++){
cur.pb(col[i][j][k]);
if(k+1==tmp or col[i][j][k]+1!=col[i][j][k+1]){
C.pb(mp(mp(cur[0]-1,cur.back()+1),mp(i,j)));//c,d,i,j
cur.clear();
}
}
}
ll ans=0;
tr(r,R)
tr(c,C)
ans+=(r->ff.ff<=c->ss.ff and c->ss.ss<=r->ff.ss
and c->ff.ff<=r->ss.ff and r->ss.ss<=c->ff.ss);
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... |