-3

I saw this coding question on an online coding competition but I couldn't find the most optimal solution. Here is the question:
"You are given an array A of N integers and Q queries.Each query is of the following type :
1 pos val: Update the element at index pos to val
2 pos : Find the smallest index i less than or equal to pos such that all elements between i and pos are same."

I somehow believe that we can use segment tree but I could not figure out what each index of the segment tree will represent.

1 Answers1

0

Here`s O(N + Q log^2 N) approach:

  1. Build a segment tree for given array
  2. In each segment tree node store minimum number of interval and maximum number in interval
  3. Now queries of type 1 can be done by simple update on segmenent tree
  4. For queries of type 2 we can use binary search to find smallest i such that minimum value in range[i, pos] is equal to maximum value in same range
Photon
  • 2,717
  • 1
  • 18
  • 22