-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForFansStatistics.java
89 lines (72 loc) · 2.27 KB
/
ForFansStatistics.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class ForFansStatistics
{
public static Map<Long, List<Integer>> storage = new HashMap<>();
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int i = 0;
int n = in.nextInt();
while(n-- > 0) {
i++;
long value = in.nextLong();
if (!storage.containsKey(value)) {
storage.put(value, new ArrayList<>());
}
storage.get(value).add(i);
}
int q = in.nextInt();
//System.out.println("");
while (q-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
long value = in.nextLong();
//System.out.println("q="+q);
if (!storage.containsKey(value) ) {
System.out.print("0");
}
else {
List<Integer> list = storage.get(value);
long t = upperBound(list, 0, list.size() -1, r);
// System.out.println("t="+t);
if (t >= l && t <= r) {
System.out.print("1");
}
else {
System.out.print("0");
}
}
}
System.out.println("");
}
public static int upperBound(List<Integer> list, int from, int to, int value) {
//System.out.println(Arrays.toString( list.subList(from, to+1).toArray() ));
if (to < from) {
return -1;
}
else if (to == from) {
return list.get(to) > value ? -1 : list.get(to);
}
else if (to == from + 1) {
if (list.get(from) > value) {
return -1;
}
else if (list.get(to) > value) {
return list.get(from);
}
else {
return list.get(to);
}
}
int m = from + (to - from)/2;
int v = list.get(m);
if (v > value) {
return upperBound(list, from, m-1, value);
}
else {
return upperBound(list, m, to, value);
}
}
}