-
Notifications
You must be signed in to change notification settings - Fork 2
/
INVCNT - Inversion Count.cpp
82 lines (65 loc) · 1.66 KB
/
INVCNT - Inversion Count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<bits/stdc++.h>
using namespace std;
struct node
{
long long key,index;
struct node *left, *right;
};
struct node *newNode(long long item, long long i)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
temp->index=i;
return temp;
}
struct node* insert(struct node* node, long long key, long long i)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key, i);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key,i);
else if (key > node->key)
node->right = insert(node->right, key,i);
/* return the (unchanged) node pointer */
return node;
}
long long search(struct node* root, long long key,long long i, long long sum)
{
// Base Cases: root is null or key is present at root
if (root == NULL)
return sum;
// Key is greater than root's key
if (root->key <=key )
{
if(root->index > i)
sum++;
return search(root->right,key,i,sum);
}
// Key is smaller than root's key
}
int main()
{
long long t;
cin>>t;
while(t--)
{
long long i,j,n,m=0;
cin>>n;
long long a[n];
for(i=0;i<n;i++)
cin>>a[i];
struct node *root = NULL;
root = insert(root, a[0],0);
for(i=1;i<n;i++)
{
insert(root,a[i],i);
}
for(i=0;i<n-1;i++)
{
m+=search(root,a[i],i,0);
}
cout<<m<<"\n";
}
}