fork download
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<queue>
  5. #include<algorithm>
  6. using namespace std;
  7. typedef long long ll;
  8.  
  9. int main() {
  10. ll t;
  11. cin >> t;
  12. while(t--){
  13. ll n, m, k, heap = 1, sum = 0;
  14. cin >> n >> m >> k;
  15. vector<pair<ll, ll> > v(n);
  16. vector<pair<ll, ll> > x(m);
  17. priority_queue<ll> pq;
  18. bool valid = true;
  19. for(int i = 0; i < n; i++){
  20. cin >> v[i].first >> v[i].second;
  21. }
  22. for(int i = 0; i < m; i++){
  23. cin >> x[i].first >> x[i].second;
  24. }
  25. sort(x.begin(), x.end());
  26. int index = 0;
  27. for(int i = 0; i < n; i++){
  28. while(index < m && x[index].first < v[i].second){
  29. if(x[index].first < v[i].first)pq.push(x[index].second);
  30. index++;
  31. }
  32.  
  33. while(heap < v[i].second - v[i].first + 2){
  34. if(pq.empty()){
  35. cout << "-1\n";
  36. valid = false;
  37. break;
  38. }
  39. heap += pq.top();
  40. pq.pop();
  41. sum ++;
  42. cout << heap << " ";
  43. }
  44. if(!valid)break;
  45. }
  46. if(valid)cout << sum << endl;
  47. }
  48. }
Success #stdin #stdout 0s 5288KB
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
6 8 9 41 4
3 -1
7 1
4 6 2