HTML & JS
#include <iostream>
using namespace std;
typedef long long int ll;
ll c[1000005],arr[1000005],res=0,n;
void merge(ll s1,ll e1,ll s2,ll e2)
{
	ll st1=s1,st2=s2,k=0;
	while(st1<=e1 && st2<=e2)
	{
		if(arr[st1]<=arr[st2])
		c[k++]=arr[st1++];
		else
		{
		    
			c[k++]=arr[st2++];
			res+=(e1-st1+1);
		}
	}
	while(st1<=e1)
	c[k++]=arr[st1++];
	while(st2<=e2)
	c[k++]=arr[st2++];
	k=0;
	for(ll i=s1;i<=e2;i++)
	{
	   arr[i]=c[k++];
	   
	    
	}
}

void mergeSort(ll st,ll e)
{
	if(st>e)
	return;
	if(st==e)
	return;
	ll mid=(st+e)/2;
	mergeSort(st,mid);
	mergeSort(mid+1,e);
	merge(st,mid,mid+1,e);
	

	
}
int main() {
	
	// your code here
	ll t;
	cin>>t;
	while(t--)
	{
		res=0;
		cin>>n;
		for(ll i=0;i<n;i++)
		{
		    cin>>arr[i];
		}
		mergeSort(0,n-1);
		cout<<res<<endl;
	}

	return 0;
}	

Generated URL:

Output:

No Output
Report Bug