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 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <bits/stdc++.h> #define ull unsigned long long
const int N = 2e5 + 10, P = 1331, INF = 1 <<30; ull p[N], hasha, sump; int pos[N]; struct Splay { int s[2], p, sz, pos; ull v, sum; void init(ull _v, int _p, int _pos) { v = sum = _v, p = _p, pos = _pos, sz = 1; } } t[N << 1]; int root, tot;
inline int read() { int x = 0, y = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * y; }
inline void pushup(int x) { t[x].sz = t[t[x].s[0]].sz + t[t[x].s[1]].sz + 1; t[x].sum = (t[t[x].s[0]].sum * P + t[x].v) * p[t[t[x].s[1]].sz] + t[t[x].s[1]].sum; }
inline void rotate(int x) { int y = t[x].p, z = t[y].p, k = t[y].s[1] == x; t[z].s[t[z].s[1] == y] = x, t[x].p = z; t[y].s[k] = t[x].s[!k], t[t[x].s[!k]].p = y; t[x].s[!k] = y, t[y].p = x; pushup(y), pushup(x); }
inline void splay(int x, int k) { while (t[x].p != k) { int y = t[x].p, z = t[y].p; if (z != k) if ((t[z].s[1] == y) ^ (t[y].s[1] == x)) rotate(x); else rotate(y); rotate(x); } if (!k) root = x; }
inline void insert(int pos, int v) { int u = root, p = 0; while (u) p = u, u = t[u].s[pos > t[u].pos]; u = ++tot; if (p) t[p].s[pos > t[p].pos] = u; t[u].init((ull)v, p, pos); splay(u, 0); }
inline void del(int pos) { int u = root; while (u) { if (t[u].pos == pos) break; else if (t[u].pos > pos) u = t[u].s[0]; else u = t[u].s[1]; } splay(u, 0); int l = t[u].s[0], r = t[u].s[1]; while (t[l].s[1]) l = t[l].s[1]; while (t[r].s[0]) r = t[r].s[0]; splay(l, 0), splay(r, l); t[r].s[0] = 0; pushup(r), pushup(l); }
int main() { int n = read(), m = read(); p[0] = sump = 1; insert(-INF, 0), insert(INF, 0); for (int i = 1; i <= 2e5; i++) p[i] = p[i - 1] * P; for (int i = 1; i <= n - 1; i++) sump += p[i]; for (int i = 1; i <= n; i++) hasha = hasha * P + read(); for (int i = 1; i <= m; i++) pos[read()] = i; for (int i = 1; i <= n - 1; i++) insert(pos[i], i); int res = 0; for (int i = n; i <= m; i++) { insert(pos[i], i); if (t[root].sum == hasha * P) res++; hasha += sump; del(pos[i - n + 1]); } printf("%d\n", res); return 0; }
|