https://www.acmicpc.net/problem/11663
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
arr = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
sb.append(upperBound(end) - lowerBound(start)).append("\n");
}
System.out.println(sb);
}
static int upperBound(int x) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] > x) end = mid - 1;
else start = mid + 1;
}
return start;
}
static int lowerBound(int x) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= x) end = mid - 1;
else start = mid + 1;
}
return start;
}
}
'ALGORITHM' 카테고리의 다른 글
[JAVA] 백준 16198번- 에너지 모으기 (0) | 2023.01.04 |
---|---|
[JAVA] 백준 9663번- N-Queen (0) | 2023.01.04 |
[JAVA] 백준 2512 - 예산 (0) | 2022.12.30 |
[JAVA] 백준 2343 - 기타 레슨 (0) | 2022.12.29 |
[JAVA] 백준 2110 - 공유기 설치 (0) | 2022.12.29 |