提交记录 17287


用户 题目 状态 得分 用时 内存 语言 代码长度
luosiyuan noi18c. 【NOI2018】你的名字 Accepted 100 937.812 ms 446496 KB C++11 2.42 KB
提交时间 评测时间
2022-01-23 14:51:12 2022-01-23 14:51:54
#include<bits/stdc++.h>
using namespace std;
const int A=1000005;
vector<int> g[A];
char str[A];
int len,lens,q,zzw,ls[A*40],rs[A*40],mx[A*40],root[A],tot,fl[A];
typedef long long ll;
void Insert(int &p,int l,int r,int x,int y){
	p=++tot,mx[p]=y;
	if(l==r)return ;
	int mid=(l+r)/2;
	if(x<=mid)Insert(ls[p],l,mid,x,y);
	else Insert(rs[p],mid+1,r,x,y);
}
int Merge(int p,int q,int l,int r){
	if(!p||!q)return p+q;
	if(l>r)return 0;
	int rr=++tot,mid=(l+r)/2;
	ls[rr]=Merge(ls[p],ls[q],l,mid),rs[rr]=Merge(rs[p],rs[q],mid+1,r);
	mx[rr]=max(mx[ls[rr]],mx[rs[rr]]);
	return rr;
}
int Query(int p,int l,int r,int x,int y){
	if((!p)||(x<=l&&r<=y))return mx[p];
	int mid=(l+r)/2,re=0;
	if(x<=mid)re=max(re,Query(ls[p],l,mid,x,y));
	if(mid<y)re=max(re,Query(rs[p],mid+1,r,x,y));
	return re;
}
struct SAM {
	int c[A][26],fa[A],len[A],lst,tot,w[A],rk[A],s[A];
	int Ins(int x,int ww) {
		int p=lst,now=++tot;
		memset(c[now],0,sizeof(c[now])),lst=now,w[now]=ww;
		len[now]=len[p]+1;
		for(; p&&!c[p][x]; p=fa[p])c[p][x]=now;
		if(!p)return fa[now]=1;
		int q=c[p][x];
		if(len[p]+1==len[q])return fa[now]=q;
		int nq=++tot;
		memcpy(c[nq],c[q],sizeof(c[q])),len[nq]=len[p]+1,fa[nq]=fa[q],fa[now]=fa[q]=nq;
		for(; p&&c[p][x]==q; p=fa[p])c[p][x]=nq;
		return 0;
	}
	void Clear(){
		tot=lst=1;
		memset(c[1],0,sizeof(c[1]));
	}
	void Make(){
		for(int i=1;i<=tot;i++)s[len[i]]++;
		for(int i=1;i<=tot;i++)s[i]+=s[i-1];
		for(int i=1;i<=tot;i++)if(w[i])Insert(root[i],1,lens,w[i],len[i]);
		for(int i=tot;i>=1;i--)rk[s[len[i]]--]=i;
		for(int i=tot,x;i>=1;i--)if(fa[x=rk[i]])root[fa[x]]=Merge(root[fa[x]],root[x],1,lens);
	}
	SAM(){
		Clear();
	}
}S,T;
void MakeS(){
	for(int i=2;i<=S.tot;i++)g[S.fa[i]].push_back(i);
	S.Make();
}
bool Has(int p,int w,int l,int r){
	if(!S.c[p][w])return 0;
	return Query(root[S.c[p][w]],1,lens,1,r)>=l;
}
int main() {
//	freopen("name18.in","r",stdin);
//	freopen("name.out","w",stdout);
	scanf("%s",str),lens=len=strlen(str);
	for(int i=0;i<len;i++)S.Ins(str[i]-'a',i+1);
	MakeS();
	scanf("%d",&q);
	while(q--){
		int L,R;
		scanf("%s%d%d",str+1,&L,&R),len=strlen(str+1),T.Clear();
		for(int i=1;i<=len;i++)T.Ins(str[i]-'a',i),fl[i]=T.len[T.fa[T.lst]];
		ll ans=0;
		for(int i=1,p=1,l=0;i<=len;i++){
			int w=str[i]-'a';
			while(1){
				if(Has(p,w,L+l,R)){
					p=S.c[p][w],l++;
					break;
				}
				if(!l)break;
				l--;
				if(l==S.len[S.fa[p]])p=S.fa[p];
			}
			if(l>fl[i])ans-=l-fl[i];
		}
		for(int i=1;i<=len;i++)ans+=i-fl[i];
		cout<<ans<<'\n';
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #15.714 ms23 MB + 96 KBAcceptedScore: 4

Testcase #26.13 ms23 MB + 464 KBAcceptedScore: 4

Testcase #36.193 ms23 MB + 468 KBAcceptedScore: 4

Testcase #427.478 ms23 MB + 912 KBAcceptedScore: 4

Testcase #526.744 ms23 MB + 892 KBAcceptedScore: 4

Testcase #6691.763 ms436 MB + 32 KBAcceptedScore: 4

Testcase #7688.835 ms435 MB + 932 KBAcceptedScore: 4

Testcase #8111.76 ms85 MB + 544 KBAcceptedScore: 4

Testcase #9121.774 ms81 MB + 60 KBAcceptedScore: 4

Testcase #10259.748 ms149 MB + 196 KBAcceptedScore: 4

Testcase #11282.06 ms143 MB + 412 KBAcceptedScore: 4

Testcase #12430.062 ms214 MB + 292 KBAcceptedScore: 4

Testcase #13465.231 ms208 MB + 272 KBAcceptedScore: 4

Testcase #14603.288 ms281 MB + 784 KBAcceptedScore: 4

Testcase #15668.259 ms275 MB + 336 KBAcceptedScore: 4

Testcase #16798.091 ms349 MB + 424 KBAcceptedScore: 4

Testcase #17879.066 ms342 MB + 768 KBAcceptedScore: 4

Testcase #18414.172 ms157 MB + 436 KBAcceptedScore: 4

Testcase #19575.196 ms219 MB + 948 KBAcceptedScore: 4

Testcase #20755.562 ms284 MB + 340 KBAcceptedScore: 4

Testcase #21909.824 ms349 MB + 528 KBAcceptedScore: 4

Testcase #22937.812 ms349 MB + 460 KBAcceptedScore: 4

Testcase #23931.086 ms349 MB + 380 KBAcceptedScore: 4

Testcase #24922.373 ms349 MB + 504 KBAcceptedScore: 4

Testcase #25908.494 ms349 MB + 532 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:43:41 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