inline void Work(void) { T.clear(); T.Add(b[1], 1); int j = 0; last = 1; for (int i = 2; i <= m; i++) { while (j && Ask(b, i - j, b[i]) != rank[j + 1]) j = p[j]; if (j == 0) Ask(b, i - j, b[i]); if (Ask(b, i - j, b[i]) == rank[j + 1]) j++; p[i] = j; T.Add(b[i], 1); } T.clear(); j = 0; last = 1; for (int i = 1; i <= n; i++) { while (j && Ask(a, i - j, a[i]) != rank[j + 1]) j = p[j]; if (j == 0) Ask(a, i - j, a[i]); if (Ask(a, i - j, a[i]) == rank[j + 1]) j++; if (j == m) { Ans[i] = true; j = p[j]; } T.Add(a[i], 1); } f[0] = 0; for (int i = 1; i <= n; i++) { // printf("%d ", Ans[i]); f[i] = f[i - 1]; if (Ans[i] > 0) f[i] = max(f[i], f[i - m] + 1); } // puts(""); printf("%d\n", f[n]); }
inline void Prepare(void) { T.clear(); T.n = MAXH; for (int i = 1; i <= m; i++) { rank[i] = make_pair(T.Cal(b[i] - 1) + 1, T.Cal(b[i])); T.Add(b[i], 1); } }
inline pair<int, int> Ask(int a[], int L, int t) { while (last < L) T.Add(a[last++], -1); return make_pair(T.Cal(t - 1) + 1, T.Cal(t)); }