SPOJ : Unknown SIGSEV on DQUERY

The Following Code Gets a SIGSEV on SPOJ for PRoblem DQuery

Given a sequence of n numbers a1, a2, …, an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, …, aj.

Input

Line 1: n (1 ≤ n ≤ 30000).
Line 2: n numbers a1, a2, …, an (1 ≤ ai ≤ 106).
Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
Output

For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, …, aj in a single line.
Example

Input
5
1 1 2 1 3
3
1 5
2 4
3 5

Output
3
2
3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000001
typedef long long int ll;
ll b[MAX];
ll arr[MAX];
ll read(ll n)
{
    ll sum=0;
    for(;n;sum+=b[n],n-=n&-n);
    return sum;
}
void update(ll n , ll val)
{
    for(;n<MAX;b[n]+=val,n+=n&-n);
}
void elim(ll n)
{
    ll val = read(arr[n])-read(arr[n]-1);
    for(;n<MAX;b[n]=b[n]-val,n+=n&-n);
}
void query(ll i,ll j,ll tot)
{
    memset(b,0,sizeof b);
    ll count=0;
    for(ll k=i;k<=j;k++)
    {
        update(arr[k],1);
    }
   for(ll k=i;k<=j;k++)
    {
        if((read(arr[k])-read(arr[k]-1))>0){count++;
        elim(arr[k]);}
    }
    printf("%lld \n",count);
}
void process()
{
    memset(arr,0,sizeof arr);
    ll n,temp=0;
    scanf("%lld",&n);
    ll k=0;
    while(n--)
    {
        scanf("%lld",&temp);
        arr[++k]=temp;
    }
    ll q;
    scanf("%lld",&q);
    while(q--)
    {
        ll i, j;
        scanf("%lld %lld",&i,&j);
        query(i,j,k);
    }
}
int main()
{
    process();
    return 0;
}

void elim(ll n)
{
ll val = read(n)-read(n-1); /*This Line Had to be Changed */
for(;n<MAX;b[n]=b[n]-val,n+=n&-n);
}

The Modified Elimination Function Above Corrected The Problem