|
1 | 1 | package g3101_3200.s3161_block_placement_queries;
|
2 | 2 |
|
3 | 3 | // #Hard #Array #Binary_Search #Segment_Tree #Binary_Indexed_Tree
|
4 |
| -// #2024_05_30_Time_137_ms_(99.38%)_Space_143.7_MB_(54.52%) |
| 4 | +// #2025_03_16_Time_47_ms_(100.00%)_Space_144.38_MB_(56.41%) |
5 | 5 |
|
6 |
| -import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
7 | 7 | import java.util.List;
|
8 | 8 |
|
9 | 9 | public class Solution {
|
10 |
| - private static class Seg { |
11 |
| - private final int start; |
12 |
| - private final int end; |
13 |
| - private int min; |
14 |
| - private int max; |
15 |
| - private int len; |
16 |
| - private boolean obstacle; |
17 |
| - private Seg left; |
18 |
| - private Seg right; |
19 |
| - |
20 |
| - public static Seg init(int n) { |
21 |
| - return new Seg(0, n); |
22 |
| - } |
23 |
| - |
24 |
| - private Seg(int start, int end) { |
25 |
| - this.start = start; |
26 |
| - this.end = end; |
27 |
| - if (start >= end) { |
28 |
| - return; |
| 10 | + public List<Boolean> getResults(int[][] queries) { |
| 11 | + int m = queries.length; |
| 12 | + int[] pos = new int[m + 1]; |
| 13 | + int size = 0; |
| 14 | + pos[size++] = 0; |
| 15 | + int max = 0; |
| 16 | + for (int[] q : queries) { |
| 17 | + max = Math.max(max, q[1]); |
| 18 | + if (q[0] == 1) { |
| 19 | + pos[size++] = q[1]; |
29 | 20 | }
|
30 |
| - int mid = start + ((end - start) >> 1); |
31 |
| - left = new Seg(start, mid); |
32 |
| - right = new Seg(mid + 1, end); |
33 |
| - refresh(); |
34 | 21 | }
|
| 22 | + Arrays.sort(pos, 0, size); |
| 23 | + max++; |
| 24 | + UnionFind left = new UnionFind(max + 1); |
| 25 | + UnionFind right = new UnionFind(max + 1); |
| 26 | + BIT bit = new BIT(max); |
| 27 | + initializePositions(size, pos, bit, left, right, max); |
| 28 | + return List.of(getBooleans(queries, m, size, left, right, bit)); |
| 29 | + } |
35 | 30 |
|
36 |
| - public void set(int i) { |
37 |
| - if (i < start || i > end) { |
38 |
| - return; |
39 |
| - } else if (i == start && i == end) { |
40 |
| - obstacle = true; |
41 |
| - min = max = start; |
42 |
| - return; |
| 31 | + private void initializePositions( |
| 32 | + int size, int[] pos, BIT bit, UnionFind left, UnionFind right, int max) { |
| 33 | + for (int i = 1; i < size; i++) { |
| 34 | + int pre = pos[i - 1]; |
| 35 | + int cur = pos[i]; |
| 36 | + bit.update(cur, cur - pre); |
| 37 | + for (int j = pre + 1; j < cur; j++) { |
| 38 | + left.parent[j] = pre; |
| 39 | + right.parent[j] = cur; |
43 | 40 | }
|
44 |
| - left.set(i); |
45 |
| - right.set(i); |
46 |
| - refresh(); |
47 | 41 | }
|
| 42 | + for (int j = pos[size - 1] + 1; j < max; j++) { |
| 43 | + left.parent[j] = pos[size - 1]; |
| 44 | + right.parent[j] = max; |
| 45 | + } |
| 46 | + } |
48 | 47 |
|
49 |
| - private void refresh() { |
50 |
| - if (left.obstacle) { |
51 |
| - min = left.min; |
52 |
| - if (right.obstacle) { |
53 |
| - max = right.max; |
54 |
| - len = Math.max(right.min - left.max, Math.max(left.len, right.len)); |
55 |
| - } else { |
56 |
| - max = left.max; |
57 |
| - len = Math.max(left.len, right.end - left.max); |
58 |
| - } |
59 |
| - obstacle = true; |
60 |
| - } else if (right.obstacle) { |
61 |
| - min = right.min; |
62 |
| - max = right.max; |
63 |
| - len = Math.max(right.len, right.min - left.start); |
64 |
| - obstacle = true; |
| 48 | + private Boolean[] getBooleans( |
| 49 | + int[][] queries, int m, int size, UnionFind left, UnionFind right, BIT bit) { |
| 50 | + Boolean[] ans = new Boolean[m - size + 1]; |
| 51 | + int index = ans.length - 1; |
| 52 | + for (int i = m - 1; i >= 0; i--) { |
| 53 | + int[] q = queries[i]; |
| 54 | + int x = q[1]; |
| 55 | + int pre = left.find(x - 1); |
| 56 | + if (q[0] == 1) { |
| 57 | + int next = right.find(x + 1); |
| 58 | + left.parent[x] = pre; |
| 59 | + right.parent[x] = next; |
| 60 | + bit.update(next, next - pre); |
65 | 61 | } else {
|
66 |
| - len = end - start; |
| 62 | + int maxGap = Math.max(bit.query(pre), x - pre); |
| 63 | + ans[index--] = maxGap >= q[2]; |
67 | 64 | }
|
68 | 65 | }
|
| 66 | + return ans; |
| 67 | + } |
| 68 | + |
| 69 | + private static final class BIT { |
| 70 | + int n; |
| 71 | + int[] tree; |
69 | 72 |
|
70 |
| - public void max(int n, int[] t) { |
71 |
| - if (end <= n) { |
72 |
| - t[0] = Math.max(t[0], len); |
73 |
| - if (obstacle) { |
74 |
| - t[1] = max; |
75 |
| - } |
76 |
| - return; |
| 73 | + public BIT(int n) { |
| 74 | + this.n = n; |
| 75 | + tree = new int[n]; |
| 76 | + } |
| 77 | + |
| 78 | + public void update(int i, int v) { |
| 79 | + while (i < n) { |
| 80 | + tree[i] = Math.max(tree[i], v); |
| 81 | + i += i & -i; |
77 | 82 | }
|
78 |
| - left.max(n, t); |
79 |
| - if (!right.obstacle || right.min >= n) { |
80 |
| - return; |
| 83 | + } |
| 84 | + |
| 85 | + public int query(int i) { |
| 86 | + int result = 0; |
| 87 | + while (i > 0) { |
| 88 | + result = Math.max(result, tree[i]); |
| 89 | + i &= i - 1; |
81 | 90 | }
|
82 |
| - t[0] = Math.max(t[0], right.min - t[1]); |
83 |
| - right.max(n, t); |
| 91 | + return result; |
84 | 92 | }
|
85 | 93 | }
|
86 | 94 |
|
87 |
| - public List<Boolean> getResults(int[][] queries) { |
88 |
| - int max = 0; |
89 |
| - for (int[] i : queries) { |
90 |
| - max = Math.max(max, i[1]); |
| 95 | + private static final class UnionFind { |
| 96 | + private final int[] parent; |
| 97 | + |
| 98 | + public UnionFind(int n) { |
| 99 | + parent = new int[n]; |
| 100 | + for (int i = 1; i < n; i++) { |
| 101 | + parent[i] = i; |
| 102 | + } |
91 | 103 | }
|
92 |
| - Seg root = Seg.init(max); |
93 |
| - root.set(0); |
94 | 104 |
|
95 |
| - List<Boolean> res = new ArrayList<>(queries.length); |
96 |
| - for (int[] i : queries) { |
97 |
| - if (i[0] == 1) { |
98 |
| - root.set(i[1]); |
99 |
| - } else { |
100 |
| - int[] t = new int[2]; |
101 |
| - root.max(i[1], t); |
102 |
| - res.add(Math.max(t[0], i[1] - t[1]) >= i[2]); |
| 105 | + public int find(int x) { |
| 106 | + if (parent[x] != x) { |
| 107 | + parent[x] = find(parent[x]); |
103 | 108 | }
|
| 109 | + return parent[x]; |
104 | 110 | }
|
105 |
| - return res; |
106 | 111 | }
|
107 | 112 | }
|
0 commit comments