Use just enough queries for each index
\nimport heapq\n\nclass FenwickTree:\n    def __init__(self, x):\n        self.bit = x\n        for i in range(len(x)):\n            j = i | (i + 1)\n          
```\nimport heapq\n\nclass FenwickTree:\n    def __init__(self, x):\n        self.bit = x\n        for i in range(len(x)):\n            j = i | (i + 1)\n            if j < len(x):\n                x[j] += x[i]\n\n    def update(self, idx, x):\n        while idx < len(self.bit):\n            self.bit[idx] += x\n            idx |= idx + 1\n\n    def query(self, end):\n        x = 0\n        while end:\n            x += self.bit[end - 1]\n            end &= end - 1\n        return x\n\nclass Solution:\n    def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int:\n        n = len(nums)\n        queries.sort()\n        dec = FenwickTree([0] * n)\n        i = 0\n        heap = []\n        res = len(queries)\n        for j in range(n):\n            while i < len(queries) and queries[i][0] <= j:\n                heapq.heappush(heap, (-queries[i][1], queries[i][0]))\n                i += 1\n            while nums[j] - dec.query(j + 1) > 0 and heap:\n                res -= 1\n                x = heapq.heappop(heap)\n                l, r = x[1], -x[0]\n                dec.update(l, 1)\n                dec.update(r + 1, -1)\n            if nums[j] - dec.query(j + 1) > 0:\n                return -1\n \xA0 \xA0 \xA0 \xA0return res \n```
[]