-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHaybales.java
More file actions
50 lines (43 loc) · 1.47 KB
/
Copy pathHaybales.java
File metadata and controls
50 lines (43 loc) · 1.47 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
package Solutions;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Haybales {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("haybales.in"));
int n = sc.nextInt();
int queries = sc.nextInt();
int bales[] = new int[n];
for(int i = 0; i < bales.length; i++) bales[i] = sc.nextInt();
Arrays.sort(bales);
PrintWriter pw = new PrintWriter(new File("haybales.out"));
while (queries-- > 0) {
sc.nextLine();
int l = sc.nextInt();
int r = sc.nextInt();
int lower_bound = n;
int upper_bound = 0;
int lo = 0, hi = n - 1;
while(lo <= hi){
int mid = (lo + hi)/2;
if(bales[mid] >= l){
hi = mid - 1;
lower_bound = Math.min(mid, lower_bound);
}else lo = mid + 1;
}
lo = 0; hi = n - 1;
while(lo <= hi){
int mid = (lo + hi)/2;
if(bales[mid] <= r){
lo = mid + 1;
upper_bound = Math.max(mid, upper_bound);
}else hi = mid -1;
}
pw.println(upper_bound - lower_bound + 1);
// System.out.println(upper_bound - lower_bound + 1);
}
pw.close();
}
}