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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <cmath> #include <cctype> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
namespace IO { const int MAXSIZE = 1 << 18 | 1; char buf[MAXSIZE], *p1, *p2;
inline int Gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> void read(T& x) { x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
const int MAXN = 1e6+5, MAXM = 1e4+1; int block;
struct Ask { int idx, L, R; bool operator < (const Ask& rhs) const { return R / block == rhs.R / block? (L == rhs.L? 0: ((R / block) & 1) & (L < rhs.L)): R < rhs.R; } } Q[MAXN];
int n, N, q; int A[MAXN]; int S[MAXN]; int idx[MAXN]; int firstpos[MAXN]; int qlen[MAXN];
inline void add(int val, int id) { S[++n] = val, idx[n] = id; }
namespace SA { int sa[MAXN], rnk[MAXN], id[MAXN], px[MAXN], ht[MAXN], cnt[MAXN];
inline bool cmp(const int& x, const int& y, const int& k) { return id[x] == id[y] && id[x+k] == id[y+k]; }
inline void build(int m) { int i, k, p = 0; for (i = 1; i <= n; ++i) ++cnt[rnk[i] = S[i]]; for (i = 1; i <= m; ++i) cnt[i] += cnt[i-1]; for (i = n; i; --i) sa[cnt[rnk[i]]--] = i; for (k = 1; k <= n && p < n; k <<= 1, m = p) { for (p = 0, i = n; i > n-k; --i) id[++p] = i; for (i = 1; i <= n; ++i) if (sa[i] > k) id[++p] = sa[i]-k; memset(cnt, 0, sizeof (int) * (m+1)); for (i = 1; i <= n; ++i) ++cnt[px[i] = rnk[id[i]]]; for (i = 1; i <= m; ++i) cnt[i] += cnt[i-1]; for (i = n; i; --i) sa[cnt[px[i]]--] = id[i]; swap(rnk, id), rnk[sa[1]] = p = 1; for (i = 2; i <= n; ++i) rnk[sa[i]] = cmp(sa[i], sa[i-1], k)? p: ++p; } for (i = 1; i <= n; ++i) rnk[sa[i]] = i; for (k = 0, i = 1; i <= n; ++i) { if (k) --k; while (S[i + k] == S[sa[rnk[i]-1] + k]) ++k; ht[rnk[i]] = k; } } } using SA::rnk; using SA::sa;
namespace ST { const int LOG = 21; int minHt[LOG][MAXN], lg2[MAXN];
inline void init() { for (int i = 1; i <= n; ++i) minHt[0][i] = SA::ht[i]; for (int j = 1; j < LOG; ++j) for (int i = 1; i+(1<<j)-1 <= n; ++i) minHt[j][i] = min(minHt[j-1][i], minHt[j-1][i+(1<<(j-1))]); lg2[1] = 0; for (int i = 2; i <= n; ++i) lg2[i] = lg2[i/2] + 1; }
inline int RMQ(int L, int R) { int k = lg2[R-L+1]; return min(minHt[k][L], minHt[k][R-(1<<k)+1]); } }
namespace MoAlg { int L, R, ans1; int Ans1[MAXN], Ans2[MAXN], cnt[MAXN];
inline void add(int pos, int cur) { pos = idx[pos]; if (pos <= N && ++cnt[pos] == 1) ++ans1, Ans2[pos] += q-cur+1; }
inline void del(int pos, int cur) { pos = idx[pos]; if (pos <= N && --cnt[pos] == 0) --ans1, Ans2[pos] -= q-cur+1; }
inline void solve() { block = int( n / sqrt(q) ); sort(Q+1, Q+1+q); L = 1, R = ans1 = 0; for (int i = 1; i <= q; ++i) { const Ask& qr = Q[i]; while (L > qr.L) add(sa[--L], i); while (R > qr.R) del(sa[R--], i); while (L < qr.L) del(sa[L++], i); while (R < qr.R) add(sa[++R], i); Ans1[qr.idx] = ans1; } } } using MoAlg::Ans1; using MoAlg::Ans2;
int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif read(N), read(q); for (int k, i = 1; i <= N; ++i) { read(k); for (int j = 1; j <= k; ++j) read(A[j]), add(A[j]+1, i); add(MAXM + 2*i-1, N+1); read(k); for (int j = 1; j <= k; ++j) read(A[j]), add(A[j]+1, i); add(MAXM + 2*i, N+1); } for (int k, i = 1; i <= q; ++i) { read(k), qlen[i] = k, firstpos[i] = n+1; for (int j = 1; j <= k; ++j) read(A[j]), add(A[j]+1, N + i); add(MAXM + 2*N + i, N+1); } SA::build(MAXM + 2*N + q), ST::init(); for (int i = 1; i <= q; ++i) { static int L, R, qL, qR; L = 1, R = rnk[firstpos[i]]-1, qL = rnk[firstpos[i]]; while (L <= R) { int Mid = (L + R) / 2; if (ST::RMQ(Mid + 1, rnk[firstpos[i]]) >= qlen[i]) qL = Mid, R = Mid - 1; else L = Mid + 1; } L = rnk[firstpos[i]]+1, R = n, qR = rnk[firstpos[i]]; while (L <= R) { int Mid = (L + R) / 2; if (ST::RMQ(rnk[firstpos[i]] + 1, Mid) >= qlen[i]) qR = Mid, L = Mid + 1; else R = Mid - 1; } Q[i] = (Ask){ i, qL, qR }; } MoAlg::solve(); for (int i = 1; i <= q; ++i) printf("%d\n", Ans1[i]); for (int i = 1; i <= N; ++i) printf("%d%c", Ans2[i], " \n"[i==N]); return 0; }
|