PKU 2002 Squares

  • 2点をfor文で回す
  • 対応する残り2点が存在するかどうかをlog(N)で調べる
int main() {
	for (int n; cin >> n && n; ) {
		set<pair<int, int> > positionSet;
		vector<pair<int, int> > positions;
		for (int i = 0; i < n; ++i) {
			int x, y;
			cin >> x >> y;
			positions.push_back(make_pair(x, y));
			positionSet.insert(make_pair(x, y));
		}

		sort(positions.begin(), positions.end());

		int answer = 0;
		for (int i = 0; i < n; ++i) {
			const pair<int, int>& p0 = positions[i];
			const int x0 = p0.first;
			const int y0 = p0.second;

			for (int j = i + 1; j < n; ++j) {
				const pair<int, int>& p1 = positions[j];
				const int x1 = p1.first;
				const int y1 = p1.second;

				const int x2 = x0 + y1 - y0;
				const int y2 = y0 - x1 + x0;
				const int x3 = x1 + y1 - y0;
				const int y3 = y1 - x1 + x0;

				if (positionSet.count(make_pair(x2, y2)) && positionSet.count(make_pair(x3, y3))) {
					++answer;
				}
			}
		}

		cout << answer / 2 << endl;
	}
}