Paimon Sorting

시간 제한1초메모리 제한1024 MB

요약
주어진 이중 반복 정렬 알고리즘이 각 접두사에 대해 수행하는 교환 횟수를 구한다.
난이도

어려움10점 중 8점

유형
배열, 정렬, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

Paimon just invents a new sorting algorithm which looks much like bubble sort, with a few differences. It accepts a 11-indexed sequence AA of length nn and sorts it. Its pseudo-code is shown below.

Functions 1 The Sorting Algorithm

  1. function Sort(AA)
  2.     for ii ← 11 to nn do // nn is the number of elements in AA
  3.         for jj ← 11 to nn do
  4.             if a_i<a_ja\_i < a\_j then // a_ia\_i is the ii-th element in AA
  5.                 Swap a_ia\_i and a_ja\_j

If you don't believe this piece of algorithm can sort a sequence it will also be your task to prove it. Anyway here comes the question:

Given an integer sequence A=a_1,a_2,⋯ ,a_nA = a\_1, a\_2, \cdots, a\_n of length nn, for each of its prefix A_kA\_k of length kk (that is, for each 1≤k≤n1 \le k \le n, consider the subsequence A_k=a_1,a_2,⋯ ,a_kA\_k = a\_1, a\_2, \cdots, a\_k), count the number of swaps performed if we call SORT(A_k)\text{SORT}(A\_k).

입력

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤1051 \le n \le 10^5) indicating the length of the sequence.

The second line contains nn integers a_1,a_2,⋯ ,a_na\_1, a\_2, \cdots, a\_n (1≤a_i≤n1 \le a\_i \le n) indicating the given sequence.

It's guaranteed that the sum of nn of all test cases will not exceed 10610^6.

출력

For each test case output one line containing nn integers s_1,s_2,⋯ ,s_ns\_1, s\_2, \cdots, s\_n separated by a space, where s_is\_i is the number of swaps performed if we call SORT(A_i)\text{SORT}(A\_i).

예제1

  1. 예제 1

    입력
    3
    5
    2 3 2 1 5
    3
    1 2 3
    1
    1
    
    예상 출력
    0 2 3 5 7
    0 2 4
    0