Skip to content

Commit 41af92c

Browse files
committed
[BOJ]선분 위의 점/실버3/1시간
1 parent a54ff82 commit 41af92c

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//BOJ11663 선분 위의 점, 실버3
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BOJ11663 {
6+
static int N, M;
7+
static long[] point;
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
13+
N = Integer.parseInt(st.nextToken());
14+
M = Integer.parseInt(st.nextToken());
15+
point = new long[N];
16+
17+
st = new StringTokenizer(br.readLine());
18+
for(int i = 0; i < N; i++) {
19+
int n = Integer.parseInt(st.nextToken());
20+
point[i] = n;
21+
}
22+
Arrays.sort(point);
23+
24+
long ans = 0;
25+
for(int j = 0; j < M; j++) {
26+
st = new StringTokenizer(br.readLine());
27+
int x = Integer.parseInt(st.nextToken());
28+
int y = Integer.parseInt(st.nextToken());
29+
30+
ans = BinarySearch(x, y);
31+
bw.write(ans +"\n");
32+
}
33+
bw.flush();
34+
bw.close();
35+
br.close();
36+
}
37+
static long BinarySearch(int x, int y) {
38+
int start = 0;
39+
int end = point.length-1;
40+
41+
while(start <= end) {
42+
int mid = (start + end) / 2;
43+
44+
if(point[mid] < x) {
45+
start = mid + 1;
46+
}else {
47+
end = mid - 1;
48+
}
49+
}
50+
int startIndex = start;
51+
52+
start = 0;
53+
end = point.length-1;
54+
55+
while(start <= end) {
56+
int mid = (start + end) / 2;
57+
58+
if(y < point[mid]) {
59+
end = mid - 1;
60+
}else {
61+
start = mid + 1;
62+
}
63+
}
64+
int endIndex = end + 1;
65+
66+
// System.out.println(startIndex + " " + endIndex);
67+
return endIndex - startIndex;
68+
}
69+
}
70+
71+
72+
73+

0 commit comments

Comments
 (0)