-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStars.java
82 lines (64 loc) · 2.15 KB
/
Stars.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
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Stars
{
private static int size;
private static int[] tree;
public static int query(int id) {
int result = 0;
while (id > 0) {
result += tree[id];
/*
* To query an index, we need all indices that were not included in
* previous intervals of form [i - 2 ^ r + 1, i]. That is, we need
* to find the next lowest number that does not have the lowest bit
* in i set. (id & -id) represents the lowest bit in id; by
* subtracting this we reach the next number we need to query.
*/
id -= (id & -id);
}
return result;
}
public static void update(int id) {
while (id <= size) {
tree[id]++;
/*
* To update an index, we need to update all indices that subsume
* the current range [i - 2 ^ r + 1, i]. That is, we need to find
* the next highest number that has the lowest bit of i unset.
*/
id += (id & -id);
}
}
public static String getLine(BufferedReader br) {
String s = null;
try {
s = br.readLine();
}
catch (Exception e) {
}
return s;
}
public static void main(String[] argv)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String s2 = getLine(br);
int total = Integer.parseInt(s2);
size = 32002;
tree = new int[size + 1];
int byLevels[] = new int[total];
int cp = total;
while(cp-- > 0) {
String[] s = getLine(br).split(" ");
int x = Integer.parseInt(s[0]);
x++;
byLevels[query(x)]++;
update(x);
}
for (int i = 0; i < total; i++) {
System.out.println(byLevels[i]);
}
}
}