getting TLE in HAYBALE http://www.spoj.com/problems/HAYBALE/

I am using BIT (range update and point query) for this question, for each input(index1,index2) i call update function. Then for i-> 1 to n, i am running read funtion to get the element at index i and store it in array and then sort it and print the middle element of the array.For fast input/output i am using getchar(). But after all of this i am getting TLE. So please suggest me some advice.

long long int finput()
{
char c=getchar();

while(c <'0' || c> '9') c=getchar();

long long int ret=0;

while(c>='0' && c<='9')
{
    ret=10*ret + c -48;
    c=getchar();
}
return ret;

}

long long int foutput(long long int n)
{
if(n==0)
{
putchar(‘0’);

}
else{
    if(n < 0){
        putchar('-');
        n*=-1;
    }
    char temp[64];
    int sze=0,i;
    while(n > 0){
        temp[sze++]= '0' + n%10;
        n/=10;
    }
    for(i=sze-1;i>=0;i--)
    {
        putchar(temp[i]);
    }
}

}

long long int read(long long int idx)
{
long long int sum=0;

while(idx > 0) {

	sum+=tree[idx];
	idx-=(idx & -idx);
}

return sum;

}

void update(long long int idx,int val)
{
while(idx <=maxval) {

	tree[idx]+=val;
	idx+=(idx & -idx);
}

}