Hi @rk_221b, I did them in the following way.

Good ranges:

It requires knowledge on stl.

The thing to be observed is for every distinct query, two numbers will be added to the answer.

We use stl set here.

For each query,

First insert the given number into the set.Now find the position of the inserted number in the set by using stl find().

There will always be these cases after you inserted the number.

Case 1:if the set has one element add n+2 to the answer.

Case 2:if the position of the inserted element is in between any two elements in the set add the insertedvalue-1 and insertedvalue+1 to the answer.

Case 3:If the position of the inserted element is at the beginning in the set, add insertedvalue+1 and the (adjacent value of the inserted element in set)-1 to the answer.

Case 4:If the inserted element is at the last add the insertedvalue-1 and the (adjacent value of the inserted element in set)+1 to the answer.

Time complexity:log n per query

Maximum depth:

It too requires knowledge of stl.

I used vector of sets.

Just do a traversal of the tree and insert each level in vector of sets.

vector<set> v

Level 0 values are inserted into v[0] and similarly.now for each query find the lower_bound(k) for given v[l] where l is the level and k is the value.

Time complexity:log n per query