F. Yet Another Problem About Pairs Satisfying an Inequality(二分搜索)

You are given an array a1,a2,…ana1,a2,…an. Count the number of pairs of indices 1≤i,j≤n1≤i,j≤n such that ai<i<aj<jai<i<aj<j.

Input

The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains an integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the elements of the array.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a single integer — the number of pairs of indices satisfying the condition in the statement.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example

input

Copy

5
8
1 1 2 3 8 2 1 4
2
1 2
10
0 2 1 6 3 4 1 2 8 3
2
1 1000000000
3
0 1000000000 2

output

Copy

3
0
10
0
1

Note

For the first test cases the pairs are (i,j)(i,j) = {(2,4),(2,8),(3,8)}{(2,4),(2,8),(3,8)}.

  • The pair (2,4)(2,4) is true because a2=1a2=1, a4=3a4=3 and 1<2<3<41<2<3<4.
  • The pair (2,8)(2,8) is true because a2=1a2=1, a8=4a8=4 and 1<2<4<81<2<4<8.
  • The pair (3,8)(3,8) is true because a3=2a3=2, a8=4a8=4 and 2<3<4<82<3<4<8.

思路:

1,a[i]<i,i<a[j],a[j]<j,把要求分为三个部分,a[i]>=i时,不要,然后采用二分查找

2,i<a[j],之前符合1的都存储位置,然后在里面找i<a[j]的个数,因为序号是有顺序的,所以二分搜索是可行的.

代码:

int a[maxj];
void solve(){
    int n;cin>>n;
    vector<int>ve;
    int ans=0;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        if(a[i]<i){
            ans+=lower_bound(ve.begin(),ve.end(),a[i])-ve.begin();
            ve.emplace_back(i);
        }
    }cout<<ans<<'\n';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值