×

# RECTQUER - Editorial

Author: Roman Rubanenko
Tester: Gerald Agapov
Editorialist: Jingbo Shang

Easy

Prefix sum

# PROBLEM:

Given a N*N matrix of at most 10 different numbers, answer Q queries about how many distinct numbers are there in a given sub matrix.

# EXPLANATION:

It is worth noting that there are at most 10 different numbers. Assume they are 1, 2, 3, ... , 10. To answer the number of distinct numbers, we can divide this problem to 10 separate problems:

for d = 1 to 10:
Is there any d in the sub matrix?


Let’s focus on a given number d. Then the matrix can be treated as binary, i.e. whether the entry equals d. Do the prefix sum for the binary matrix:

S[i][j] = S[i-1][j] + S[i][j-1] – S[i-1][j-1] + Matrix[i][j]


With this O(N^2) preprocess, we can answer the problem “Is there any d in the sub matrix?” in O(1) time. That is,

# of number d in (x1,y1)-(x2,y2) = S[x2][y2]–S[x2][y1-1]–S[x1-1][y2]+S[x1-1][y1-1]


Also, you can see the following figure for visualization. Denote the sum of red region as R, similar to Y(ellow), G(ray), B(lue).

Then we can have

S[x2][y2] = R + B + G + Y
S[x2][y1-1] = G + B
S[x1-1][y2] = G + Y;
S[x1-1][y1-1] = G
Our goal is R.


Using this technique, it is easy to solve this problem in O(N^2 + Q * D). D is the different numbers in the matrix.

# AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

161446173
accept rate: 0%

4★rsaha77
9653815

 0 I used 2-D BIT, i would like to know any faster methods answered 16 Dec '13, 19:28 826●6●12●24 accept rate: 18% 1 @yashkumar18 i thought of implementing using segment trees and it actually worked for larger inputs also, http://www.codechef.com/viewsolution/3069222 (tle). so i thought of another method and came up with an algo similar to the above editorial, http://www.codechef.com/viewplaintext/3095118. Method mentioned in the above editorial is better as you can see here, http://www.codechef.com/DEC13/status/RECTQUER,sudharkj. I thought of implementing using bit also. But, i was not able to code. @shangjingbo and @yashkumar18 could any one tell why segment tree method gave tle, but not for bit? Thanks. (16 Dec '13, 20:50) sudharkj3★ i think segment tree should give a faster solution but i prefer BIT, its much simpler, but i am not sure why segment tree would give tle (16 Dec '13, 21:31) may be wrong implementation of segment trees because someone else sg1993 was able to do it. Anyhow i liked the method in this editorial. (17 Dec '13, 10:21) sudharkj3★ my segment tree works well. (17 Dec '13, 14:55) muttakyn3★
 0 I used 3D matrix and DP strategy :) As it is given that only 10 different colours are used , we can use hashing here #include #include #include using namespace std; int main(){ int buff; int a[301][301]; int dp[301][301][10]; for(int i = 0 ; i <= 300 ; i++){ for(int j = 0 ; j <= 300 ; j++){ for(int k = 1 ; k <= 10 ; k++){ dp[i][j][k] = 0; }}} int n,q; cin>>n; for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ cin>>a[i][j]; dp[i][j][a[i][j]] = 1; }} for(int i = 1 ; i <= n ; i++){ int hash[11] = {0}; for(int j = 1 ; j <= n ; j++){ hash[a[i][j]]++; for(int k = 1 ; k <= 10 ; k++){ dp[i][j][k] = hash[k]; }}} cin>>q; int x1,x2,y1,y2; while(q--){ cin>>x1>>y1>>x2>>y2; if( x1 == x2 && y1 == y2) cout<<1< 0 && hash[j] == 0) { hash[j] = 1; count++ ; } }} cout<
 0 Complexity of my solution is: PreProcessing O(N^2) + Q*Nlog(N). I didn't understand this editorial and would like to know some better algorithms for this kind of problems. answered 16 Dec '13, 20:49 227●3●6●9 accept rate: 0% 1 The idea is to maintain 10 different 2-D prefix sum. (16 Dec '13, 20:59)
 0 I used 2-d segment tree for each element 1-10. answered 17 Dec '13, 08:33 2★sg1993 46●1●2●4 accept rate: 0%
 0 I solved it using binary search. but it took a bit time, 0.82 answered 17 Dec '13, 13:01 16●1●4 accept rate: 0% how to solve it with binary search? (17 Dec '13, 16:12) muttakyn3★ ^^ please tell how to solve with binary search? (20 Dec '13, 14:12)
 0 Do the prefix sum for the binary matrix: What does this mean? answered 17 Dec '13, 21:44 5★yash_15 518●1●7●16 accept rate: 2% http://discuss.codechef.com/questions/32038/prefix-sumhelp (17 Dec '13, 21:49)
 0 Hi could anyone please help me to find error in my solution for this problem RECTQUER. link to solution: http://www.codechef.com/viewsolution/3066953 Algorithm: Instead of 2D array, I have taken n * n one dimensional array. now one 2D temp array[n*n][10] (it will store cumulative count of all numbers till ith position). now to get the number of distinct element from (x1,y1) to (x2,y2) , I am converting these index for one dimensional array so l=x1 * n+y1 and r=x2 * n+y2. then take the difference array[r][10]-array[i][10]. I have also taken care of boundary conditions and everything. please run this program in C++11 for given sample input or user generated input and point out why it is failing...please Help!!!!!!!!!! answered 18 Dec '13, 15:40 1 accept rate: 0%
 0 I am having problem in understanding the editorial solution. For the matrix given in the question i.e. {{1, 2, 3}, {3, 2, 1}, {5, 6, 3}} the prefix sum matrix would be {{1, 3, 6}, {4, 8, 12}, {9, 19, 26}}?? If my prefix sum matrix is correct then the answer to the query (2,2) - (3,3) would be s[3][3] - s[3][1] -s[1][3] + s[1][1] = 26-9-6+1 => 12. How come it is 12? when the answer should be 4. Please tell me where I am wrong? answered 28 Dec '13, 19:42 2★pawan68 1●1●1 accept rate: 0% Nope, here S[][], is a prefix matrix of binary matrix for a given number.d. Please read the editorial step by step. We first divide the original problem into D parts (D is the number of different numbers in the original matrix, at most 10). (08 Jan '14, 19:43)
 0 Someone please help me find why all my solution to this problem throw verdict "SIGSEGV" :'( I used the same logic as mentioned in the editorial. http://www.codechef.com/DEC13/status/RECTQUER,yoyosonu answered 29 Dec '13, 14:06 3★yoyosonu 31●1●1●4 accept rate: 0%

# of number d in (x1,y1)-(x2,y2) = S[x2][y2]–S[x2][y1-1]–S[x1-1][y2]+S[x1-1][y1-1]

can someone explain this??

3★vipulk10
1
accept rate: 0%

You may look at the figure. It is based on inclusion-exclusion principle actually. But it is straightforward in the picture.

(08 Jan '14, 19:42)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×14,070
×3,056
×142
×16

question asked: 16 Dec '13, 18:51

question was seen: 5,115 times

last updated: 08 Jan '14, 19:43