Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- int arr[100000];
- int N, M;
- int binarySearchLow(int bound) {
- int start = 0, end = N-1, mid, save;
- while (start <= end) {
- mid = (start + end) / 2;
- if (arr[mid] >= bound) {
- save = mid;
- end = mid - 1;
- }
- else {
- start = mid + 1;
- }
- }
- return save;
- }
- int binarySearchHigh(int bound) {
- int start = 0, end = N - 1, mid, save;
- while (start <= end) {
- mid = (start + end) / 2;
- if (arr[mid] <= bound) {
- save = mid;
- start = mid + 1;
- }
- else {
- end = mid - 1;
- }
- }
- return save;
- }
- int main()
- {
- scanf("%d", &N);
- for (int i = 0; i < N; i++) {
- scanf("%d", &arr[i]);
- }
- scanf("%d", &M);
- int lowBound, highBound, lowIndex, highIndex;
- for (int i = 0; i < M; i++) {
- scanf("%d %d", &lowBound, &highBound);
- lowIndex = binarySearchLow(lowBound);
- highIndex = binarySearchHigh(highBound);
- printf("%d\n", highIndex - lowIndex + 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement