Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cmath>
- #include <algorithm>
- #include <memory.h>
- #include <stdio.h>
- #include <vector>
- #include <stack>
- #include <deque>
- #include <queue>
- #include <vector>
- #include <set>
- using namespace std;
- vector <int> vc;
- int left_bin(int l, int r, int key)
- {
- int m;
- while (r - l > 1)
- {
- m = (r + l) / 2;
- if (vc[m] > key)
- {
- r = m;
- }
- else
- {
- l = m;
- }
- }
- /*if (vc[l] > key && l + 1 < vc.size())
- {
- return l + 1;
- }*/
- if (vc[vc.size() - 1] < key || vc[r] == key)
- {
- return r;
- }
- if (vc[l] <= key)
- {
- return l;
- }
- return -1;
- }
- int right_bin(int l, int r, int key)
- {
- int m;
- while (r - l > 1)
- {
- m = (r + l) / 2;
- if (vc[m] >= key)
- {
- r = m;
- }
- else
- {
- l = m;
- }
- }
- /*if (vc[r] > key && r + 1 < vc.size())
- {
- return r + 1;
- }*/
- if (vc[0] > key || vc[l] == key)
- {
- return l;
- }
- if (vc[r] >= key)
- {
- return r;
- }
- return -1;
- }
- int main() {
- int n, q;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- cin >> q;
- vc.push_back(q);
- }
- sort(vc.begin(), vc.end());
- /*for (int i = 0; i < n; i++)
- {
- cout << vc[i] << " ";
- }
- cout << endl;*/
- int k, l, r, a1, a2;
- cin >> k;
- for (int i = 0; i < k; i++)
- {
- cin >> l >> r;
- a1 = left_bin(0, n - 1, r);
- if (a1 == -1)
- {
- cout << 0 << " ";
- }
- else
- {
- a2 = right_bin(0, n - 1, l);
- if (a2 == -1)
- {
- cout << 0 << " ";
- }
- else
- {
- cout << a1 - a2 + 1 << " ";
- }
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement