裸的模板。
/* Footprints In The Blood Soaked Snow */ #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 50005, maxm = maxn, maxsqrtn = 224; int n, m, num[maxn], cnt[maxn]; LL ans[maxm], deno[maxm]; struct _query { int id, l, r; } q[maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline bool cmp(_query a, _query b) { return a.l / maxsqrtn < b.l / maxsqrtn || (a.l / maxsqrtn == b.l / maxsqrtn && a.r < b.r); } inline LL gcd(LL a, LL b) { for(; b; b ^= a ^= b ^= a %= b); return a; } int main() { n = iread(); m = iread(); for(int i = 1; i <= n; i++) num[i] = iread(); for(int i = 1; i <= m; i++) { int l = iread(), r = iread(); q[i] = (_query){i, l, r}; deno[i] = (LL)(q[i].r - q[i].l + 1) * (q[i].r - q[i].l); } sort(q + 1, q + 1 + m, cmp); LL tmp = 0; int l = 1, r = 0; for(int i = 1; i <= m; i++) { while(q[i].l < l) { l--; tmp += cnt[num[l]] << 1LL; cnt[num[l]]++; } while(l < q[i].l) { cnt[num[l]]--; tmp -= cnt[num[l]] << 1LL; l++; } while(q[i].r < r) { cnt[num[r]]--; tmp -= cnt[num[r]] << 1LL; r--; } while(r < q[i].r) { r++; tmp += cnt[num[r]] << 1LL; cnt[num[r]]++; } ans[q[i].id] = tmp; } for(int i = 1; i <= m; i++) { LL div = gcd(ans[i], deno[i]); printf("%lld/%lld\n", ans[i] / div, deno[i] / div); } return 0; }