|
1 | 1 | package g3201_3300.s3245_alternating_groups_iii; |
2 | 2 |
|
3 | | -// #Hard #Array #Binary_Indexed_Tree #2025_02_12_Time_135_ms_(86.36%)_Space_84.24_MB_(40.91%) |
| 3 | +// #Hard #Array #Binary_Indexed_Tree #2025_03_14_Time_38_ms_(91.84%)_Space_77.53_MB_(87.76%) |
4 | 4 |
|
5 | 5 | import java.util.ArrayList; |
| 6 | +import java.util.BitSet; |
6 | 7 | import java.util.List; |
7 | | -import java.util.Map; |
8 | | -import java.util.TreeMap; |
9 | 8 |
|
10 | 9 | public class Solution { |
11 | | - private static final int SZ = 63333; |
12 | | - private static final int OFFSET = SZ - 10; |
13 | | - private static final BIT[] BITS = {new BIT(), new BIT()}; |
14 | | - |
15 | | - // Binary Indexed Tree (BIT) class. |
16 | | - private static class BIT { |
17 | | - int[] bs = new int[SZ]; |
18 | | - |
19 | | - // Update BIT: add value y to index x. |
20 | | - void update(int x, int y) { |
21 | | - x = OFFSET - x; |
22 | | - for (; x < SZ; x += x & -x) { |
23 | | - bs[x] += y; |
24 | | - } |
25 | | - } |
26 | | - |
27 | | - // Query BIT: get the prefix sum up to index x. |
28 | | - int query(int x) { |
29 | | - x = OFFSET - x; |
30 | | - int ans = 0; |
31 | | - for (; x > 0; x -= x & -x) { |
32 | | - ans += bs[x]; |
| 10 | + public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) { |
| 11 | + int n = colors.length; |
| 12 | + BitSet set = new BitSet(); |
| 13 | + BIT bit = new BIT(n); |
| 14 | + for (int i = 0; i < n; i++) { |
| 15 | + if (colors[i] == colors[getIndex(i + 1, n)]) { |
| 16 | + add(set, bit, n, i); |
33 | 17 | } |
34 | | - return ans; |
35 | 18 | } |
36 | | - |
37 | | - // Clear BIT values starting from index x. |
38 | | - void clear(int x) { |
39 | | - x = OFFSET - x; |
40 | | - for (; x < SZ; x += x & -x) { |
41 | | - bs[x] = 0; |
| 19 | + List<Integer> ans = new ArrayList<>(); |
| 20 | + for (int[] q : queries) { |
| 21 | + if (q[0] == 1) { |
| 22 | + if (set.isEmpty()) { |
| 23 | + ans.add(n); |
| 24 | + } else { |
| 25 | + int size = q[1]; |
| 26 | + int[] res = bit.query(size); |
| 27 | + ans.add(res[1] - res[0] * (size - 1)); |
| 28 | + } |
| 29 | + } else { |
| 30 | + int i = q[1]; |
| 31 | + int color = colors[i]; |
| 32 | + if (q[2] == color) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + int pre = getIndex(i - 1, n); |
| 36 | + if (colors[pre] == color) { |
| 37 | + remove(set, bit, n, pre); |
| 38 | + } |
| 39 | + int next = getIndex(i + 1, n); |
| 40 | + if (colors[next] == color) { |
| 41 | + remove(set, bit, n, i); |
| 42 | + } |
| 43 | + colors[i] ^= 1; |
| 44 | + color = colors[i]; |
| 45 | + if (colors[pre] == color) { |
| 46 | + add(set, bit, n, pre); |
| 47 | + } |
| 48 | + if (colors[next] == color) { |
| 49 | + add(set, bit, n, i); |
| 50 | + } |
42 | 51 | } |
43 | 52 | } |
| 53 | + return ans; |
44 | 54 | } |
45 | 55 |
|
46 | | - // --- BIT wrapper methods --- |
47 | | - // Updates both BITs for a given group length. |
48 | | - private void edt(int x, int y) { |
49 | | - // Second BIT is updated with x * y. |
50 | | - BITS[1].update(x, x * y); |
51 | | - // First BIT is updated with y. |
52 | | - BITS[0].update(x, y); |
53 | | - } |
54 | | - |
55 | | - // Combines BIT queries to get the result for a given x. |
56 | | - private int qry(int x) { |
57 | | - return BITS[1].query(x) + (1 - x) * BITS[0].query(x); |
58 | | - } |
59 | | - |
60 | | - // Returns the length of a group from index x to y. |
61 | | - private int len(int x, int y) { |
62 | | - return y - x + 1; |
63 | | - } |
64 | | - |
65 | | - // --- Group operations --- |
66 | | - // Removes a group (block) by updating BIT with a negative value. |
67 | | - private void removeGroup(int start, int end) { |
68 | | - edt(len(start, end), -1); |
69 | | - } |
70 | | - |
71 | | - // Adds a group (block) by updating BIT with a positive value. |
72 | | - private void addGroup(int start, int end) { |
73 | | - edt(len(start, end), 1); |
74 | | - } |
75 | | - |
76 | | - // Initializes the alternating groups using the colors array. |
77 | | - private void initializeGroups(int[] colors, TreeMap<Integer, Integer> groups) { |
78 | | - int n = colors.length; |
79 | | - int i = 0; |
80 | | - while (i < n) { |
81 | | - int r = i; |
82 | | - // Determine the end of the current alternating group. |
83 | | - while (r < n && (colors[r] + colors[i] + r + i) % 2 == 0) { |
84 | | - ++r; |
85 | | - } |
86 | | - // The group spans from index i to r-1. |
87 | | - groups.put(i, r - 1); |
88 | | - // Update BITs with the length of this group. |
89 | | - edt(r - i, 1); |
90 | | - // Skip to the end of the current group. |
91 | | - i = r - 1; |
92 | | - ++i; |
| 56 | + private void add(BitSet set, BIT bit, int n, int i) { |
| 57 | + if (set.isEmpty()) { |
| 58 | + bit.update(n, 1); |
| 59 | + } else { |
| 60 | + update(set, bit, n, i, 1); |
93 | 61 | } |
| 62 | + set.set(i); |
94 | 63 | } |
95 | 64 |
|
96 | | - // Processes a type 1 query: returns the number of alternating groups |
97 | | - // of at least the given size. |
98 | | - private int processQueryType1(int[] colors, TreeMap<Integer, Integer> groups, int groupSize) { |
99 | | - int ans = qry(groupSize); |
100 | | - Map.Entry<Integer, Integer> firstGroup = groups.firstEntry(); |
101 | | - Map.Entry<Integer, Integer> lastGroup = groups.lastEntry(); |
102 | | - // If there is more than one group and the first and last colors differ, |
103 | | - // adjust the answer by "merging" the groups at the boundaries. |
104 | | - if (firstGroup != lastGroup && colors[0] != colors[colors.length - 1]) { |
105 | | - int leftLen = len(firstGroup.getKey(), firstGroup.getValue()); |
106 | | - int rightLen = len(lastGroup.getKey(), lastGroup.getValue()); |
107 | | - ans -= Math.max(leftLen - groupSize + 1, 0); |
108 | | - ans -= Math.max(rightLen - groupSize + 1, 0); |
109 | | - ans += Math.max(leftLen + rightLen - groupSize + 1, 0); |
| 65 | + private void remove(BitSet set, BIT bit, int n, int i) { |
| 66 | + set.clear(i); |
| 67 | + if (set.isEmpty()) { |
| 68 | + bit.update(n, -1); |
| 69 | + } else { |
| 70 | + update(set, bit, n, i, -1); |
110 | 71 | } |
111 | | - return ans; |
112 | 72 | } |
113 | 73 |
|
114 | | - // Processes a type 2 query: updates the color at index x and adjusts groups. |
115 | | - private void processQueryType2( |
116 | | - int[] colors, TreeMap<Integer, Integer> groups, int x, int newColor) { |
117 | | - if (colors[x] == newColor) { |
118 | | - return; |
119 | | - } |
120 | | - // Update the color at index x. |
121 | | - colors[x] = newColor; |
122 | | - // Find the group (block) that contains index x. |
123 | | - Map.Entry<Integer, Integer> it = groups.floorEntry(x); |
124 | | - int l = it.getKey(); |
125 | | - int r = it.getValue(); |
126 | | - // Remove the old group from BIT and map. |
127 | | - removeGroup(l, r); |
128 | | - groups.remove(l); |
129 | | - int ml = x; |
130 | | - int mr = x; |
131 | | - // Process the left side of index x. |
132 | | - if (l != x) { |
133 | | - groups.put(l, x - 1); |
134 | | - addGroup(l, x - 1); |
135 | | - } else { |
136 | | - if (x > 0 && colors[x] != colors[x - 1]) { |
137 | | - it = groups.floorEntry(x - 1); |
138 | | - if (it != null) { |
139 | | - ml = it.getKey(); |
140 | | - removeGroup(it.getKey(), it.getValue()); |
141 | | - groups.remove(it.getKey()); |
142 | | - } |
143 | | - } |
| 74 | + private void update(BitSet set, BIT bit, int n, int i, int v) { |
| 75 | + int pre = set.previousSetBit(i); |
| 76 | + if (pre == -1) { |
| 77 | + pre = set.previousSetBit(n); |
144 | 78 | } |
145 | | - // Process the right side of index x. |
146 | | - if (r != x) { |
147 | | - groups.put(x + 1, r); |
148 | | - addGroup(x + 1, r); |
149 | | - } else { |
150 | | - if (x + 1 < colors.length && colors[x + 1] != colors[x]) { |
151 | | - it = groups.ceilingEntry(x + 1); |
152 | | - if (it != null) { |
153 | | - mr = it.getValue(); |
154 | | - removeGroup(it.getKey(), it.getValue()); |
155 | | - groups.remove(it.getKey()); |
156 | | - } |
157 | | - } |
| 79 | + int next = set.nextSetBit(i); |
| 80 | + if (next == -1) { |
| 81 | + next = set.nextSetBit(0); |
158 | 82 | } |
| 83 | + bit.update(getIndex(next - pre + n - 1, n) + 1, -v); |
| 84 | + bit.update(getIndex(i - pre, n), v); |
| 85 | + bit.update(getIndex(next - i, n), v); |
| 86 | + } |
159 | 87 |
|
160 | | - // Merge the affected groups into one new group. |
161 | | - groups.put(ml, mr); |
162 | | - addGroup(ml, mr); |
| 88 | + private int getIndex(int index, int mod) { |
| 89 | + int result = index >= mod ? index - mod : index; |
| 90 | + return index < 0 ? index + mod : result; |
163 | 91 | } |
164 | 92 |
|
165 | | - // Clears both BITs. This is done after processing all queries. |
166 | | - private void clearAllBITs(int n) { |
167 | | - for (int i = 0; i <= n + 2; ++i) { |
168 | | - BITS[0].clear(i); |
169 | | - BITS[1].clear(i); |
| 93 | + private static class BIT { |
| 94 | + int n; |
| 95 | + int[] tree1; |
| 96 | + int[] tree2; |
| 97 | + |
| 98 | + BIT(int n) { |
| 99 | + this.n = n + 1; |
| 100 | + tree1 = new int[n + 1]; |
| 101 | + tree2 = new int[n + 1]; |
170 | 102 | } |
171 | | - } |
172 | 103 |
|
173 | | - // Main function to handle queries on alternating groups. |
174 | | - public List<Integer> numberOfAlternatingGroups(int[] colors, int[][] queries) { |
175 | | - TreeMap<Integer, Integer> groups = new TreeMap<>(); |
176 | | - int n = colors.length; |
177 | | - List<Integer> results = new ArrayList<>(); |
178 | | - // Initialize alternating groups. |
179 | | - initializeGroups(colors, groups); |
180 | | - // Process each query. |
181 | | - for (int[] query : queries) { |
182 | | - if (query[0] == 1) { |
183 | | - // Type 1 query: count alternating groups of at least a given size. |
184 | | - int groupSize = query[1]; |
185 | | - int ans = processQueryType1(colors, groups, groupSize); |
186 | | - results.add(ans); |
187 | | - } else { |
188 | | - // Type 2 query: update the color at a given index. |
189 | | - int index = query[1]; |
190 | | - int newColor = query[2]; |
191 | | - processQueryType2(colors, groups, index, newColor); |
| 104 | + void update(int size, int v) { |
| 105 | + for (int i = size; i > 0; i -= i & -i) { |
| 106 | + tree1[i] += v; |
| 107 | + tree2[i] += v * size; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + int[] query(int size) { |
| 112 | + int count = 0; |
| 113 | + int sum = 0; |
| 114 | + for (int i = size; i < n; i += i & -i) { |
| 115 | + count += tree1[i]; |
| 116 | + sum += tree2[i]; |
192 | 117 | } |
| 118 | + return new int[] {count, sum}; |
193 | 119 | } |
194 | | - // Clear BITs after processing all queries. |
195 | | - clearAllBITs(n); |
196 | | - return results; |
197 | 120 | } |
198 | 121 | } |
0 commit comments