Java TreeMap
\npublic int getNumberOfBacklogOrders(int[][] orders) {\n        TreeMap<Integer, Long> buy = new TreeMap();\n        TreeMap<Integer, Long> sell = new TreeMap(
```\npublic int getNumberOfBacklogOrders(int[][] orders) {\n        TreeMap<Integer, Long> buy = new TreeMap();\n        TreeMap<Integer, Long> sell = new TreeMap();\n        for(int []order: orders){\n            long orderCount = order[1];\n            if(order[2] == 0){\n                while(true){\n                    Map.Entry<Integer, Long> me = sell.firstEntry();\n                    if(orderCount == 0 || me == null || me.getKey() > order[0]){\n                        break;\n                    }\n                    if(me.getValue() <= orderCount){\n                        sell.remove(me.getKey());\n                    }else{\n                        sell.put(me.getKey(), me.getValue() - orderCount);\n                    }\n                    orderCount = Math.max(0, orderCount - me.getValue());\n                }\n                if(orderCount != 0)\n                    buy.put(order[0], buy.getOrDefault(order[0], 0L) + orderCount);\n            }else{\n                while(true){\n                    Map.Entry<Integer, Long> me = buy.lastEntry();\n                    if(orderCount == 0 || me == null || me.getKey() < order[0]){\n                        break;\n                    }\n                    if(me.getValue() <= orderCount){\n                        buy.remove(me.getKey());\n                    }else{\n                        buy.put(me.getKey(), me.getValue() - orderCount);\n                    }\n                    orderCount = Math.max(0, orderCount - me.getValue());\n                }\n                if(orderCount != 0)\n                    sell.put(order[0], sell.getOrDefault(order[0], 0L) + orderCount);\n            }\n        }\n        long ans = 0;\n        for(Long i: buy.values()){\n            ans += i;\n        }\n        for(Long i: sell.values()){\n            ans += i;\n        }\n        return (int)(ans % 1000000007);\n    }\n```
[]