fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. struct Query{
  7. ll A, B;
  8. int id;
  9. bool operator<(const Query& other) const { return A > other.A; }
  10. };
  11.  
  12. int main(){
  13. ios::sync_with_stdio(false);
  14. cin.tie(nullptr);
  15.  
  16. int N, Q;
  17. if(!(cin >> N >> Q)) return 0;
  18.  
  19. vector<pair<ll,ll>> dolls(N);
  20. for(int i=0;i<N;i++){
  21. ll R, H; cin >> R >> H;
  22. dolls[i] = {R, H};
  23. }
  24. vector<Query> qs(Q);
  25. for(int j=0;j<Q;j++){
  26. ll A, B; cin >> A >> B;
  27. qs[j] = {A, B, j};
  28. }
  29.  
  30. sort(dolls.begin(), dolls.end(), [](const auto& x, const auto& y){
  31. if(x.first != y.first) return x.first < y.first;
  32. return x.second > y.second;
  33. });
  34. sort(qs.begin(), qs.end());
  35.  
  36. vector<ll> tails;
  37. vector<int> ans(Q);
  38. int p = N - 1;
  39.  
  40. for(const auto& qu : qs){
  41. while(p >= 0 && dolls[p].first >= qu.A){
  42. ll h = dolls[p].second;
  43. auto it = upper_bound(tails.begin(), tails.end(), h);
  44. if(it == tails.end()) tails.push_back(h);
  45. else *it = h;
  46. --p;
  47. }
  48. ans[qu.id] = int(upper_bound(tails.begin(), tails.end(), qu.B) - tails.begin());
  49. }
  50.  
  51. for(int i=0;i<Q;i++) cout << ans[i] << '\n';
  52. return 0;
  53. }
Success #stdin #stdout 0s 5324KB
stdin
10 8
14 19
9 16
11 2
7 18
20 16
9 5
10 9
20 6
4 17
13 8
7 14
9 3
9 13
4 19
12 4
19 16
18 10
7 14
stdout
3
1
3
5
0
2
1
3