-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPBDS_COORDINATE_COMPRESSION
184 lines (162 loc) · 3.32 KB
/
PBDS_COORDINATE_COMPRESSION
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
Author : Akshat Jindal
Codforces Handle : akshatj07
*/
class PBDS{
private int N; // tot number of Nodes
private int[] key; // Coordinate compression ( INDEX : ELEMENTS)
private int[] bit; // Binary Index Tree
private int tot; // Keep track of number of elements
private int[] count; // count of elements
private boolean duplicate; // duplicates are Allowed or Not
PBDS(boolean duplicate , int[]... ele){
this.duplicate = duplicate;
TreeSet<Integer> tree = new TreeSet<>();
for(int[] arr:ele) {
for(int e:arr) tree.add(e);
}
this.N = tree.size()+1;
key = new int[N];
for(int i = 1 ; i<N ; i++) {
key[i] = tree.first();
tree.remove(key[i]);
}
bit = new int[N];
this.tot = 0;
count = new int[N];
}
// update BIT
private void update(int ind , int val ) {
for( ; ind<N ;ind += (ind & (-ind))) {
bit[ind] += val;
}
}
// PREFIX SUM for given index
private int val(int ind) {
int tot = 0;
while(ind > 0) {
tot += bit[ind];
ind -= (ind & (-ind));
}
return tot;
}
// Index on which the Element present
private Integer index(int ele) {
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(key[m] > ele) r = m-1;
else l = m+1;
}
if(r > 0 && key[r] == ele) return r;
else return null;
}
void add(int ele) {
Integer ind = index(ele);
if(ind == null) return;
if(!duplicate && count[ind] > 0) return;
update(ind , 1);
count[ind]++;
tot++;
}
boolean contains(int ele) {
Integer ind = index(ele);
if(ind == null) return false;
return count[ind] > 0;
}
boolean remove(int ele) {
Integer ind = index(ele);
if(ind == null) return false;
if(count[ind] == 0 ) return false;
update(ind , -1);
count[ind]--;
tot--;
return true;
}
int size() {
return tot;
}
Integer eleAtIndex(int index) {
if(index > tot || index < 1) return null;
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(val(m) < index) l = m+1;
else r = m-1;
}
return key[l];
}
int count_ceil(int ele) {
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(key[m] < ele) l = m+1;
else r = m-1;
}
int ind = l-1;
int count = val(ind);
return tot - count;
}
Integer ceil(int ele) {
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(key[m] < ele) l = m+1;
else r = m-1;
}
int ind = l;
if(ind == N) return null;
int v = val(ind);
if(count[ind] == 0) v++;
l = 1 ; r = N-1;
l = 1 ; r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(val(m) < v) l = m+1;
else r = m-1;
}
if(l == N) return null;
return key[l];
}
int count_floor(int ele) {
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(key[m] > ele) r = m-1;
else l = m+1;
}
int ind = r;
int count = val(ind);
return count;
}
Integer floor(int ele) {
int l = 1 , r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(key[m] > ele) r = m-1;
else l = m+1;
}
int ind = r;
int v = val(ind);
if(v == 0) return null;
l = 1 ; r = N-1;
while(l<=r) {
int m = (l+r)/2;
if(val(m) < v) l = m+1;
else r = m-1;
}
if(l == N) return null;
return key[l];
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 1 ; i<N ; i++) {
int c = count[i];
int ele = key[i];
while(c -- >0) {
sb.append(ele +",");
}
}
return String.valueOf(sb);
}
}