#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50010;
int n , q;
int c[N] , cnt[N];
ll ans , ans2[N] , res[N];
ll gcd (ll a , ll b) {
return b ? gcd (b , a % b) : a;
}
array<int , 3> que[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL) , cout.tie(NULL);
cin >> n >> q;
// scanf ("%d%d" , &n , &q);
for (int i = 1; i <= n; ++ i) {
cin >> c[i];
}
for (int i = 0; i < q; ++ i) {
int l , r;
cin >> l >> r;
// scanf ("%d%d" , &l , &r);
que[i] = {l , r , i};
ans2[i] = (ll)(r - l) * (r - l + 1) / 2;
}
int B = 500;
sort (que , que + q , [&](array<int , 3> a , array <int , 3> b) {
if (a[0] / B != b[0] / B) return a[0] / B < b[0] / B;
return a[1] < b[1];
});
int l = 1 , r = 0;
auto add = [&] (int x) {
ans += cnt[c[x]];
cnt[c[x]] ++;
};
auto del = [&] (int x) {
cnt[c[x]] --;
ans -= cnt[c[x]];
};
for (int i = 0; i < q; ++ i) {
while (r < que[i][1]) {
r++;
add (r);
}
while (l > que[i][0]) {
l --;
add (l);
}
while (r > que[i][1]) {
del (r);
r--;
}
while (l < que[i][0]) {
del (l);
l ++;
}
res[que[i][2]] = ans;
}
for (int i = 0; i < q; ++ i) {
ll d = gcd (res[i] , ans2[i]);
if (que[i][1] == que[i][0]/*这里*/) {
cout << "0/1" << endl;
continue;
}
if (d == 0) d = 1;
cout << res[i] / d << '/' << ans2[i] / d << endl;
// printf("%lld/%lld\n", res[i] / d , ans2[i] / d);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50010;
int n , q;
int c[N] , cnt[N];
ll ans , ans2[N] , res[N];
int tl[N] , tr[N];
ll gcd (ll a , ll b) {
return b ? gcd (b , a % b) : a;
}
array<int , 3> que[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL) , cout.tie(NULL);
cin >> n >> q;
// scanf ("%d%d" , &n , &q);
for (int i = 1; i <= n; ++ i) {
cin >> c[i];
}
for (int i = 0; i < q; ++ i) {
int l , r;
cin >> l >> r;
// scanf ("%d%d" , &l , &r);
que[i] = {l , r , i};
tl[i] = l , tr[i] = r;
ans2[i] = (ll)(r - l) * (r - l + 1) / 2;
}
int B = 500;
sort (que , que + q , [&](array<int , 3> a , array <int , 3> b) {
if (a[0] / B != b[0] / B) return a[0] / B < b[0] / B;
return a[1] < b[1];
});
int l = 1 , r = 0;
auto add = [&] (int x) {
ans += cnt[c[x]];
cnt[c[x]] ++;
};
auto del = [&] (int x) {
cnt[c[x]] --;
ans -= cnt[c[x]];
};
for (int i = 0; i < q; ++ i) {
while (r < que[i][1]) {
r++;
add (r);
}
while (l > que[i][0]) {
l --;
add (l);
}
while (r > que[i][1]) {
del (r);
r--;
}
while (l < que[i][0]) {
del (l);
l ++;
}
res[que[i][2]] = ans;
}
for (int i = 0; i < q; ++ i) {
ll d = gcd (res[i] , ans2[i]);
if (tl[i] == tr[i]/*这里*/) {
cout << "0/1" << endl;
continue;
}
if (d == 0) d = 1;
cout << res[i] / d << '/' << ans2[i] / d << endl;
// printf("%lld/%lld\n", res[i] / d , ans2[i] / d);
}
return 0;
}
两份代码就只有特判 l,r 相等的地方不一样。