模板题qwq
不过不会数学,,, 看似是概率题 其实就是统计区间
[L,R]
[
L
,
R
]
,内部每个数字出现次数的平方
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+10;
struct node {
int l, r, id;
}q[MAXN];
int sz, n, m;
int cnt[MAXN*20], arr[MAXN];
ll res = 0, ans[MAXN];
ll a[MAXN], b[MAXN];
ll gcd(ll a, ll b) {
if(b == 0)
return a;
return gcd(b, a%b);
}
ll cmp(const node &a, const node &b) {
if(a.l/sz == b.l/sz) {
return a.r < b.r;
}
return a.l < b.l;
}
void add(int x) {
res -= (ll)cnt[arr[x]]*cnt[arr[x]];
cnt[arr[x]]++;
res += (ll)cnt[arr[x]]*cnt[arr[x]];
}
void del(int x) {
res -= cnt[arr[x]]*cnt[arr[x]];
cnt[arr[x]]--;
res += cnt[arr[x]]*cnt[arr[x]];
}
int main() {
scanf("%d%d",&n,&m);
sz = (ll)sqrt(n);
for(int i = 1; i <= n; ++i)
scanf("%d",&arr[i]);
for(int i = 1; i <= m; ++i) {
scanf("%d%d",&q[i].l, &q[i].r);
q[i].id = i;
}
sort(q+1, q+1+m, cmp);
ll L = 1, R = 0;
for(int i = 1; i <= m; ++i) {
while(L < q[i].l) {
del(L++);
}
while(L > q[i].l) {
add(--L);
}
while(R < q[i].r) {
add(++R);
}
while(R > q[i].r) {
del(R--);
}
int id = q[i].id;
a[id] = res-(R-L+1);
b[id] = ll(R-L+1)*(R-L);
int k = gcd(a[id], b[id]);
a[id] /= k;
b[id] /= k;
}
for(int i = 1; i <= m; ++i) {
cout << a[i] << "/" << b[i] << endl;
}
return 0;
}