Distinct element in range using segment tree getting tle PLS HELP

#include <bits/stdc++.h>
using namespace std;
// distinct vlaues in a range

void built(long long int ind,long long int low,long long int high,long long int a[],setseg[])
{
if(low==high)
{
seg[ind].insert(a[low]);
return ;
}
long long int mid=(low+high)/2;
built(2ind+1,low,mid,a,seg);
built(2
ind+2,mid+1,high,a,seg);
seg[ind].insert(seg[2ind+1].begin(),seg[2ind+1].end());
seg[ind].insert(seg[2ind+2].begin(),seg[2ind+2].end());

}
set query(long long int ind,long long int low,long long int high,long long int l,long long int h,setseg[])
{
if(l>high || h<low)
{
sets;
return s;

}
if(l<=low && h>=high)
{
return seg[ind];
}
long long int mid=(low+high)/2;
sets1 =query(2ind+1,low,mid,l,h,seg);
sets2=query(2
ind+2,mid+1,high,l,h,seg);
sets3;
s3.insert(s1.begin(),s1.end());
s3.insert(s2.begin(),s2.end());
return s3;

}

int main()
{
long long int n,q;
cin>>n>>q;
long long int a[n];
for(long long int i=0;i<n;i++)
{
cin>>a[i];
}
setseg[4*n];
built(0,0,n-1,a,seg);

while(q–)
{
long long int l,r;
cin>>l>>r;
l–;
r–;
cout<<query(0,0,n-1,l,r,seg).size()<<endl;

}
return 0;
}

It’s not optimal solution so TLE is understandable =.=