-7

Given a N*N matrix and Q queries for the same given matrix. Each Query is of form x1,y1,x2,y2. We have to find the number of distinct elements in the sub-matrix defined by (x1,y1) and (x2,y2) as top left and bottom right corner respectively. Constraints: N<=300 Q<=10^5 I am using naive approach of iterating over the sub matrix for each query. Is there any better approach?

SuperCoder
  • 121
  • 1
  • 14
  • 2
    What you left off that the last person who posted this question included was that the number of possible entries for each cell was constrained as well. In the last posting, values were between 1 & 10. If you have similar constraints, it makes the problem easier. And yes, with enough pre-computation, you can reduce the problem to O(1). The last person also posted the link to the original question, which is here: [codechef.com](http://www.codechef.com/DEC13/problems/RECTQUER) – woolstar Dec 10 '13 at 06:28
  • This question is from an on-going contest at codechef. From the codechef website: "Discussing CodeChef’s problems or any aspect of problem, on any other platform on web, on identification, could lead to disabling of respective account and banning from the community." – rettvest Dec 10 '13 at 06:51
  • finally I solved it myself. However,thanks to all you die hard followers of "codechef code of conduct". – SuperCoder Dec 10 '13 at 18:56

1 Answers1

1

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • The time limit is 1 second and number of queries as mentioned in the question is 10^5. Each element of matrix is a number in the range 1 to 10 only. So I think neither of this appproaches is sufficient to get accepted. Can you suggest anything related to segment tree? – SuperCoder Dec 10 '13 at 06:32
  • Well the suggestion in the answer above worked nicely (with a little bit of elaboration). However, the example in the Codechef question is inconsistent with the description, apparently he's switchex x and y in the matrix. And apparently the description of possible input values is also wrong. Anyway, I coded up an example and it works locally but, trying the various variations of coordinatae system etc., all variants just produced "Wrong answer" on the site. – Cheers and hth. - Alf Dec 10 '13 at 08:43
  • okay, finally got correct answer but at abstraction cost, run time 0.21 seconds on the server. main info: coordinate system as in the example, with x down and y right. – Cheers and hth. - Alf Dec 10 '13 at 08:53
  • will you please elaborate the approach you took to get correct answer at codechef?Please help. I am not getting you. – SuperCoder Dec 10 '13 at 09:23
  • @SuperCoder: no, I think discussing what the task description *means* is OK, but already with the answer above I told you too much -- I didn't know then that it was CodeChef question with still ongoing competition. – Cheers and hth. - Alf Dec 10 '13 at 09:24
  • Ok.Just tell me whether You used the first approach or second as mentioned in the answer above? Thanks anyway. – SuperCoder Dec 10 '13 at 09:35