提交记录 4152


用户 题目 状态 得分 用时 内存 语言 代码长度
jijiang noi18c. 【NOI2018】你的名字 Accepted 100 996.689 ms 378908 KB C++ 2.87 KB
提交时间 评测时间
2018-07-18 21:40:27 2020-07-31 22:33:47
#include<bits/stdc++.h>
typedef long long ll;
char ibuf[1<<23],*ih=ibuf,obuf[1<<22],*oh=obuf;
inline void read(int&x){
	for(;!isdigit(*ih);++ih);
	for(x=0;isdigit(*ih);x=x*10+*ih++-48);
}
inline void readstr(char*c){
	for(;isspace(*ih);++ih);
	for(;!isspace(*ih);*c++=*ih++);*c=0;
}
inline void out(ll x){
	if(!x){*oh++='0';return;}
	static int buf[30];int xb=0;
	for(;x;x/=10)buf[++xb]=x%10;
	for(;xb;)*oh++=buf[xb--]|48;
}
const int N=1000005;
struct node{
	int l,r,sz;
}t[N*22];
int sz;
void ins(int x,int&y,int l,int r,int k){
	t[y=++sz]=t[x];++t[y].sz;
	if(l==r)return;int m=(l+r)>>1;
	if(k>m)ins(t[x].r,t[y].r,m+1,r,k);else ins(t[x].l,t[y].l,l,m,k);
}
bool query(int x,int y,int rl,int rr,int l,int r){
	if(rl==l && rr==r)return t[y].sz>t[x].sz;
	if(!y)return 0;
	int m=(rl+rr)>>1;
	if(r<=m)return query(t[x].l,t[y].l,rl,m,l,r);
		else if(l>m)return query(t[x].r,t[y].r,m+1,rr,l,r);
				else return query(t[x].l,t[y].l,rl,m,l,m) || query(t[x].r,t[y].r,m+1,rr,m+1,r);
}
char c[N];
struct SAM{
	int ch[N][26],pre[N],step[N],xb,n,wz[N],pos[N];
	inline void build(){
		int p,q,np,nq,lst=xb=1,i,x;
		readstr(c+1);memset(ch[1],0,104);
		for(i=1;c[i];++i){
			x=c[i]-'a';step[np=++xb]=step[p=lst]+1;
			memset(ch[np],0,104);
			for(;p && !ch[p][x];p=pre[p])ch[p][x]=np;
			if(p){
				q=ch[p][x];
				if(step[p]+1!=step[q]){
					step[nq=++xb]=step[p]+1;
					memcpy(ch[nq],ch[q],104);
					pre[nq]=pre[q];pre[q]=pre[np]=nq;
					for(;p && ch[p][x]==q;p=pre[p])ch[p][x]=nq;
				}else pre[np]=q;
			}else pre[np]=1;
			wz[pos[lst=np]=i]=np;
		}n=i-1;
	}
	int rt[N],id[N],ri[N],dfn[N],cn;
	std::vector<int>e[N];
	void dfs(int x){
		dfn[id[x]=++cn]=x;
		for(int i=0;i<e[x].size();++i)dfs(e[x][i]);
		ri[x]=cn;
	}
	inline void ini(){
		int i;
		for(i=1;i<=xb;++i)e[pre[i]].push_back(i);
		dfs(1);
		for(i=1;i<=cn;++i){
			rt[i]=rt[i-1];
			if(pos[dfn[i]])ins(rt[i],rt[i],1,n,pos[dfn[i]]);
		}		
	}
	int len[N];
	inline void wk(){
		ll ans=0;
		int i;static int a[N],b[N];
		for(i=1;i<=xb;++i)++a[step[i]];
		for(i=1;a[i];++i)a[i]+=a[i-1];
		for(i=1;i<=xb;++i)b[a[step[i]]--]=i;
		for(i=xb;i;--i){
			int x=step[b[i]]-step[pre[b[i]]];
			if(len[b[i]]>x)ans+=x,len[pre[b[i]]]=len[b[i]]-x;
				else ans+=len[b[i]];
		}
		for(i=1;i<=xb;++i)a[step[i]]=0;
		out(ans);*oh++='\n';
	}
}S,T;
int Q,l,r,i;
int main(){
//	freopen("name.in","r",stdin);freopen("name.out","w",stdout);
	fread(ibuf,1,1<<23,stdin);
	S.build();S.ini();read(Q);
	while(Q--){
		T.build();read(l);read(r);
		int u=1,x;
		memset(T.len+1,0,T.xb<<2);
		int le=0;
		for(i=1;i<=T.n;++i){
			x=c[i]-'a';
			for(;u>1 && !S.ch[u][x];u=S.pre[u]);
			if(le>S.step[u])le=S.step[u];
			if(!S.ch[u][x]){T.len[T.wz[i]]=i;continue;}u=S.ch[u][x];++le;
			for(;u>1 && (le>r-l+1 || !query(S.rt[S.id[u]-1],S.rt[S.ri[u]],1,S.n,l+le-1,r));)
				--le<=S.step[S.pre[u]]?u=S.pre[u]:0;
			if(le>S.step[u])le=S.step[u];
			T.len[T.wz[i]]=i-le;
		}
		T.wk();
	}
	fwrite(obuf,1,oh-obuf,stdout);
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #111.108 ms46 MB + 32 KBAcceptedScore: 4

Testcase #212.109 ms46 MB + 328 KBAcceptedScore: 4

Testcase #312.332 ms46 MB + 328 KBAcceptedScore: 4

Testcase #460.956 ms47 MB + 208 KBAcceptedScore: 4

Testcase #558.755 ms47 MB + 184 KBAcceptedScore: 4

Testcase #6551.258 ms370 MB + 28 KBAcceptedScore: 4

Testcase #7552.113 ms369 MB + 932 KBAcceptedScore: 4

Testcase #884.605 ms92 MB + 92 KBAcceptedScore: 4

Testcase #9118.117 ms87 MB + 520 KBAcceptedScore: 4

Testcase #10188.414 ms137 MB + 36 KBAcceptedScore: 4

Testcase #11295.741 ms131 MB + 116 KBAcceptedScore: 4

Testcase #12320.517 ms182 MB + 608 KBAcceptedScore: 4

Testcase #13507.993 ms176 MB + 456 KBAcceptedScore: 4

Testcase #14450.645 ms229 MB + 864 KBAcceptedScore: 4

Testcase #15744.493 ms223 MB + 300 KBAcceptedScore: 4

Testcase #16596.053 ms277 MB + 272 KBAcceptedScore: 4

Testcase #17996.689 ms270 MB + 500 KBAcceptedScore: 4

Testcase #18489.873 ms146 MB + 860 KBAcceptedScore: 4

Testcase #19597.209 ms189 MB + 324 KBAcceptedScore: 4

Testcase #20705.625 ms232 MB + 1008 KBAcceptedScore: 4

Testcase #21778.661 ms277 MB + 464 KBAcceptedScore: 4

Testcase #22811.587 ms277 MB + 396 KBAcceptedScore: 4

Testcase #23809.205 ms277 MB + 312 KBAcceptedScore: 4

Testcase #24799.995 ms277 MB + 444 KBAcceptedScore: 4

Testcase #25792.497 ms277 MB + 472 KBAcceptedScore: 4


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