@@ -14,7 +14,7 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
14
14
nex = 2 * n ;
15
15
}
16
16
if (pre != -1 && pre < n && --fs [nex - pre ] == 0 ) {
17
- ff .unset (nex - pre );
17
+ ff .unsetPos (nex - pre );
18
18
}
19
19
}
20
20
if (lst .get (ind )) {
@@ -24,7 +24,7 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
24
24
nex = 2 * n ;
25
25
}
26
26
if (pre != -1 && pre < n && --fs [nex - pre ] == 0 ) {
27
- ff .unset (nex - pre );
27
+ ff .unsetPos (nex - pre );
28
28
}
29
29
}
30
30
if (lst .get (ind + 1 )) {
@@ -34,17 +34,17 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
34
34
nex = 2 * n ;
35
35
}
36
36
if (pre != -1 && pre < n && --fs [nex - pre ] == 0 ) {
37
- ff .unset (nex - pre );
37
+ ff .unsetPos (nex - pre );
38
38
}
39
39
}
40
- lst .unset (ind );
41
- lst .unset (ind + 1 );
40
+ lst .unsetPos (ind );
41
+ lst .unsetPos (ind + 1 );
42
42
c [ind ] ^= 1 ;
43
43
if (ind > 0 && c [ind ] != c [ind - 1 ]) {
44
- lst .set (ind );
44
+ lst .setPos (ind );
45
45
}
46
46
if (ind + 1 < c .length && c [ind + 1 ] != c [ind ]) {
47
- lst .set (ind + 1 );
47
+ lst .setPos (ind + 1 );
48
48
}
49
49
if (ind > 0 ) {
50
50
int pre = lst .prev (ind - 1 );
@@ -53,7 +53,7 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
53
53
nex = 2 * n ;
54
54
}
55
55
if (pre != -1 && pre < n && ++fs [nex - pre ] == 1 ) {
56
- ff .set (nex - pre );
56
+ ff .setPos (nex - pre );
57
57
}
58
58
}
59
59
if (lst .get (ind )) {
@@ -63,7 +63,7 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
63
63
nex = 2 * n ;
64
64
}
65
65
if (pre < n && ++fs [nex - pre ] == 1 ) {
66
- ff .set (nex - pre );
66
+ ff .setPos (nex - pre );
67
67
}
68
68
}
69
69
if (lst .get (ind + 1 )) {
@@ -73,7 +73,7 @@ private void go(int ind, LST lst, int[] fs, int n, LST ff, int[] c) {
73
73
nex = 2 * n ;
74
74
}
75
75
if (pre < n && ++fs [nex - pre ] == 1 ) {
76
- ff .set (nex - pre );
76
+ ff .setPos (nex - pre );
77
77
}
78
78
}
79
79
}
@@ -87,7 +87,7 @@ public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) {
87
87
LST lst = new LST (2 * n + 3 );
88
88
for (int i = 1 ; i < 2 * n ; i ++) {
89
89
if (c [i ] != c [i - 1 ]) {
90
- lst .set (i );
90
+ lst .setPos (i );
91
91
}
92
92
}
93
93
int [] fs = new int [2 * n + 1 ];
@@ -99,7 +99,7 @@ public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) {
99
99
ne = 2 * n ;
100
100
}
101
101
fs [ne - i ]++;
102
- ff .set (ne - i );
102
+ ff .setPos (ne - i );
103
103
}
104
104
}
105
105
List <Integer > ans = new ArrayList <>();
@@ -135,26 +135,29 @@ public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) {
135
135
}
136
136
137
137
private static class LST {
138
- public long [][] set ;
139
- public int n ;
138
+ private long [][] set ;
139
+ private int n ;
140
140
141
141
public LST (int n ) {
142
142
this .n = n ;
143
143
int d = 1 ;
144
- {
145
- int m = n ;
146
- while (m > 1 ) {
147
- m >>>= 6 ;
148
- d ++;
149
- }
150
- }
144
+ d = getD (n , d );
151
145
set = new long [d ][];
152
146
for (int i = 0 , m = n >>> 6 ; i < d ; i ++, m >>>= 6 ) {
153
147
set [i ] = new long [m + 1 ];
154
148
}
155
149
}
156
150
157
- public LST set (int pos ) {
151
+ private int getD (int n , int d ) {
152
+ int m = n ;
153
+ while (m > 1 ) {
154
+ m >>>= 6 ;
155
+ d ++;
156
+ }
157
+ return d ;
158
+ }
159
+
160
+ public LST setPos (int pos ) {
158
161
if (pos >= 0 && pos < n ) {
159
162
for (int i = 0 ; i < set .length ; i ++, pos >>>= 6 ) {
160
163
set [i ][pos >>> 6 ] |= 1L << pos ;
@@ -163,7 +166,7 @@ public LST set(int pos) {
163
166
return this ;
164
167
}
165
168
166
- public LST unset (int pos ) {
169
+ public LST unsetPos (int pos ) {
167
170
if (pos >= 0 && pos < n ) {
168
171
for (int i = 0 ;
169
172
i < set .length && (i == 0 || set [i - 1 ][pos ] == 0L );
@@ -179,7 +182,8 @@ public boolean get(int pos) {
179
182
}
180
183
181
184
public int prev (int pos ) {
182
- for (int i = 0 ; i < set .length && pos >= 0 ; i ++, pos >>>= 6 , pos --) {
185
+ int i = 0 ;
186
+ while (i < set .length && pos >= 0 ) {
183
187
int pre = prev (set [i ][pos >>> 6 ], pos & 63 );
184
188
if (pre != -1 ) {
185
189
pos = pos >>> 6 << 6 | pre ;
@@ -188,6 +192,9 @@ public int prev(int pos) {
188
192
}
189
193
return pos ;
190
194
}
195
+ i ++;
196
+ pos >>>= 6 ;
197
+ pos --;
191
198
}
192
199
return -1 ;
193
200
}
0 commit comments