puchi-and-luggage
Source: hackerearth
Difficulty: EASY

Puchi hates to carry luggage, but unfortunately he got a job to carry the luggage of his N friends in office. Each day,  one of his N friends, gives him the luggage of a particular weight to carry. You will be given the weight of luggage of each friend in the array Weight, where Weighti is the weight of luggage of i^th friend carried by Puchi on i^th day. It is given that all the luggages carried by Puchi are distinct in their weights. 
As Prateek assigned this job to Puchi, so for each day, he wants to know the number of days in future when Puchi will have to carry the luggage , having  weight less than the weight of luggage of current day.
Please help Prateek for the same.

Input:
The first line contains a single integer T, denoting the number of test cases. In each test case, the following input will be present: 
First line contains an integer N, where N represents the number of friends.
Next N line contains N integers, where i^th line contains i^th integer, which represents Weighti.   

Output:
Output exactly T lines. Each line contains N integer separated by a space, where i^th integer represents the number of luggage of future, which are less than the weight of luggage of the current day.

Constraints:

Subtask 1:
1 ≤ T ≤ 30
1 ≤ N ≤ 10^4
1 ≤ Weighti ≤ 10^6 
Subtask 2:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
1 ≤ Weighti ≤ 10^6

SAMPLE INPUT
1
4
2
1
4
3

SAMPLE OUTPUT
1 0 1 0

Explanation

In sample input, given T =1 in first line and N = 4 in next line. In next 4 lines, Weighti i.e weight of luggage of each friend on i^th day is given.

For weight on first day i.e 2 ,future weights smaller than 2 are   {1} . Therefore output for this weight will be 1.
For weight on second day i.e 1, no future weights are smaller than 1. So output for this weight will be 0.
For weight on third day i.e 4, future weights smaller than  4 are {3}. So output for this weight will be 1.
For weight on fourth day i.e 3, no future weights are smaller than 3. So output for this weight will be 0.
So the final output is {1, 0, 1, 0}.


def merge_sort_count(xss):
	if len(xss) == 1:
		return xss
	mid = len(xss) / 2
	xs = merge_sort_count(xss[:mid])
	ys = merge_sort_count(xss[mid:])
	res = []
	i, j, count = 0, 0, 0
	while True:
		if i >= len(xs):
			res.extend(ys[j:])
			break
		if j >= len(ys) or xs[i] < ys[j]:
			res.append((xs[i][0], xs[i][1] + count))
			i += 1
		else:
			count += 1
			res.append(ys[j])
			j += 1
	return res

def solve(xs):
	lookup = dict(merge_sort_count(list(zip(xs, [0] * len(xs)))))
	res = []
	for x in xs:
		res.append(str(lookup[x]))
	return ' '.join(res)
	
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		xs = [int(input()) for _ in range(n)]
		print(solve(xs))


def f(l,i,j):
	#print ">>>>",l
	global n
	if(i>=j):
		l[i][2]=0
		return
	mid=(i+j)/2
	f(l,i,mid)
	f(l,mid+1,j)
	temp=[]
	prev=0
	a=i
	b=mid+1
	while a<=mid and b<=j:
		if l[a][0]<l[b][0]:
			temp.append([l[a][0],l[a][1],l[a][2]+prev])
			a+=1
		else:
			prev+=1
			temp.append([l[b][0],l[b][1],l[b][2]])
			b+=1
	if a==mid+1:
		while b<=j:
			temp.append([l[b][0],l[b][1],l[b][2]])
			b+=1
	else:
		while a<=mid:
			temp.append([l[a][0],l[a][1],l[a][2]+prev])
			a+=1
	#print "temp =",temp
	index=0
	for x in range(i,j+1):
		for y in range(0,3):
			l[x][y]=temp[index][y]
		index+=1
	#print "l after temp =",l
t=eval(input())
for kk in range(0,t):
	n=eval(input())
	l=[]
	for i in range(0,n):
		l.append([eval(input()),i,0])
	#print "l b4 =",l
	f(l,0,n-1)
	#print l
	ans=[0]*n
	for i in range(0,n):
		ans[l[i][1]]=l[i][2]
	for i in range(0,n):
		print(ans[i], end=' ')
	print()


def merge(start,mid,end,arr,count):
	p = start
	q = mid+1
	a = [0]*(end-start+1)
	k=0
	c=0
	for i in range(start,end+1):
		if p >mid:
			a[k]=arr[q]
			k+=1
			q+=1
		elif q>end:
			a[k]=arr[p]
			count[arr[p]]+=c
			k+=1
			p+=1
		elif arr[p]<=arr[q]:
			a[k]=arr[p]
			count[arr[p]]+=c
			k+=1
			p+=1
		else:
			a[k]=arr[q]
			c+=1
			k+=1
			q+=1
	for i in range(k):
		arr[start]=a[i]
		start+=1
