☕ Java solution ✅ || 😼 Beats 86.75% ✨
\n\n\nimport java.util.*;\n\nclass Solution {\n\n    static int[] dist;\n    static int[] ans;\n    static List<Integer> obstacle;\n\n    static void buildTree(
![2024-06-26_15-41-27.png](https://assets.leetcode.com/users/images/50871d44-13b4-43b6-aa4b-91f0f89a43b6_1719405747.6424675.png)\n\n```\nimport java.util.*;\n\nclass Solution {\n\n    static int[] dist;\n    static int[] ans;\n    static List<Integer> obstacle;\n\n    static void buildTree(int l, int r, int idx) {\n        if (l == r) {\n            ans[idx] = dist[l];\n            return;\n        }\n        int mid = l + (r - l) / 2;\n        buildTree(l, mid, 2 * idx + 1);\n        buildTree(mid + 1, r, 2 * idx + 2);\n        ans[idx] = Math.max(ans[2 * idx + 1], ans[2 * idx + 2]);\n    }\n\n    static int findMax(int l, int r, int left, int right, int idx) {\n        if (r < left || l > right) return Integer.MIN_VALUE;\n        if (l >= left && r <= right) return ans[idx];\n        int mid = l + (r - l) / 2;\n        return Math.max(findMax(l, mid, left, right, 2 * idx + 1), findMax(mid + 1, r, left, right, 2 * idx + 2));\n    }\n\n    static void update(int l, int r, int idx, int pos, int val) {\n        if (l == r) {\n            ans[idx] = val;\n            return;\n        }\n        int mid = l + (r - l) / 2;\n        if (pos <= mid) update(l, mid, 2 * idx + 1, pos, val);\n        else update(mid + 1, r, 2 * idx + 2, pos, val);\n        ans[idx] = Math.max(ans[2 * idx + 1], ans[2 * idx + 2]);\n    }\n\n    public static int findNext(int x) {\n        int left = 0;\n        int right = obstacle.size() - 1;\n        while (left <= right) {\n            int mid = left + (right - left) / 2;\n            if (obstacle.get(mid) > x) right = mid - 1;\n            else left = mid + 1;\n        }\n        return left;\n    }\n\n    public static int findPrev(int x) {\n        int left = 0;\n        int right = obstacle.size() - 1;\n        while (left <= right) {\n            int mid = left + (right - left) / 2;\n            if (obstacle.get(mid) < x) left = mid + 1;\n            else right = mid - 1;\n        }\n        return right;\n    }\n\n    public static List<Boolean> getResults(int[][] q) {\n        int n = Math.min(50004, 3 * q.length) + 5;\n        dist = new int[n];\n        ans = new int[4 * n];\n        obstacle = new ArrayList<>();\n        obstacle.add(0);\n        obstacle.add(n - 1);\n        List<Boolean> results = new ArrayList<>();\n        dist[n - 1] = n - 1;\n        buildTree(0, n - 1, 0);\n        boolean flag = true;\n\n        for (int[] query : q) {\n            int type = query[0];\n            int x = query[1];\n            if (type == 1) {\n                flag = false;\n                int nx = findNext(x);\n                int pr = findPrev(x);\n                int next = obstacle.get(nx);\n                int prev = obstacle.get(pr);\n                obstacle.add(nx, x);\n                dist[x] = x - prev;\n                update(0, n - 1, 0, x, x - prev);\n                dist[next] = next - x;\n                update(0, n - 1, 0, next, next - x);\n            } else {\n                int sz = query[2];\n                int prev = findPrev(x);\n                int max = findMax(0, n - 1, 0, obstacle.get(prev), 0);\n                max = Math.max(max, x - obstacle.get(prev));\n                if (flag) max = x - obstacle.get(prev);\n                results.add(sz <= max);\n            }\n        }\n        return results;\n    }\n\n    public static void main(String[] args) {\n        int[][] q = {{2, 3, 1}};\n        System.out.println(getResults(q));\n    }\n}\n\n```
['Java']