54 pts Wa on 7 - 11 求调。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
const int maxm=1e4;
const int B=pow(maxn,0.5);
namespace SA{
int x[maxn+5],y[maxn+5];
int ws[maxn+5],wv[maxn+5];
inline bool Cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
inline void Getsa(int *r,int *sa,int n,int m){
int i,j,p;
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[x[i]=r[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[wv[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
for(swap(x,y),p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=Cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
}
struct Node{
int l,r,id;
Node(int il,int ir,int iid){
l=il,r=ir,id=iid;
}
Node(){};
};
int n,m;
int len,tot;
int a[maxn+5],ca[maxn+5];
int id[maxn+5];
int sa[maxn+5],csa[maxn+5];
Node b[maxn+5];int bt;
int L=1,R,res;
int cnt[maxn+5];
int ap[maxn+5],bp[maxn+5];
inline bool Cmp(Node x,Node y){
x.l/=B,y.l/=B;
return (x.l<y.l)||(x.l==y.l&&x.r<y.r);
}
inline void Insert(int x){
x=id[sa[x]];
if(!cnt[x]) res++,ap[x]+=len;
cnt[x]++;
}
inline void Delete(int x){
x=id[sa[x]];
cnt[x]--;
if(!cnt[x]) res--,ap[x]-=len;
}
inline void Move(int l,int r){
while(l<L) Insert(--L);
while(R<r) Insert(++R);
while(L<l) Delete(L++);
while(r<R) Delete(R--);
}
signed main(){
int x,tl,tr,l,r,mid,tmp;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=2;j++){
scanf("%d",&len);
while(len--){
scanf("%d",&a[++tot]);
id[tot]=i;
}
a[++tot]=maxm+1;
}
}
for(int i=1;i<=tot;i++) ca[i-1]=a[i];
SA::Getsa(ca,csa,tot,maxm+2);
for(int i=1;i<=tot;i++) sa[i]=csa[i-1]+1;
for(int i=1;i<=m;i++){
tl=1,tr=tot;
scanf("%d",&len);
for(int j=0;j<len;j++){
scanf("%d",&x);
l=tl,r=tr;
while(l<=r){
mid=(r-l)/2+l;
if(a[sa[mid]+j]<x) l=mid+1;
else r=mid-1;
}
tmp=l,l=tl,r=tr;
while(l<=r){
mid=(r-l)/2+l;
if(a[sa[mid]+j]<=x) l=mid+1;
else r=mid-1;
}
tl=tmp,tr=r;
}
if(tl<=tr) b[++bt]=Node(tl,tr,i);
}
sort(b+1,b+bt+1,Cmp);
for(int i=1;i<=bt;i++){
len=bt-i+1;
Move(b[i].l,b[i].r);
bp[b[i].id]=res;
}
for(int i=1;i<=m;i++) printf("%d\n",bp[i]);
for(int i=1;i<=n;i++) printf("%d ",ap[i]);
return 0;
}