TLE in RECTQUER

why my code for RECTQUER is giving TLE while same algorithm in c++ is executing in 0.98 sec??

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class rectq{
        static int mat[][][]=new int [301][301][11];
public static void main (String args[])throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n,r,c,k;
n=Integer.parseInt(in.readLine());
String str;
  String REGEX = " ";

for(r=1;r<=n;r++)
{
    str=in.readLine();
    Pattern pp = Pattern.compile(REGEX);
    String[] item = pp.split(str);
    for(c=1;c<=n;c++)
    {   mat[r][c][0]=Integer.parseInt(item[c-1]);
        mat[r][c][mat[r][c][0]]=mat[r][c-1][mat[r][c][0]]+1;
        for(k=1;k<=10;k++)
        {  if(k!=mat[r][c][0])
           mat[r][c][k]=mat[r][c-1][k];
        }
    }
}
int q;
q=Integer.parseInt(in.readLine());
String str1;
while(q>0){
 str1=in.readLine();
int x1,y1,x2,y2,count=0;
Pattern p = Pattern.compile(REGEX);
 String[] item = p.split(str1);
int check[]=new int[11];
x1=Integer.parseInt(item[0]);
y1=Integer.parseInt(item[1]);
 x2=Integer.parseInt(item[2]);
 y2=Integer.parseInt(item[3]);
for(r=x1;r<=x2;r++)
{ 
    for(k=1;k<=10;k++)
        {
            if(mat[r][y2][k]-mat[r][y1-1][k]>0)
            {   if(check[k]==0)
                {
                check[k]=1; 
                count++;}
                if(count==10)
                break;
            }
        }
        if(count==10)
        break;
    }
    System.out.println(count);
    q--;
}
}
}

maybe because JAVA is a bit slow…though it gets 2x timelimit time, the timelimit maybe a bit too strict for the algo…causing a tle due to time wasted in I/O operations…hope this helps…:slight_smile:

@kumar_ranjan : many java users on codechef use the following i/o method which is faster compared to others.

Try the following link for fast i/o. http://discuss.codechef.com/questions/7394/help-on-fast-inputoutput

if u r a java programmer can u tell me any faster I/o method than what i hv used…

i do code in java…but i’m more into cpp…i can tell u abt fast io in cpp…for JAVA u can see some of the top JAVA submissions for the problem…INTEST…http://www.codechef.com/status/INTEST?language=10&status=15&handle=&sort_by=Time&sorting_order=asc …hope this helps…:slight_smile:

also u can see this code of @vineetpaliwalhttp://www.codechef.com/viewsolution/3054206 …it is a JAVA solution for this problem…hope this helps…also u can refer to @vineetpaliwal’s codes…as he codes in JAVA only…:slight_smile: