-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinversion-count.cs
45 lines (40 loc) · 1.06 KB
/
inversion-count.cs
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
using System;
using System.Linq;
using System.Collections.Generic;
class Node {
public long m_val, m_sze;
public Node m_lft, m_rht;
public Node(long p_val) {
m_val = p_val;
m_sze = 1;
}
public long addNode(Node p_n) {
++m_sze;
if (p_n.m_val < m_val) {
if (null == m_lft) {
m_lft = p_n;
return m_sze - 1;
}
return m_lft.addNode(p_n) + 1 + (m_rht?.m_sze ?? 0);
}
if (null == m_rht) {
m_rht = p_n;
return 0;
}
return m_rht.addNode(p_n);
}
}
class Solution {
static void Main(string[] args) {
long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
long res = 0;
Node root = null;
for (int i = 0; i < nums[3]; i++) {
nums[2] = nums[1] * nums[2] % nums[0];
Node node = new Node(nums[2]);
if (root == null) root = node;
else res += root.addNode(node);
}
Console.WriteLine(res);
}
}