fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. priority_queue<int>p;
  4. void solve() {
  5. while(!p.empty()) p.pop();
  6. int n, l, m;
  7. cin >> n >> m >> l;
  8.  
  9. vector<pair<int, int>> h(n);
  10. for (int i = 0; i < n; i++) {
  11. int x,y;
  12. cin >> x >> y;
  13. h[i] = {x, y - x + 2};
  14. }
  15. int x[m],u[m];
  16. for (int i = 0; i < m; i++) {
  17. cin >> x[i] >> u[i];
  18. }
  19.  
  20. int curr = 1, ans = 0;
  21. for (int i = 0; i < n; i++) {int j=0;
  22. while (j<m && x[j]<h[i].first) {p.push(u[j]);j++;}
  23. while(curr<h[i].second){
  24. if(p.empty()){cout<<-1<<endl;return;}
  25. curr+=p.top();ans++;p.pop();}}
  26.  
  27. cout << ans << endl;
  28. }
  29.  
  30. int main() {
  31. int t;
  32. cin >> t;
  33. while (t--) {
  34. solve();
  35. }
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5284KB
stdin
4
2 5 50
7 14
30 40
2 2
3 1
3 5
18 2
22 32
4 3 50
4 6
15 18
20 26
34 38
1 2
8 2
10 2
1 4 17
10 14
1 6
1 2
1 2
16 9
1 2 10
5 9
2 3
2 2
stdout
4
-1
1
2