Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.4 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
bool compare_second_descending(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first != b.first)
        return false; // Not needed if sorting pairs by first value already
    else
        return a.second > b.second; // Sort by second value in descending order
}

void find_and_print_range(const vector<pair<int, int>>& pairs, int range_start, int range_end) {
   auto low_it = lower_bound(pairs.begin(), pairs.end(), make_pair(range_start, 0), compare_second_descending);

    // Using STL upper_bound to find the ending position
    auto high_it = upper_bound(pairs.begin(), pairs.end(), make_pair(range_end, 0), compare_second_descending);

    int count = distance(low_it, high_it);

    cout << count << " mobiles are available" << endl;

    for (auto it = low_it; it != high_it; ++it) {
        cout << "Price " << it->first << " Speed " << it->second << "\n";
    }
}

bool compare_pair(const pair<int, int>& a, const pair<int, int>& b) {
    return a.first < b.first;
}

int main() {
    int n; 
    cin >> n;
    
    vector<pair<int,int>>a(n);
    
    for(int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    sort(begin(a), end(a), compare_pair);
    int q, l, r;
    cin >> q;
    for(int i = 0; i < q; i++) {
        cin >> l >> r;
        find_and_print_range(a, l, r);
    }
    
}
Leave a Comment