using
System;
using
System.Collections.Generic;
public
class
GFG {
public
class
Item {
public
int
val;
public
int
index;
public
Item(
int
val,
int
index)
{
this
.val = val;
this
.index = index;
}
}
public
static
List<
int
> CountLarge(
int
[] a)
{
int
len = a.Length;
Item[] items =
new
Item[len];
for
(
int
i = 0; i < len; i++) {
items[i] =
new
Item(a[i], i);
}
int
[] count =
new
int
[len];
MergeSort(items, 0, len - 1, count);
List<
int
> res =
new
List<
int
>();
foreach
(
int
i
in
count) { res.Add(i); }
return
res;
}
public
static
void
MergeSort(Item[] items,
int
low,
int
high,
int
[] count)
{
if
(low >= high) {
return
;
}
int
mid = low + (high - low) / 2;
MergeSort(items, low, mid, count);
MergeSort(items, mid + 1, high, count);
Merge(items, low, mid, mid + 1, high, count);
}
public
static
void
Merge(Item[] items,
int
low,
int
lowEnd,
int
high,
int
highEnd,
int
[] count)
{
int
m = highEnd - low + 1;
Item[] sorted =
new
Item[m];
int
rightCounter = 0;
int
lowInd = low, highInd = high;
int
index = 0;
while
(lowInd <= lowEnd && highInd <= highEnd) {
if
(items[lowInd].val < items[highInd].val) {
rightCounter++;
sorted[index++] = items[highInd++];
}
else
{
count[items[lowInd].index] += rightCounter;
sorted[index++] = items[lowInd++];
}
}
while
(lowInd <= lowEnd) {
count[items[lowInd].index] += rightCounter;
sorted[index++] = items[lowInd++];
}
while
(highInd <= highEnd) {
sorted[index++] = items[highInd++];
}
Array.Copy(sorted, 0, items, low, m);
}
public
static
void
PrintArray(List<
int
> countList)
{
foreach
(
int
i
in
countList)
{
Console.Write(i +
" "
);
}
Console.WriteLine();
}
public
static
void
Main(
string
[] args)
{
int
[] arr = { 3, 7, 1, 5, 9, 2 };
int
n = arr.Length;
List<
int
> countList = CountLarge(arr);
PrintArray(countList);
}
}