2 priority_queue to record the end of queries,  O(nlogn)
Intuition\n Describe your first thoughts on how to solve this problem. \nThe thing is that you traver the nums by index, when you meet a nums that is nonzero, y
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe thing is that you traver the nums by index, when you meet a nums that is nonzero, you want to choose the segment that cover this point. Whats more, the most important, you want this segment to be as far as possible. (not as long as possible, because nums before this index has already became 0).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nYou sort the queries by their index, so that when you visit nums[i]. You know the previous part of quries cover nums[i]. Then you put all those queries cover this nums[i] into first PQ as candidate. After this, the start point of queries become no use. You only care about the end. So you only need to put the end into PQ. When you find the nums[i] is nonzero, you choose from the candidats, first PQ. And put it into second PQ, which are those queries you choose.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        sort(queries.begin(), queries.end());\n        priority_queue<int> candidate;\n        priority_queue<int, vector<int>, greater<>> chosen;\n        int ans = 0;\n        int n = nums.size();\n        int j = 0;\n        for (int i = 0; i < n; i++){\n            while (j < queries.size() && queries[j][0] == i){\n                candidate.push(queries[j][1]);\n                j++;\n            }\n            nums[i] -= chosen.size();\n            while (nums[i] > 0 && !candidate.empty() && candidate.top() >= i){\n                ans++;\n                chosen.push(candidate.top());\n                candidate.pop();\n                nums[i]--;\n            } \n            if (nums[i] > 0)\n                return -1;\n            while (!chosen.empty() && chosen.top() == i)\n                chosen.pop();\n            \n        }\n        return queries.size() - ans;\n        \n    }\n};\n```
['C++']