简单版本:将区间分成sqrt(n)块并将询问按照左端点的所在块序号排序,左端点块序号一样则按照右端点排序
LL Gcd( LL a , LL b ){ return b==0?a:Gcd( b , a%b ); }
struct Query
{
int L,R,id;
}Q[maxm];
struct Ans
{
LL a,b;
void reduce()
{
LL d = Gcd( a , b );
a /= d;
b /= d;
}
}A[maxm];
int a[maxn],num[maxn],n,m,block;
bool cmp( Query a , Query b )
{
if ( a.L/block!=b.L/block ) return a.L/block<b.L/block;
else return a.R<b.R;
}
void work()
{
LL tmp = 0; memset ( num , 0 , sizeof(num) );
int L = 1,R = 0;
for ( int i=0 ; i<m ; i++ )
{
while ( R<Q[i].R )
{
R++;
tmp -= 1LL*num[a[R]]*num[a[R]];
num[a[R]]++;
tmp += 1LL*num[a[R]]*num[a[R]];
}
while ( R>Q[i].R )
{
tmp -= 1LL*num[a[R]]*num[a[R]];
num[a[R]]--;
tmp += 1LL*num[a[R]]*num[a[R]];
R--;
}
while ( L<Q[i].L )
{
tmp -= 1LL*num[a[L]]*num[a[L]];
num[a[L]]--;
tmp += 1LL*num[a[L]]*num[a[L]];
L++;
}
while ( L>Q[i].L )
{
L--;
tmp -= 1LL*num[a[L]]*num[a[L]];
num[a[L]]++;
tmp += 1LL*num[a[L]]*num[a[L]];
}
A[Q[i].id].a = tmp-(R-L+1);
A[Q[i].id].b = 1LL*(R-L+1)*(R-L);
A[Q[i].id].reduce();
}
}
int main()
{
for ( ; scanf ( "%d%d" , &n , &m )==2 ; )
{
for ( int i=1 ; i<=n ; i++ )
scanf ( "%d" , &a[i] );
for ( int i=0 ; i< m ; i++ )
{
Q[i].id = i;
scanf ( "%d%d" , &Q[i].L , &Q[i].R );
}
block = sqrt(n); sort ( Q , Q+m , cmp ); work();
for ( int i=0 ; i<m ; i++ ) printf ( "%lld/%lld\n" , A[i].a , A[i].b );
}
return 0;
}