Glass Bridge

Time limit1sMemory limit512 MB

Summary
Given N and values a_i, count pairs i < j with a_i > a_j.
Level

Medium7 of 10

Topics
Array, Divide and conquer, Sorting, Prefix sum
Solved
No attempts yet

Problem

Water pollution troubles many large cities, and the ACM metropolis of ICPC country is no exception. Many cities treat sewage before releasing it into a water source, but the ACM metropolis has too little spare land for treatment plants.

The government of the ACM metropolis picked a chemical method instead. It releases a treatment chemical into the rivers around the city to purify the water. The reaction needs sunlight, so every bridge across a river is being rebuilt in glass.

A glass bridge still blocks 50% of the sunlight. Where two glass bridges overlap, only 25% of the sunlight gets through. The chemical needs at least 40% of the sunlight to work, so an overlap is a problem. Overlaps cannot be avoided completely, so the government wants the area below 40% to be as small as possible.

The government plans to build NN more bridges over one river. A three level bridge is not buildable, so at most two bridges overlap at any point. The government wants to know how many points are covered by two bridges.

The districts along the river face each other. District 1 sits directly across from district −1-1, district 2 across from district −2-2, and so on. Plan ii is a bridge from district ii to district −ai-a_i. Bridge ii and bridge jj cross over the river exactly when i<ji < j and ai>aja_i > a_j. Count the pairs of bridges that cross.

Input

The first line contains the number of test cases TT. (1≤T≤201 \le T \le 20)

Each test case starts with a line holding the number of bridges to build, NN. (2≤N≤100 0002 \le N \le 100\,000)

The next line holds NN integers a1 a2 … aNa_1\ a_2\ \dots\ a_N separated by spaces. (1≤ai≤N1 \le a_i \le N) Plan ii builds a bridge from district ii to district −ai-a_i. The same value may appear more than once.

Output

For each test case, print the number of crossing bridge pairs on its own line.

Examples5

  1. Example 1

    Input
    2
    8
    4 8 1 2 3 5 7 6
    3
    3 2 1
    
    Expected output
    10
    3
    
  2. Example 2

    Input
    1
    2
    1 2
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    2
    2 1
    
    Expected output
    1
    
  4. Example 4

    Input
    1
    10
    1 2 3 4 5 6 7 8 9 10
    
    Expected output
    0
    
  5. Example 5

    Input
    1
    10
    10 9 8 7 6 5 4 3 2 1
    
    Expected output
    45