I had a task to find if multiple numbers (n) are in multiple intervals (m) and I was pointed to lower_bound and upper_bound to do the checking faster than O(nm) , something like O((n+m)*log(n)).
#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cout.tie(0);
int n, m, temp, temp1;
vector <pair<int, int>> uogienes;
vector <int> erskeciai;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> temp;
erskeciai.push_back(temp);
}
temp = 0;
for (int i = 0; i < m; i++) {
cin >> temp >> temp1;
uogienes.push_back(make_pair(temp, temp1));
}
for (int i = 0; i < m; i++) {
temp = 0;
for (int h = 0; h < n; h++) {
if (uogienes[i].first <= erskeciai[h] && uogienes[i].second >= erskeciai[h]) {
temp++;
}
}
cout << temp << "\n";
}
return 0;
}
Now I have no idea how to progress further with lower_bound or upper_bound, since they are pretty new to me, so are iterators etc. Could anyone help me with this task?