title: Find closest number to P in a given range of array
body: <p>An array A is given to you containing N integers. You need to operate Q queries on the array. Queries are of two types.</p>
  
  <p>1 U P: You need to update value of A<sub>u</sub> to P.</p>
  
  <p>2 L R P: You need to find A<sub>k</sub> such that  A<sub>k</sub> - P is minimum and  A<sub>k</sub> > P and L&lt;=k&lt;=R</p>
  
  <p><strong>Input Format:</strong></p>
  
  <p>First Line of input contains a single integer N. Next line contains N space-separated integers, elements of array A.</p>
  
  <p>Next line of input contains a single inter Q.</p>
  
  <p>Q lines follow each containing a query as per described in statement. </p>
  
  <p><strong>Output Format:</strong> </p>
  
  <p>For query of type 2,you need to print the value of A<sub>k</sub>.if there is no such K print -1.Print answer for each query in new line. </p>
  
  <pre><code>Example Input:                               Example Output:
  5                                            2
  3 2 1 1 5                                    -1
  3
  2 1 5 1
  1 4 4
  2 1 4 5
  </code></pre>
  
  <p>Explanation: For first query in the range [1,5] and P=1, the required A<sub>k</sub> is 2.</p>
  
  <p>I am thinking of a segment tree solution with O(log(N)) for query type 2.But not able to figure out how to do.  </p>