forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
136 lines (121 loc) · 4.87 KB
/
H.cpp
File metadata and controls
136 lines (121 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 100111;
const int MV = 2000111;
pair<int,int> a[MN];
int n;
int cntX[MV], cntY[MV], used[MN], xGroupId[MV], yGroupId[MV];
map< int, int > id[MV];
int nGroup;
vector<int> groups[MN];
void init() {
memset(cntX, 0, sizeof cntX);
memset(cntY, 0, sizeof cntY);
memset(used, 0, sizeof used);
memset(xGroupId, 0, sizeof xGroupId);
memset(yGroupId, 0, sizeof yGroupId);
nGroup = 0;
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
while (scanf("%d", &n) == 1) {
init();
FOR(i,1,n) {
scanf("%d%d", &a[i].first, &a[i].second);
a[i].first += 1000000;
a[i].second += 1000000;
cntX[a[i].first]++;
cntY[a[i].second]++;
}
// assign points to groups using X-coordinates
FOR(i,1,n) {
int x = a[i].first;
if (cntX[x] <= 333) {
int g = xGroupId[x];
if (g == 0) {
xGroupId[x] = ++nGroup;
groups[nGroup].clear();
g = xGroupId[x];
}
used[i] = g;
id[a[i].first][a[i].second] = g;
groups[g].push_back(i);
}
}
// assign remaining points to groups using Y-coordinates
FOR(i,1,n) if (!used[i]) {
int y = a[i].second;
int g = yGroupId[y];
if (g == 0) {
yGroupId[y] = ++nGroup;
groups[nGroup].clear();
g = yGroupId[y];
}
used[i] = g;
id[a[i].first][a[i].second] = g;
groups[g].push_back(i);
}
ll res = 0;
ll duplicated = 0;
// for each group
FOR(g,1,nGroup) {
// PR0(groups[g], SZ(groups[g]));
// count number of squares with 2 vertices in this group
REP(id1,SZ(groups[g])) {
FOR(id2,id1+1,SZ(groups[g])-1) {
int i = groups[g][id1], j = groups[g][id2];
if (a[i].first == a[j].first) {
// same x
// left
int side = abs(a[i].second - a[j].second);
auto u = make_pair(a[i].first - side, a[i].second);
auto v = make_pair(a[j].first - side, a[j].second);
if (u.first >= 0) {
if (id[u.first].count(u.second) && id[v.first].count(v.second)) {
if (id[u.first][u.second] == id[v.first][v.second]) ++duplicated;
++res;
}
}
// right
u = make_pair(a[i].first + side, a[i].second);
v = make_pair(a[j].first + side, a[j].second);
if (u.first < MV) {
if (id[u.first].count(u.second) && id[v.first].count(v.second)) {
if (id[u.first][u.second] == id[v.first][v.second]) ++duplicated;
++res;
}
}
}
else {
// same y
// down
int side = abs(a[i].first - a[j].first);
auto u = make_pair(a[i].first, a[i].second - side);
auto v = make_pair(a[j].first, a[j].second - side);
if (id[u.first].count(u.second) && id[v.first].count(v.second)) {
if (id[u.first][u.second] == id[v.first][v.second]) ++duplicated;
++res;
}
// up
u = make_pair(a[i].first, a[i].second + side);
v = make_pair(a[j].first, a[j].second + side);
if (id[u.first].count(u.second) && id[v.first].count(v.second)) {
if (id[u.first][u.second] == id[v.first][v.second]) ++duplicated;
++res;
}
}
}
}
}
cout << res - duplicated / 2 << endl;
}
}