Title Link: BZOJ-2821
Problem analysis
Because the force is online, so can't use Mo team. You can use chunking to do this.
This is done by dividing the number of n into n/x blocks, each with a block size of X. Pre-F[I][J], which indicates the number of occurrences of the numbers from block I to block J is even.
This complexity is n * (n/x).
Then put the number and position in the structure, according to the number of the first keyword, the position of the second keyword sort. This is to facilitate the subsequent binary search [L, R] in Num appeared several times.
For each query, add the answer to the whole block contained in the middle. Then for each side up to 2x number, processing separately, two points to find their number in the middle of the whole block, to update the answer.
The idea of the update is probably: Look at the current number, the number of occurrences is from odd-even or even-odd, or 0-1, and then choose ++ans or--ans or do nothing.
The complexity of processing the query is n * x * Logn.
Analyze the optimal size of x. The total complexity is (n^2/x) + (NLOGN * x), by the mean inequality, when n^2/x = = Nlogn * x, the total complexity is minimal, so the optimal value of x is x = sqrt (N/LOGN).
Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> using namespace std; const int MAXN = 100000 + 5, MAXBLK = 1300 + 5; inline void Read (int &num) {char c = getchar (); while (C < ' 0 ' | | c > ' 9 ') c = GetChar (); num = C-' 0 '; c = GetChar (); while (c >= ' 0 ' && C <= ' 9 ') {num = num * + C-' 0 '; c = GetChar (); }}} int n, m, Cset, Ans, Blksize, Lastblk;int A[MAXN], FIRST[MAXN], LAST[MAXN], CNT[MAXN], f[maxblk][maxblk], l[maxblk], R[ MAXBLK]; struct es{int Pos, Num; BOOL operator < (const ES &b) Const {if (Num = = B.num) return Pos < B.pos; return Num < B.num; }} E[MAXN]; int Get (int s, int t, int Num) {if (S > t | | s > e[last[num]]. Pos | | T < E[first[num]]. Pos) return 0; int L, R, Mid, PL, PR; L = First[num]; r = Last[num]; while (L <= r) {mid = (L + r) >> 1; If (E[mid]. Pos >= s) {pl = mid; R = mid-1; } else L = mid + 1; } L = First[num]; r = Last[num]; while (L <= r) {mid = (L + r) >> 1; if (E[mid]. Pos <= t) {PR = mid; L = mid + 1; } else R = mid-1; } return PR-PL + 1;} int main () {//init Read (n); Read (Cset); Read (m); for (int i = 1; I <= n; ++i) Read (A[i]); blksize = (int) sqrt ((double) N/log ((double) n) * log (2.0)); LASTBLK = (n-1)/blksize + 1; for (int i = 1; I <= lastblk; ++i) {L[i] = (i-1) * blksize + 1; R[i] = i * blksize; } R[lastblk] = n; memset (CNT, 0, sizeof (CNT)); for (int i = 1, i <= lastblk; ++i) {for (int j = 1; j <= Cset; ++j) cnt[j] = 0; for (int j = i; j <= lastblk; ++j) {f[i][j] = f[i][j-1]; for (int k = l[j]; k <= r[j]; ++k) {++cnt[a[k]]; if ((Cnt[a[k]] & 1)= = 0) ++f[i][j]; else if (cnt[a[k]! = 1)--f[i][j]; }}} for (int i = 1; I <= n; ++i) {e[i]. Pos = i; E[i]. Num = A[i]; } sort (e + 1, e + n + 1); for (int i = 1; I <= n; ++i) {if (First[e[i]. Num] = = 0) first[e[i]. Num] = i; Last[e[i]. Num] = i; }//the array cnt[] 'll be used later. memset (CNT, 0, sizeof (CNT)); Solve queries int L, r, X, Y, Lx, Ry, G; Ans = 0; for (int case = 1; Case <= m; ++case) {Read (L); Read (R); L = (l + Ans)% n + 1; R = (r + Ans)% n + 1; if (L > R) Swap (L, R); x = (L-1)/blksize + 1; if (l! = l[x]) ++x; y = (r-1)/blksize + 1; if (r! = R[y])--y; if (x > y) {Ans = 0; for (int i = l; I <= R; ++i) {++cnt[a[i]]; if ((Cnt[a[i]] & 1) = = 0) ++ans; else if (cnt[a[i]]! = 1)--ans; } for (int i = l;I <= R; ++i)--cnt[a[i]; } else {Lx = l[x]; Ry = R[y]; Ans = F[x][y]; for (int i = l; i < Lx; ++i) {++cnt[a[i]]; G = Get (Lx, Ry, A[i]); if ((((cnt[a[i)] + G) & 1) = = 0) ++ans; else if (Cnt[a[i]] + G! = 1)--ans; } for (int i = r; i > Ry; i.) {++cnt[a[i]]; G = Get (Lx, Ry, A[i]); if ((((cnt[a[i)] + G) & 1) = = 0) ++ans; else if (Cnt[a[i]] + G! = 1)--ans; } for (int i = l; i < Lx; ++i)--cnt[a[i]; for (int i = r; i > Ry; i.)--cnt[a[i]; } printf ("%d\n", Ans); } return 0;}
[Bzoj 2821] Poetry (poetize) "chunking"