http://www.lydsy.com/JudgeOnline/problem.php?id=3543
枚举每一个点,作为左下角
然后枚举 相同的x坐标,y坐标 少的那个 作为另一个角
二分判断另外两个角是否存在
#include<map> #include<vector> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 int x[N],y[N]; int hx[N],hy[N],tx,ty; map<int,int>fx,fy; vector<int>sx[N],sy[N]; void read(int &x) { x=0; int f=1; char c=getchar(); while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } int main() { int n; read(n); int mi=0; for(int i=1;i<=n;++i) { read(x[i]); read(y[i]); hx[++tx]=x[i]; hy[++ty]=y[i]; mi=min(mi,x[i]); mi=min(mi,y[i]); } sort(hx+1,hx+tx+1); sort(hy+1,hy+ty+1); tx=unique(hx+1,hx+tx+1)-hx-1; ty=unique(hy+1,hy+ty+1)-hy-1; for(int i=1;i<=n;++i) { x[i]=lower_bound(hx+1,hx+tx+1,x[i])-hx; y[i]=lower_bound(hy+1,hy+ty+1,y[i])-hy; } mi=max(0,-mi); for(int i=1;i<=tx;++i) fx[hx[i]+mi]=i; for(int i=1;i<=ty;++i) fy[hy[i]+mi]=i; for(int i=1;i<=n;++i) { sx[x[i]].push_back(y[i]); sy[y[i]].push_back(x[i]); } for(int i=1;i<=tx;++i) sort(sx[i].begin(),sx[i].end()); for(int i=1;i<=ty;++i) sort(sy[i].begin(),sy[i].end()); int j,k; int sizx,sizy; int len; int xx,yy; int ans=0; for(int i=1;i<=n;++i) { j=upper_bound(sx[x[i]].begin(),sx[x[i]].end(),y[i])-sx[x[i]].begin(); k=upper_bound(sy[y[i]].begin(),sy[y[i]].end(),x[i])-sy[y[i]].begin(); sizx=sx[x[i]].size(); sizy=sy[y[i]].size(); if(sizx-j<sizy-k) { for(;j<sizx;++j) { len=hy[sx[x[i]][j]]-hy[y[i]]; xx=fx[hx[x[i]]+len+mi]; if(!xx) continue; if(binary_search(sx[xx].begin(),sx[xx].end(),y[i]) && binary_search(sx[xx].begin(),sx[xx].end(),sx[x[i]][j])) ans++; } } else { for(;k<sizy;++k) { len=hx[sy[y[i]][k]]-hx[x[i]]; yy=fy[hy[y[i]]+len+mi]; if(!yy) continue; if(binary_search(sy[yy].begin(),sy[yy].end(),x[i]) && binary_search(sy[yy].begin(),sy[yy].end(),sy[y[i]][k])) ans++; } } } cout<<ans; }