Offline practices: Mo team.
Offline processing of queries, the left end of the block, in the same piece of the right end of the order from small to large.
Then, for the previous query [L,r] and today's inquiry [l ', R '] constantly adjust the range of intervals.
How to adjust it? For the information contained in [L,r], we can adjust the time of O (1) to [L,r+1],[l,r-1],[l+1,r],[l-1,r].
The most authentic Mo team seems to be the point set Manhattan distance plus the smallest spanning tree, very troublesome ... So he was written as a sub-team.
#include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm > #include <fstream> #include <iostream> #include <queue> #define REP (i, L, R) for (int i = l; I <= R; i++) #define DOWN (i, L, R) for (int i = l; I >= R; i--) #define MAXN 56789#define MAX 1<<30#define ll Long longusing Namespace Std;int N, M, C[maxn], pos[maxn];ll ans, K, s[maxn];struct node {int L, r, id; ll a, B;} Q[maxn];bool CMP (node A, Node B) {if (pos[a.l] = = POS[B.L]) return A.R < B.r;return a.l < B.L;} BOOL cmp_id (Node A, Node B) {return a.id < b.id;} ll GCD (ll A,ll b) {return b==0?a:gcd (b,a%b);} ll Sqr (ll x) {return x*x;} void Update (int p, int x) {ans-= SQR (S[c[p]]); s[c[p] [+ X;ans + = SQR (S[c[p]]);} int main () {scanf ("%d%d", &n, &m), Rep (i, 1, N) scanf ("%d", &c[i]), int block = int (sqrt (n)); Rep (i, 1, n) pos[i] = (i-1)/block+1;rep (i, 1, m) scanf ("%d%d", &Q[I].L, &Q[I].R); Rep (I, 1, m) q[i].id = I;sort (q+1, Q+1+M, CMP);int L = 1, R = 0;rep (i, 1, m) {while (R < Q[I].R) Update (++R, 1), while (R-> Q[I].R) Update (R--,-1), while (L < q[i ].L) Update (l++,-1), while (L > Q[i].l) Update (--L, 1), if (q[i].l = = Q[I].R) q[i].a = 0, q[i].b = 1;else {q[i].a = ans- (q[i].r-q[i].l+1); q[i].b = (ll) (q[i].r-q[i].l+1) * (Q[I].R-Q[I].L); k = gcd (q[i].a, q[i].b); q[i].a/= K; Q[i].b/= k;}} Sort (q+1, q+1+m, cmp_id); Rep (I, 1, m) printf ("%lld/%lld\n", q[i].a, q[i].b); return 0;}
BZOJ-2038 Little Z's socks