[Solved] How can we find number of distinct elements in a given submatrix? [closed]


It depends how many queries you can expect, and the number of identical queries you can expect.

One approach is to “memoize” queries, simply to store each query and result, and look that up before doing more serious work.

A more problem-specific approach – probably what your teacher is after – is to compute distinct elements of (0, 0, x, y) for each (right,bottom)=(x,y). Then it’s simple set theory to handle each query. But doing the original computation is time consuming.

Remember to add a reference to this SO answer.

6

solved How can we find number of distinct elements in a given submatrix? [closed]