C# SortedSet - O(N*LogN) time
SortedSet works as a priority queue here. SortedSet.Min, SortedSet.Max, SortedSet.Add and SortedSet.Remove methods take LogN time each which leads to O(NLogN) t
SortedSet works as a priority queue here. SortedSet.Min, SortedSet.Max, SortedSet.Add and SortedSet.Remove methods take LogN time each which leads to O(NLogN) time complexity overall.\nWe need method AddOrder because SortedSet doesn\'t allow to store duplicates.\n```\npublic class Solution\n{\n    public int GetNumberOfBacklogOrders(int[][] orders)\n    {\n        var buyQueue = new SortedSet<Tuple<int, int>>();\n        var sellQueue = new SortedSet<Tuple<int, int>>();\n        for (int i = 0; i < orders.Length; i++)\n        {\n            var price = orders[i][0];\n            var amount = orders[i][1];\n            if (orders[i][2] == 0)\n            {\n                while (amount > 0 && sellQueue.Count > 0)\n                {\n                    var min = sellQueue.Min;\n                    var minPrice = min.Item1;\n                    if (minPrice > price)\n                        break;\n                    amount = DecreaseAmount(amount, sellQueue, min);\n                }\n                if (amount > 0)\n                    AddOrder(price, amount, buyQueue);\n            }\n            else\n            {\n                while (amount > 0 && buyQueue.Count > 0)\n                {\n                    var max = buyQueue.Max;\n                    var maxPrice = max.Item1;\n                    if (maxPrice < price)\n                        break;\n                    amount = DecreaseAmount(amount, buyQueue, max);\n                }\n                if (amount > 0)\n                    AddOrder(price, amount, sellQueue);\n            }\n        }\n        var result = 0;\n        foreach (var item in buyQueue)\n            result = (result + item.Item2) % (int)(1e9 + 7);\n        foreach (var item in sellQueue)\n            result = (result + item.Item2) % (int)(1e9 + 7);\n        return result;\n    }\n    \n    private int DecreaseAmount(int amount, SortedSet<Tuple<int, int>> queue, Tuple<int, int> order)\n    {\n        queue.Remove(order);\n        var orderPrice = order.Item1;\n        var orderAmount = order.Item2;\n        if (orderAmount > amount)\n        {\n            AddOrder(orderPrice, orderAmount - amount, queue);\n            return 0;\n        }\n        return amount - orderAmount;\n    }\n    \n    private void AddOrder(int price, int amount, SortedSet<Tuple<int, int>> queue)\n    {\n        var order = Tuple.Create(price, amount);\n        while (queue.Contains(order))\n        {\n            queue.Remove(order);\n            order = Tuple.Create(price, order.Item2 * 2);\n        }\n        queue.Add(order);\n    }\n}\n```
[]