Is there any way to find frequency of a number in a given range using segment tree? -
suppose, i've array [1, 2, 3, 5, 5, 6, 5, 5]
now, there can 2 kind of operation. 1 of them "update" operation , increase value in index 4[1 based indexing] x. other operation "query" operation, given range, suppose [4, 8](both inclusive) , value suppose 5. find frequency of value 5 in given range [4, 8]. in case, answer should 4.
how can "query" step using segment tree??
thanks in advance.
i have 2 solutions without using segment tree
first solution :
split each query
freq(r,x)-freq(l-1,x)
iterate on input , store counts in array (or map if
range big)if there query on position, add/subtract count array should work in o(n + q) if elements small enough
array or o(n + q log n) if need use map.
second solution : use mo's algorithm solving problem time complexity o((n + q) * sqrt(n) * f).
Comments
Post a Comment