def merge_sort(start,end,arr,count):
	if(start < end):
		mid = (start+end)/2
		merge_sort(start,mid,arr,count)
		merge_sort(mid+1,end,arr,count)
		merge(start,mid,end,arr,count)		

t = int(input())
while t>0:
	count=[0]*1000001
	n = int(input())
	wt=[0]*n
	for i in range(n):
		wt[i]=int(input())
	swt = list(wt)	
	merge_sort(0,n-1,swt,count)
	for i in range(n):
		print(count[wt[i]], end=' ')

	print('\n', end=' ')		

	t-=1




my_dict={}
def merge(array,start,mid,end):
	tmp_array=[]
	count=0
	p = start
	q = mid + 1
	for steps in range(end-start+1):
		if p>mid:
			tmp_array.append(array[q])
			q+=1
		elif q>end:
			tmp_array.append(array[p])
			my_dict[repr(array[p])]+=count
			p+=1
		elif array[p]>array[q]:
			tmp_array.append(array[q])
			q+=1
			count+=1
		else:
			tmp_array.append(array[p])
			my_dict[repr(array[p])]+=count
			p+=1
	for i in range(end-start+1):
		array[start+i]=tmp_array[i]
	
	
def mergesort(array,start,end):
	if (start<end):
		mid = (start+end)/2
		mergesort(array,start,mid)
		mergesort(array,mid+1,end)
		merge(array,start,mid,end)

test_cases = eval(input())
for cases in range(test_cases):
	n=eval(input())
	array=[]
	for inputs in range(n):
		array.append(eval(input()))
	for each in array:
		my_dict[repr(each)]=0
	dup = array[::]
	mergesort(array,0,n-1)
	for each in dup:
		#print each
		print(my_dict[repr(each)], end=' ')
	print()
	my_dict.clear()


def mergeSort(a, low, high):
	if low >= high:
		return
	mid = (low + high) / 2
	mergeSort(a, low, mid)
	mergeSort(a, mid+1,high)
	merge(a, low, mid, high)
	
	
def merge(a, low, mid, right):
	count = 0
	p = low
	q = mid + 1
	aux = []
	
	for i in range(right - low + 1): #for the first part
		if p > mid: #first part exhausted
			aux.append(a[q])
			q += 1
			continue
		if q > right: #second half exhausted 
			aux.append(a[p])	
			p += 1
			if p <= mid:
				inv[weights[a[p]]] += count
			continue
		if a[p] > a[q]:
			count += 1
			aux.append(a[q])
			inv[weights[a[p]]] += 1
			q += 1
		else:
			aux.append(a[p])
			p += 1
			if p <= mid: #carry forward the count to the next element in queue
				inv[weights[a[p]]] += count
			
	for i in range(right - low + 1):
		a[low + i] = aux[i]
	

t = int(input())

inv = []
weights = {}
for i in range(t):
	n = int(input())
	a = [int(input()) for j in range(n)]
	inv = [0 for j in range(n)]
	for j in range(len(a)):
		weights[a[j]] = j
	mergeSort(a, 0, n-1)
	
	for j in inv:
		print(j, end=' ')
	print()


# def mergearray(a,l1,b,l2):
# 	ptr1,ptr2=0,0
# 	c=[]
# 	while(ptr1<l1 and ptr2<l2):
# 		if a[ptr1]<b[ptr2]:
# 			c.append(a[ptr1])
# 			ptr1+=1
# 		elif a[ptr1]>b[ptr2]:
# 			# for j in xrange(ptr1,l1):
# 			# 	dic[a[j]]+=1
# 			acc=0
# 			temp=ptr2
# 			while(temp<l2 and b[temp]<a[ptr1]):
# 				acc+=1
# 				temp+=1
# 			c.extend(b[ptr2:temp])
# 			ptr2=temp
# 			for j in xrange(ptr1,l1):
# 				dic[a[j]]+=acc	
# 			# c.append(b[ptr2])
# 			# ptr2+=1
# 	if ptr1==l1:
# 		c.extend(b[ptr2:l2])
# 	elif ptr2==l2:
# 		c.extend(a[ptr1:l1])
# 	return c					

def mergearray(a,m,n):
	mid=(m+n)>>1
	p,q=m,mid+1
	c=[]
	while(p<=mid and q<=n):
		if a[p]>a[q]:
			c.append(a[q])
			q+=1
		elif a[p]<a[q]:
			dic[a[p]]+=q-mid-1
			c.append(a[p])
			p+=1
	while(p<=mid):
		dic[a[p]]+=q-mid-1
		c.append(a[p])
		p+=1
	while(q<=n):
		c.append(a[q])
		q+=1
	for i in range(n-m+1):
		a[m]=c[i]
		m+=1				
