Number of distinct value segments

Given n segments, each segment are described by three numbers l_i, r_i and v_i. We want to make some queries:

  1. Change v_i for some segment i for some number x, so from now v_i = x.
  2. How many distinct values v_i are written on segments, such that l_i \leq x \leq r_i for some x. We know that for some segment i we have l_i = x.

We have all queries given so maybe we can answer offline?

I suspect segment trees or persistent segment trees may be useful, maybe sqrt decomposition.