int pos[M];// it stores the index of last occurence of i(number);
int countt[N];// it denotes the number of distinct elements in the array from 0 to i(0 based indexing);
int node[4*N+5];// segment tree;
int sum(int i , int j , int l , int r ,int index)
{
if(j<l||i>r)
{
return0;
}
if(i>=l&&j<=r)
{
return node[index];
}
int mid=(i+j)/2;
int left=sum(i,mid,l,r,2*index);
int right=sum(mid+1,j,l,r,2*index+1);
node[index]=left+right;
return node[index];
}
void build(int i, int j , int index)
{
if(i==j)
{
node[index]=countt[i];
return;
}
int mid=(i+j)/2;
build(i,mid,2*index);
build(mid+1,j,2*index+1);
node[index]=node[2*index]+node[2*index+1];
return;
}
int main ()
{
int n;
cin>> n ;
memset(pos,-1,sizeof(pos));// it is denoting that for all are input values, the index of previous occurence of them is not yet calculated.
memset(node,0,sizeof(node));
memset(countt,0,sizeof(countt));
for(int i=0;i<n;i++)
{
cin>> arr[i];
}
// Taking in queries
int m ;
cin>> m;
vector<int>v[n+1];// for storing the queries ending with r ;
map<pair<int,int>,int>mp;//Just for having a track of queries;
for(int i=0;i<m;i++)
{
int l , r ;
cin>> l >> r;
l--,r--;
v[r].push_back(l);
pair<int,int>temp=make_pair(l,r);
mp[temp]=i;
}
// a query is denoted by (l,r)
// Now we are treating each i as r of a query;
int s[m+5];
for(int i=0;i<n;i++)
{
if(pos[arr[i]]!=-1)// This means that if there is any previous occurence of arr[i] till ith index,we need to remove that in order to calculate
{// distinct elements that is why we have stored the index of that previous occurence in pos[arr[i] , now for that position
countt[pos[arr[i]]]--;// we will decrement its countt[pos[arr[i]];
}
pos[arr[i]]=i;// Now once we have removed that previous occurence of arr[i], now we also need to include it again as is in the range till i(for which we are
countt[pos[arr[i]]]++;// calculating for now) , hence our new position of arr[i] will become i only;
//Now for all queries ending with i(as their r), we need to calculate our sum from l to r for which we can use segment tree;