#include<bits/stdc++.h>
#define ll long long
const int N=5e4+10;
ll bl[N];
ll gcd1[N];
ll cnt[N],res=0;
ll a[N];
struct node{
ll l,r,id;
}q[N];
struct node2{
ll f,s;
}ans[N];
inline void move(ll x,ll k)
{
res-=cnt[a[x]]*cnt[a[x]];
cnt[a[x]]+=k;
res+=cnt[a[x]]*cnt[a[x]];
}
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar_unlocked();
while(!isdigit(ch))
{
if (ch == '-')
f=-1;
ch=getchar_unlocked();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar_unlocked();
}
return x*f;
}
inline void write(int x)
{
if (x<0) putchar('-'),x=-x;
if (x>9) write(x/10);
putchar(x%10+'0');
}
inline bool cmp(node a,node b)
{
if(bl[a.l]==bl[b.l])
return a.r<b.r;
return a.l<b.l;
}
inline int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
ll n,m;
n=read(),m=read();
ll len=n/sqrt(m*2/3);
for(int i=1;i<=n;i++)
{
a[i]=read();
bl[i]=(i-1)/len+1;
}
int nl=1,nr=0;
for(int i=1;i<=m;i++)
{
q[i].l=read(),q[i].r=read(),q[i].id=i;
}
for(int i=1;i<=m;++i)
{
int l=q[i].l,r=q[i].r,id=q[i].id;
while(nl<l) move(nl++,-1);
while(nl>l) move(--nl,1);
while(nr<r) move(++nr,1);
while(nr>r) move(nr--,-1);
if(l==r)
{
ans[id].f=0,ans[id].s=1;
continue;
}
ans[id].s=(r-l)*(r-l+1);
ans[id].f=res-(r-l+1);
gcd1[id]=gcd(ans[id].s,ans[id].f);
}
for(int i=1;i<=m;++i)
{
write(ans[i].f/gcd1[i]);
putchar('/');
write(ans[i].s/gcd1[i]);
puts("");
}
return 0;
}
代码逻辑 没有一点问题 样例也过了 为什么0前三个RE后7个TLE