1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int N = 5e4 + 10; typedef long long LL; int n, m, len, c[N], cnt[N]; LL sum, ans1[N], ans2[N]; struct query { int l, r, id; bool operator<(const query &x) const { if (l / len != x.l / len) return l < x.l; return (l / len) & 1 ? r < x.r : r > x.r; } } q[N]; void add(int i) { sum += cnt[i]; cnt[i]++; } void del(int i) { cnt[i]--; sum -= cnt[i]; } LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } int main() { scanf("%d%d", &n, &m); len = sqrt(n); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); for (int i = 0; i < m; i++) { scanf("%d%d", &q[i].l, &q[i].r); q[i].id = i; } sort(q, q + m); for (int i = 0, l = 1, r = 0; i < m; i++) { if (q[i].l == q[i].r) { ans1[q[i].id] = 0; ans2[q[i].id] = 1; continue; } while (l > q[i].l) add(c[--l]); while (r < q[i].r) add(c[++r]); while (l < q[i].l) del(c[l++]); while (r > q[i].r) del(c[r--]); ans1[q[i].id] = sum; ans2[q[i].id] = (LL)(r - l + 1) * (r - l) / 2; } for (int i = 0; i < m; i++) { if (ans1[i]) { LL g = gcd(ans1[i], ans2[i]); ans1[i] /= g, ans2[i] /= g; } else ans2[i] = 1; printf("%lld/%lld\n", ans1[i], ans2[i]); } return 0; }
|