<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/change.png" style="width: 635px; height: 312px;" /></p>

<p>Winston was given the above mysterious function <code>func</code>. He has an integer array <code>arr</code> and an integer <code>target</code> and he wants to find the values <code>l</code> and <code>r</code> that make the value <code>|func(arr, l, r) - target|</code> minimum possible.</p>

<p>Return <em>the minimum possible value</em> of <code>|func(arr, l, r) - target|</code>.</p>

<p>Notice that <code>func</code> should be called with the values <code>l</code> and <code>r</code> where <code>0 &lt;= l, r &lt; arr.length</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [9,12,3,7,15], target = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1000000,1000000,1000000], target = 1
<strong>Output:</strong> 999999
<strong>Explanation:</strong> Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,2,4,8,16], target = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>0 &lt;= target &lt;= 10<sup>7</sup></code></li>
</ul>

class SegmentTree {
private:
    vector<int> tree;
    vector<int> arr;
    int n;

    void build(int node, int start, int end) {
        if (start == end) {
            // Leaf node will have a single element
            tree[node] = arr[start];
        } else {
            int mid = (start + end) / 2;
            // Recursively build the segment tree
            build(2 * node + 1, start, mid);
            build(2 * node + 2, mid + 1, end);
            // Internal node will have the AND of the two children
            tree[node] = tree[2 * node + 1] & tree[2 * node + 2];
        }
    }

    void update(int node, int start, int end, int idx, int value) {
        if (start == end) {
            // Leaf node
            arr[idx] = value;
            tree[node] = value;
        } else {
            int mid = (start + end) / 2;
            if (start <= idx && idx <= mid) {
                // If idx is in the left child, recurse on the left child
                update(2 * node + 1, start, mid, idx, value);
            } else {
                // If idx is in the right child, recurse on the right child
                update(2 * node + 2, mid + 1, end, idx, value);
            }
            // Internal node will have the AND of the two children
            tree[node] = tree[2 * node + 1] & tree[2 * node + 2];
        }
    }

    int query(int node, int start, int end, int L, int R) {
        if (R < start || end < L) {
            // range represented by a node is completely outside the given range
            return INT_MAX; // Identity for AND operation
        }
        if (L <= start && end <= R) {
            // range represented by a node is completely inside the given range
            return tree[node];
        }
        // range represented by a node is partially inside and partially outside the given range
        int mid = (start + end) / 2;
        int left = query(2 * node + 1, start, mid, L, R);
        int right = query(2 * node + 2, mid + 1, end, L, R);
        return left & right;
    }

public:
    SegmentTree(const vector<int>& inputArr) {
        arr = inputArr;
        n = arr.size();
        int height = (int)(ceil(log2(n)));
        int maxSize = 2 * (int)pow(2, height) - 1;
        tree.resize(maxSize, 0);
        build(0, 0, n - 1);
    }

    void update(int idx, int value) {
        update(0, 0, n - 1, idx, value);
    }

    int query(int L, int R) {
        return query(0, 0, n - 1, L, R);
    }
};
class Solution {
public:
    int closestToTarget(vector<int>& arr, int target) {
        SegmentTree sg(arr);
        int n=arr.size(),ans=1e9;
        for(int i=0;i<n;i++){
            // nearst large to target and nearst small to target
            int l=0,r=i,mid,mxLeft=i,mxRight=i-1;
            while(l<=r){ // mxLeft
                mid=(l+r)/2;
                int op=sg.query(mid,i);
                ans=min(ans,abs(target-op));
                if(op<target) l=mid+1;
                else if(op>target) r=mid-1;
                else {
                    mxLeft=mid;
                    r=mid-1;
                }
            }
            l=0,r=i;
            while(l<=r){
                mid=(l+r)/2;
                int op=sg.query(mid,i);
                ans=min(ans,abs(target-op));
                if(op<target) l=mid+1;
                else if(op>target) r=mid-1;
                else {
                    mxRight=mid;
                    l=mid+1;
                }
            }
        }
        return ans;
    }
};
75500