Problem:
input:square matrix of order n and a query which will denote the submatrix.(x1,y1,x2,y2)
output:no of distinct elements in this submatrix.
constraint:time limit=1 sec
this is what i tried
#include<stdio.h>
//#include<conio.h>
int main()
{
//clrscr();
int a[300][300],test[100000],count[10],m,n,c,j,p,q,r,s;
long qu,re,i;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
scanf("%ld",&qu);
for(re=0;re<qu;re++)
{
c=0;
for(i=0;i<10;i++)
count[i]=0;
scanf("%d %d %d %d",&p,&q,&r,&s);
for(i=(p-1);i<r;i++)
for(j=(q-1);j<s;j++)
{
m=a[i][j];
count[--m]++;
}
for(i=0;i<10;i++)
if(count[i]!=0)
c++;
test[re]=c;
}
for(i=0;i<qu;i++)
printf("%d\n",test[i]);
//getch();
return 0;
}
but got a TLE(time limit exceeded) error.
It has to do something with cumulative frequency of each number.
Guys i have been working for over 3 days now on this problem but still didn't get it.
Please suggest me an approach to tackle this.