Practice arena
C++17·2 s·128 MB
SortingIntermediate

Count inversions

Count pairs (i, j) where i < j and a[i] > a[j] without checking every pair.

Input

  • The first line contains T.
  • Each case contains n followed by n integers.

Output

Print the 64-bit inversion count for each case.

Technique

merge-sort augmentation

Target

O(n log n)

Constraints & sample
  • 1 ≤ n ≤ 200,000
  • The answer may exceed a signed 32-bit integer

Sample input

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

Expected output

3
10
0
Review the concept

Before you submit

  • Test the smallest valid input and one awkward edge case.
  • Keep the solution within O(n log n).
  • Match spacing and line breaks exactly before checking 8 hidden cases.
main.cpp
Draft saved locally · 423 / 30,000
Ln 1, Col 1Spaces: 4UTF-8LFC++17

Visible testcases

Editable and stored only in this browser. Hidden judge cases never change.

Checking your session…

Run checks Sample · Submit checks 8 hidden cases

Run the sample first, then send your solution through 8 hidden cases.

Code runs in an isolated Judge0 environment. Drafts and custom visible cases stay on this device; submission metadata and progress sync to MongoDB.