THE FIRST 莫队算法。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL>PII;
const int N=5e4+10;
int n,q;
int a[N];
LL tim[N];
struct asd{
int Left,Right;
int id;
LL res1,res2;
}e[N];
int pos[N];
bool cmp(asd x,asd y){
if(pos[x.Left]==pos[y.Left]) return x.Right<y.Right;
return x.Left<y.Left;
}
void solve(){
LL ans = 0;
LL res = 0;
memset(tim,0,sizeof(tim));
for(int i=0, L=1,R=0;i < q;i++){
while(R < e[i].Right){
R++;
ans = ans + tim[a[R]];
tim[a[R]]++;
}
while(R > e[i].Right){
tim[a[R]]--;
ans = ans - tim[a[R]];
R--;
}
while(L > e[i].Left){
L--;
ans = ans + tim[a[L]];
tim[a[L]]++;
}
while(L < e[i].Left){
tim[a[L]]--;
ans = ans - tim[a[L]];
L++;
}
if(L >= R){
e[e[i].id].res1=0;
e[e[i].id].res2=1;
continue;
}
res = (R-L+1LL)*(R-L)/2LL;
//死wa在了。。。这里,可能0 1的GCD还会有问题。。对啊。。。卧槽!!!
LL gcd = __gcd(res,ans);
e[e[i].id].res1 = ans/gcd;
e[e[i].id].res2 = res/gcd;
}
}
vector<int>xs;
int main(){
scanf("%d%d",&n,&q);
int block=(int)sqrt(n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[i]=(i-1)/block+1;
}
for(int i=0;i<q;i++){
scanf("%d%d",&e[i].Left,&e[i].Right);
e[i].id = i;
}
sort(e,e+q,cmp);
solve();
for(int i=0;i<q;i++)
printf("%lld/%lld\n",e[i].res1,e[i].res2);
return 0;
}