Official
D - Rectangles Editorial by en_translator
We use the property that a rectangle is uniquely determined if we know the lower-left vertex and the upper-right vertex.
We first brute force over the pairs of lower-left and upper-right vertices, and then check if corresponding upper-left and lower-right vertices are included in the \(N\) points. One can check if a point is contained in the \(N\) points in an \(O(\log N)\) time with a binary search if we sort the points beforehand. If we compress the coordinates beforehand, we can find it in an \(O(1)\) time.
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
vector<pair<int,int>> v(n);
for(int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort(v.begin(), v.end());
int ans = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(v[i].first < v[j].first && v[i].second < v[j].second) {
if(binary_search(v.begin(), v.end(), make_pair(v[i].first, v[j].second)) && binary_search(v.begin(), v.end(), make_pair(v[j].first, v[i].second))) ans++;
}
cout << ans << endl;
}
posted:
last update: