-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11663.cpp
More file actions
53 lines (49 loc) · 1.2 KB
/
Copy path11663.cpp
File metadata and controls
53 lines (49 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
#define FastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
int dot[100001];
int binary_search(int start, int end, int target){
if(start>=end)return start;
int mid = (start+end)/2;
if(dot[mid]==target){
return mid;
}
else if(dot[mid]>target){
end=mid-1;
}
else{
start=mid+1;
}
return binary_search(start, end, target);
}
int main(void){
FastIO;
int n,m;
cin>>n>>m;
for(int i=0; i<n; i++){
cin>>dot[i];
}
sort(dot,dot+n);
for(int t=0; t<m; t++){
int first,second;
cin>>first>>second;
int tmp = first;
if(first>second){
first=second;
second=tmp;
}
int firstIndex,secondIndex;
firstIndex=binary_search(0,n-1, first);
secondIndex=binary_search(firstIndex, n-1, second);
if(dot[firstIndex]<first)firstIndex+=1;
if(dot[secondIndex]>second)secondIndex-=1;
if(firstIndex==secondIndex){
cout<<0<<"\n";
continue;
}
cout<<secondIndex-firstIndex<<"\n";
}
}