Solution using segment tree with step by step explanation
IntuitionI first solved it using linear time complexity for each query, which gave me time limit exceeded error.
Later I read up about the segment trees and tri
# Intuition
I first solved it using linear time complexity for each query, which gave me time limit exceeded error. 
Later I read up about the segment trees and tried this approach using the segment tree.
The important part is to understand the base conditions when getting the value out of the segment tree for a particular range. Please look thoroughly at it.

# Approach
We first need to understand what a segment tree is, and how we can use it for the approach. Check out below video to understand about segment tree:
[Video explaining segment tree](https://www.loom.com/share/12089afbc7de4d4c8d77d15f6d0eb382?sid=80a403cf-e787-4a67-bbd0-bfa8a7a30b99)


# Complexity
- O(klogn); k= no of queries, n = size of the segment tree

- Space complexity:
- O(n): n = size of the segment tree

# Code
```java []
class Solution {
    private int maxValue;
    public List<Boolean> getResults(int[][] queries) {
        maxValue=50001;
        int[] sTree = new int[4*maxValue];
        TreeSet<Integer> treeset = new TreeSet<Integer>();
        treeset.add(0);
        treeset.add(maxValue);
        // updates the sTree for idx=0 with initial value of 50000
        updateSTree(0, maxValue, sTree);
        updateSTree(maxValue, 0, sTree);
        
        List<Boolean> result = new ArrayList<>();
        for(int[] query: queries) {
            if(query.length == 2) {                
                int blockPointer = query[1];                
                int nextBlockPointer = treeset.ceiling(blockPointer);
                int prevBlockPointer = treeset.floor(blockPointer);
                treeset.add(blockPointer);
                updateSTree(prevBlockPointer, blockPointer-prevBlockPointer, sTree);
                updateSTree(blockPointer, nextBlockPointer-blockPointer, sTree);
            } else {
                int size = query[2];
                int limit = query[1]-size;
                /* we need to search the size within 0-limit so that 
                we can place the block
                */
                int maxValueInTheSTree = getMaxRangeValueInSTree(0, 0, maxValue, 0, limit, sTree);
                result.add(maxValueInTheSTree >= size);
            }
        }
        return result;
    }

    private void updateSTree(int idx, int value, int[] sTree) {
        updateStree(0, 0, maxValue, idx, value, sTree);
    }

    /*
    Method to update the new individual value in STree and 
    update all the affected ranges because of that
    */
    private void updateStree(int i, int l, int r, int idx, int value, int[] sTree) {
        // we have found the idx for which we need to make the update!
        if(l==r) {        
            sTree[i]=value;
            return;
        }
        int mid = l + (r-l)/2;
        int leftChild = 2*i+1;
        int rightChild = 2*i+2;
        if(idx<=mid) {
            updateStree(leftChild, l, mid, idx, value, sTree);
        } else {
            updateStree(rightChild, mid+1, r, idx, value, sTree);
        }
        sTree[i] = Integer.max(sTree[leftChild], sTree[rightChild]);
    }

    private int getMaxRangeValueInSTree(int i, int l, int r, int start, int end, int[] sTree) {
        // the asked range completely gets convered in the asked range
        if(start==l && end==r) {
            return sTree[i];
        }
        if(start>end) {
            return 0;
        }
        if(!isRangeOverLapping(l,r,start,end)){
            return 0;
        }
        int mid = l + (r-l)/2;
        int leftChild = 2*i+1;
        int rightChild = 2*i+2;
        
        int leftVal = getMaxRangeValueInSTree(leftChild, l, mid, start, Integer.min(end,mid), sTree);
        int rightVal = getMaxRangeValueInSTree(rightChild, mid+1, r, Integer.max(start, mid+1), end, sTree);
        return Integer.max(leftVal, rightVal);
    }

    private boolean isRangeOverLapping(int l, int r, int start, int end) {
        return (start >= l && start <= r) || (end >= l && end <= r);
    }


}
```
['Java']