Why I am getting TLE

1. because of my program does not terminate
OR
2. because of my program cannot process the numerous test cases in time.
Please give me a suggestion, Here is my Code:
Code: Select all
#include <stdio.h>
#include <string.h>
void main()
{
char c[1000001],last,ch;
unsigned long i,j,n,temp;
int cas=0,flag,casflag,newline,k=0;;
while(1)
{
while(1)
{
if(scanf("%c",&ch)==EOF)goto exit;
if(ch=='\n')
{
if(newline==2)goto exit;
else if(newline!=1){ newline++; c[k]='\0'; k=0; break; }
newline++;
}
else { newline=0; c[k]=ch; k++; }
}
if(!strlen(c))break;
scanf("%lu",&n);
casflag=1;
cas++;
while(n)
{
scanf("%lu %lu",&i,&j);
if(i>j){ temp=i; i=j; j=temp; }
if(casflag==1)printf("Case %d:\n",cas);
casflag=0;
flag=0;
last=c[i];
for(temp=i;temp<=j;temp++)if(c[temp]!=last){ flag=1;break; }
if(flag==0)printf("Yes\n");
else printf("No\n");
n--;
}
}
exit:
}

Thank you![/quote]