Satyam and Counting
Problem
Satyam is given n distinct points on the 2D coordinate plane. It is guaranteed that 0 ≤ y i ≤ 1 0≤y_i≤1 0≤yi≤1 for all given points ( x i , y i ) (x_i,y_i) (xi,yi). How many different nondegenerate right triangles∗ can be formed from choosing three different points as its vertices?
Two triangles a and b are different if there is a point v such that v is a vertex of a but not a vertex of b.
A nondegenerate right triangle has positive area and an interior 90∘ angle.
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 ) (1≤t≤10^4) (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n n n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 ) (3≤n≤2⋅10^5) (3≤n≤2⋅105) — the number of points.
The following n lines contain two integers x i x_i xi and y i y_i yi ( 0 ≤ x i ≤ n , 0 ≤ y i ≤ 1 ) (0≤x_i≤n, 0≤y_i≤1) (0≤xi≤n,0≤yi≤1) — the i’th point that Satyam can choose from. It is guaranteed that all ( x i , y i ) (x_i,y_i) (xi,yi) are pairwise distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
Output
Output an integer for each test case, the number of distinct nondegenerate right triangles that can be formed from choosing three points.
Example
Input
3
5
1 0
1 1
3 0
5 0
2 1
3
0 0
1 0
3 0
9
1 0
2 0
3 0
4 0
5 0
2 1
7 1
8 1
9 1
Output
4
0
8
Note
The four triangles in question for the first test case:
Code
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+10;
bool v[N][2];//0<=x<=n,0<=y<=1
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
for(ll i=0;i<=n;i++)
v[i][0]=v[i][1]=0;
for(ll i=1;i<=n;i++)
{
ll x,y;
cin>>x>>y;
v[x][y]=1;
}
ll ans=0;
for(ll i=0;i<=n;i++)
{
if(v[i][0]&&v[i][1]) ans+=(n-2);//x=i(2个点)
if(v[i][0]&&v[i-1][1]&&v[i+1][1]) ans++;//y=1(2个点),y=0(1个点)
if(v[i][1]&&v[i-1][0]&&v[i+1][0]) ans++;//y=0(2个点),y=1(1个点)
}
cout<<ans<<endl;
}
return 0;
}