B - Linear Algebra Test Gym - 101502B

Dr. Wail is preparing for today's test in linear algebra course. The test's subject is Matrices Multiplication.

Dr. Wail has n matrices, such that the size of the ith matrix is (ai × bi), where aiis the number of rows in the ith matrix, and bi is the number of columns in the ithmatrix.

Dr. Wail wants to count how many pairs of indices i and j exist, such that he can multiply the ith matrix with the jth matrix.

Dr. Wail can multiply the ith matrix with the jth matrix, if the number of columns in the ith matrix is equal to the number of rows in the jth matrix.

Input

The first line contains an integer T (1 ≤ T ≤ 100), where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), where n is the number of matrices Dr. Wail has.

Then n lines follow, each line contains two integers ai and bi (1 ≤ ai, bi ≤ 109) (ai ≠ bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

Output

For each test case, print a single line containing how many pairs of indices i and jexist, such that Dr. Wail can multiply the ith matrix with the jth matrix.

Example

Input

1
5
2 3
2 3
4 2
3 5
9 4

Output

5

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

In the first test case, Dr. Wail can multiply the 1st matrix (2 × 3) with the 4thmatrix (3 × 5), the 2nd matrix (2 × 3) with the 4th matrix (3 × 5), the 3rd matrix (4 × 2) with the 1st and second matrices (2 × 3), and the 5th matrix (9 × 4) with the 3rd matrix (4 × 2). So, the answer is 5.

Hint

对于(c,d),(e,f),如果d=e,那么为·一种情况,要计算对于给定的数据判断要多少组。注意如果d=e,那么c!=f

提示:如果有的话,那么d=e必然在分别在所下方设立的两个数组中,其中a【】为第一个坐标,b【】为第二个坐标,比如(1,2)和(2,3),2在a,b两个数组中,那么只需要计算出2在a,b数组的次数成绩即可,其它的一样。

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1e5+5;///数组的元素最大个数
int a[maxn],b[maxn];
int find_x(int x,int l,int r,int b[])///二分查找
{
    int mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(b[mid]==x)return mid;
        else if(b[mid]>x)r=mid-1;
        else l=mid+1;
    }
    return -1;///没有返回-1
}
int main()
{
    int t,n,i,p,j;
    long long c,s;///可能超出int范围
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
        }
        sort(a,a+n);///从小到大排序
        sort(b,b+n);
        c=0;
        for(i=0;i<n;i++)
        {
            if(i==0||a[i-1]!=a[i])///a数组中第一个元素或者a数组当前元素不等于上一个元素
            {
                s=0;
                p=find_x(a[i],0,n-1,b);
                if(p!=-1)///b数组中有a[i]时
                {
                    for(j=p;j<n;j++)
                    {
                        if(b[j]==a[i])s++;
                        else break;
                    }
                    for(j=p-1;j>=0;j--)
                    {
                        if(b[j]==a[i])s++;
                        else break;
                    }
                }
                c+=s;
            }
            else c+=s;///当i>0并且a[i]=a[i-1],即a数组当前元素等于上一个元素,减少时间消耗
        }
        printf("%I64d\n",c);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值