6

4

1 votes
Problem

6.

#include <stdio.h>
#include <stdlib.h>
int *mem, tmp[1000001];
void merge ( int a, int start, int mid, int end)
{
int l = start, r = mid + 1, c = 0, count = 0,i;
for (i = start ; i <= end ; i++)
{
if (l > mid)
{
tmp[c] = a[r++];
}
else if (r > end)
{
tmp[c++] = a[l];
mem[a[l]] += count;
l++;
}
else if (a[l] <= a[r])
{
tmp[c++] = a[l];
mem[l] += count;
l++;
}
else
{
tmp[c++] = a[r++];
count;
}
}
for(i = 0 ; i < c ; i++)
{
a[start++] = tmp[i];
}
}
void shravan ( int *a, int start, int end)
{
int mid = (start + end) / 2;
if (start < end)
{
shravan(a, start, mid);
merge(a, start, mid, end);
shravan(a, mid+1, end);
}
return;
}
int main()
{
int t, n, i;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int a[n], c[n];
mem = ( int *)calloc(sizeof(int), 1000001);
for (i = 0 ; i < n ; i++) {
scanf("%d", &a[i]);
c[i] = a[i];
}
shravan(a, 0, n-1);
for (i = 0 ; i < n-1 ; i++)
printf("%d ", mem[c[i]]);
printf("%d\n", mem[c[n-1]]);
}
return 0;
}

Input Format

1.Number of test case

2.an integer n denoting size of array

3.n integers

Output Format

n integers

Sample Input

2

10

15 2 3 654 54 5 64 23 12 8

5

12 36 2 15 4

Sample output

5 0 0 6 4 0 3 2 1 0

Test case-2

2 3 0 1 0

Constraints :

0< testcase <=10

0 < size of array <= 10^5

-10^6 <= Elements of array <= 10^6

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?