# def mergesort(a,m,n):
# 	mid=(m+n)/2
# 	if m==n:
# 		return [a[m]]
# 	if n==m+1:
# 		if a[n]<a[m]:
# 			dic[a[m]]+=1
# 			a[m],a[n]=a[n],a[m]
# 		return a[m:n+1]
# 	b=mergesort(a,m,mid)
# 	c=mergesort(a,mid+1,n)
# 	d=mergearray(b,mid-m+1,c,n-mid)
# 	return d
def mergesort(a,m,n):
	if(m<n):
		mid=(m+n)>>1
		mergesort(a,m,mid)
		mergesort(a,mid+1,n)
		mergearray(a,m,n)

t=eval(input())
while(t>0):
	n=eval(input())
	a,b=[],[]
	dic={}
	for i in range(n):
		temp=eval(input())
		a.append(temp)
		b.append(temp)
		dic[a[i]]=0
	mergesort(a,0,n-1)
	for i in b:
		print(dic[i], end=' ')
	print()	
	t-=1


'''
Created on Nov 15, 2015

@author: Ismael
'''


def merge(a,b):
	c = (len(a)+len(b))*[0]
	i = 0
	j = 0
	while i < len(a) and j < len(b):
		if a[i]>=b[j]:
			c[i+j] = a[i]
			dict_nb_inv[a[i]] += len(b)-j
			i += 1
		else:
			c[i+j] = b[j]
			j += 1
	while i < len(a):
		c[i+j] = a[i]
		i += 1
	while j < len(b):
		c[i+j] = b[j]
		j += 1
	return c
		
