D. Training Session
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.
Monocarp has nn problems that none of his students have seen yet. The ii-th problem has a topic aiai (an integer from 11 to nn) and a difficulty bibi (an integer from 11 to nn). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.
Monocarp decided to select exactly 33 problems from nn problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both):
- the topics of all three selected problems are different;
- the difficulties of all three selected problems are different.
Your task is to determine the number of ways to select three problems for the problemset.
Input
The first line contains a single integer tt (1≤t≤500001≤t≤50000) — the number of testcases.
The first line of each testcase contains an integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of problems that Monocarp have.
In the ii-th of the following nn lines, there are two integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n) — the topic and the difficulty of the ii-th problem.
It is guaranteed that there are no two problems that have the same topic and difficulty at the same time.
The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.
Output
Print the number of ways to select three training problems that meet either of the requirements described in the statement.
Example
input
Copy
2 4 2 4 3 4 2 1 1 3 5 1 5 2 4 3 3 4 2 5 1
output
Copy
3 10
Note
In the first example, you can take the following sets of three problems:
- problems 11, 22, 44;
- problems 11, 33, 44;
- problems 22, 33, 44.
Thus, the number of ways is equal to three.
-------------------------------------------------------------------------------------------
正难则反 ,题目要去三个二元组中满足 ai 或者 bi 至少一个两两不同,反面是?
或者变成并且,一个两两不同变成两两相同或者三个全部相同。
ai bi 如果一方三个相同,结果如下
1 2
1 2
1 3
但是题目要求不存在相同二元组
故只能两两相同,交叉着
ai bi
1 3
1 2
4 2
可以看出,有一个类似于中间点的,(1,2),它决定了这个三个二元组的性质,对于每个唯一的二元组(a,b) 找出另外ai=a的个数,就是这个中间点上部的情况,bi=b,就是下部情况,二者相乘
就是答案
另外要用总方案数减去不满足的方案数,注意long long
# include<iostream>
# include<math.h>
# include<stack>
# include<vector>
# include<algorithm>
# include<cstring>
using namespace std;
typedef long long int ll;
ll a[200000+10];
ll b[200000+10];
ll cnt1[200000+10];
ll cnt2[200000+10];
int main ()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cnt1[i]=cnt2[i]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i],&b[i]);
cnt1[a[i]]++;
cnt2[b[i]]++;
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ans+=(cnt1[a[i]]-1)*(cnt2[b[i]]-1);
}
cout<<(ll)n*(ll)(n-1)*(ll)(n-2)/6-ans<<'\n';
}
return 0;
}