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
|
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #define rep(i,x,y) for(register int i=x;i<=y;++i) #define repd(i,x,y) for(register int i=x;i>=y;--i) #define long long using namespace std; const int N=5e5+7; int tr[N<<2][2],nm[N],sum[N<<2],n,m,cnt=1,end[N]; inline void (int *a,int len){ int p=1; rep(i,1,len){ if(!tr[p][a[i]])tr[p][a[i]]=++cnt; sum[tr[p][a[i]]]++;p=tr[p][a[i]]; } end[p]++; } inline int query(int *a,int len){ int p=1,ans=0; rep(i,1,len){ if(!tr[p][a[i]])return ans; p=tr[p][a[i]]; if(p)ans+=end[p]; } return ans+sum[p]-end[p]; } int main(){ scanf("%d%d",&n,&m); rep(i,1,n){ int x;scanf("%d",&x); rep(j,1,x)scanf("%d",&nm[j]); add(nm,x); } rep(i,1,m){ int x;scanf("%d",&x); rep(j,1,x)scanf("%d",&nm[j]); printf("%dn",query(nm,x)); } return 0; }
|
近期评论