def merge_sort(l):
	if len(l) <= 1:
		return l
	else:
		a = merge_sort(l[:len(l)//2])
		b = merge_sort(l[len(l)//2:])
		c = merge(a,b)
		return c

# dict_nb_inv = Counter()
# l=[4,3,2,1]
# c,nb_inv=merge_sort(l)
# print(l,nb_inv)
# r = [dict_nb_inv[v] for v in l]
# print(*r)
# #assert sum(r)==nb_inv

def main():
	global dict_nb_inv
	t=int(input())
	for _ in range(t):
		n=int(input())
		l=[int(input()) for _ in range(n)]
		dict_nb_inv = (10**6+1)*[0]
		merge_sort(l)
		print(*[dict_nb_inv[v] for v in l])
 
main()


def merge(A,l,r):
	mid=(l+r)>>1;
	Aux=[[0]*3 for i in range(r-l+1)];
	p=l;
	q=mid+1;
	i=0;
	j=0;
	while (p<=mid and q<=r):
		if (A[p][2]<A[q][2]):
			A[p][1]+=(q-mid-1);
			Aux[i]=A[p];
			p+=1;
		else :
			Aux[i]=A[q];
			q+=1;
		i+=1;
	while (p<=mid):
		A[p][1]+=(r-mid);
		Aux[i]=A[p];
		i+=1;
		p+=1;
	while (q<=r) :
		Aux[i]=A[q];
		i+=1;
		q+=1;
	for j in range(i):
		A[l]=Aux[j];
		l+=1;

def split(A,l,r):
	if l==r :
		return 0;
	mid=(l+r)>>1;
 	split(A,l,mid);
 	split(A,mid+1,r);
 	merge(A,l,r);

t=eval(input());
while t > 0:
	t=t-1;
 	n=eval(input());
 	A=[[0]*3 for i in range (n)]
 	B=[0]*(100000);
 	for i in range(n):
 		A[i][0]=i;#day
 		A[i][1]=0;#ans
 		A[i][2]=eval(input());
 	split(A,0,n-1);
 	for i in range(n):
 		B[A[i][0]]=A[i][1];
 	for i in range(n):
 		print(B[i], end=' ')
 	print("");


from pprint import pprint as pp
def GI(): return int(input())
def GIS(): return list(map(int, input().split()))

aux = []
arr = None
wgts_invs = {}

def main():
	global arr, aux
	for t in range(GI()):
		arr = []

		for i in range(GI()):
			arr.append(GI())

		orig_arr = list(arr)
		aux = [0] * len(arr)
		wgts_invs.clear()
		sort(0, len(arr))
		
		print(' '.join(map(str, [wgts_invs.get(x, 0) for x in orig_arr])))


def sort(s, e):
	if (e - s) < 2:
		return
	mid = s + ((e - s) / 2)
	sort(s, mid)
	sort(mid, e)
	merge(s, e)

def merge(s, e):
	#print arr, s, e, wgts_invs
	mid = s + ((e - s) / 2)
	i = s
	j = mid
	
	for k in range(s, e):
		#print k, i, j
		if i == mid or j < e and arr[j] < arr[i]:
			aux[k] = arr[j]
			j += 1
		else:
			wgts_invs[arr[i]] = wgts_invs.get(arr[i], 0) + (j - mid)
			aux[k] = arr[i]
			i += 1

		#print wgts_invs

	arr[s:e] = aux[s:e]


main()


def merge(a, l, r):
	mid = (l + r)/2
	p = l
	q = mid + 1

	aux = [0]*(r-l+1)
	i = 0
	while p <= mid and q <= r:
		if a[p] < a[q]:
			d[a[p]] += (q-mid-1)
			aux[i] = a[p]
			p += 1
		else:
			aux[i] = a[q]
			q += 1
		i += 1

	while p <= mid:
		d[a[p]] += r-mid
		aux[i] = a[p]
		p += 1
		i += 1

	while q <= r:
		aux[i] = a[q]
		q += 1
		i += 1

	for j in range(i):
		a[l+j] = aux[j]

def split(a, l, r):
	if l < r:
		mid = (l + r)/2 
		split(a, l, mid)
		split(a, mid + 1, r)
		merge(a, l, r)


test = int(input())
for _ in range(test):
	n = int(input())
	a = []
	d = {}

	for _ in range(n):
		a.append(int(input())) 
		d[a[-1]] = 0

	split(a[:],0,n-1)

	# print a
	for i in a:
		print(d[i], end=' ')
	print() 



def merge(a, l, r):
	mid = (l + r)/2
	p = l
	q = mid + 1

	aux = [0]*(r-l+1)
	i = 0
	while p <= mid and q <= r:
		if a[p] < a[q]:
			d[a[p]] += (q-mid-1)
			aux[i] = a[p]
			p += 1
		else:
			aux[i] = a[q]
			q += 1
		i += 1

	while p <= mid:
		d[a[p]] += r-mid
		aux[i] = a[p]
		p += 1
		i += 1

	while q <= r:
		aux[i] = a[q]
		q += 1
		i += 1

	for j in range(i):
		a[l+j] = aux[j]

def split(a, l, r):
	if l < r:
		mid = (l + r)/2 
		split(a, l, mid)
		split(a, mid + 1, r)
		merge(a, l, r)


test = int(input())
for _ in range(test):
	n = int(input())
	a = []
	d = {}

	for _ in range(n):
		a.append(int(input())) 
		d[a[-1]] = 0

	split(a[:],0,n-1)

	# print a
	for i in a:
		print(d[i], end=' ')
	print() 



a=[]
tmp_a=[]
freq=dict()
def merge(l,mid,r):
	global a
	#print a,l,mid,r
	b=[]
	x=l
	y=mid+1
	cnt=0
	while x<=mid and y<=r:
		if a[x]<=a[y]:
			b.append(a[x])
			#print a,a[x],a[y],x,y,'***',cnt
			freq[a[x]]+=cnt
			x+=1
		else:
			b.append(a[y])
			y+=1
			cnt+=1
	while x<=mid:
		b.append(a[x])
		freq[a[x]]+=cnt
		x+=1
	while y<=r:
		b.append(a[y])
		y+=1
	a[l:r+1]=b
def mergesort(l,r):
	global a
	if l<r:
		mid=(l+r)//2
		mergesort(l,mid)
		mergesort(mid+1,r)
		merge(l,mid,r)
for t in range(int(input())):
	a=[]
	freq=dict()
	tmp_a=[]
	n=int(input())
	for i in range(n):
		tmp=int(input())
		a.append(tmp)
		tmp_a.append(tmp)
		freq[tmp]=0
	mergesort(0,n-1)
	for i in tmp_a:
		print(freq[i], end=' ')
	print()


def mergesort(a,dic):
	if len(a)<=1:
		return a
	else:
		n=len(a)/2
		x=mergesort(a[:n],dic)
		y=mergesort(a[n:],dic)
		i=0;j=0
		b=[]
		x.append(999999999999)
		y.append(999999999999)
		count=0
		while i<len(x)-1 or j<len(y)-1:
			if x[i]<y[j]:
				if(x[i]!=999999999999):
					dic[x[i]]+=count
				b.append(x[i])
				i+=1
			else:
				if(x[i]!=999999999999):
					count+=1
				b.append(y[j])
				j+=1
		return b
def mergeSort(a,reverse=False):
	if reverse==False:
		return(mergesort(a))
	else:
		return(mergesort(a)[::-1])
n=int(input())
for x in range(n):
	m=int(input())
	dic={}
	a=[]
	for y in range(m):
		data=int(input())
		dic[data]=0
		a.append(data)
	mergesort(a,dic)
	for s in a:
		print(dic[s], end=' ')
	print()