-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count the elements
75 lines (57 loc) · 1.78 KB
/
Count the elements
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Count the elements
import java.io.*;
import java.util.*;
class Array {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcases = Integer.parseInt(br.readLine());
while(testcases-- > 0){
int n = Integer.parseInt(br.readLine());
String line1 = br.readLine();
String line2 = br.readLine();
String[] a1 = line1.trim().split("\\s+");
String[] b1 = line2.trim().split("\\s+");
int a[] = new int[n];
int b[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(a1[i]);
b[i] = Integer.parseInt(b1[i]);
}
int q = Integer.parseInt(br.readLine());
int query[] = new int[q];
for(int i = 0; i < q; i++) {
query[i] = Integer.parseInt(br.readLine());
}
Solution ob = new Solution();
int ans[] = ob.countElements(a, b, n, query, q);
for(int i = 0; i < q; i++)
System.out.println(ans[i]);
}
}
}
class Solution {
public static int[] countElements(int a[], int b[], int n, int query[], int q) {
int mx = -1;
for(int i = 0; i < n; i++) {
mx = Math.max(mx, b[i]);
}
int heap[] = new int[mx + 1];
Arrays.fill(heap, 0);
for(int i = 0; i < n; i++) {
heap[b[i]]++;
}
for(int i = 1; i <= mx; i++) {
heap[i] += heap[i - 1];
}
int ans[] = new int[q];
for(int i = 0; i < q; i++) {
if(a[query[i]] > mx) {
ans[i] = n;
}
else {
ans[i] = heap[a[query[i]]];
}
}
return ans;
}
}