NOI2018 你的名字

这个lk菜爆了

线段树都能写错

luogu loj ### 68pts 让TSSAM中匹配得到每个前缀最长在S中出现的后缀,长度为f[i],然后对TSAM,每加入一个T[i]trans(start,T[1:i])=t,对答案的贡献就是min(maxlen(t)-maxlen(link(t)),maxlen(t)-f[i])=maxlen(t)-max(maxlen(link(t)),f[i])。 ### 100pts 每次S只是原来S的子串。

用可持久化线段树求出每个状态的endpos集合。

设当前匹配上的串是W

每次W=W+c的时候,只要W不在S中,就把W第一个字符删掉。

W长度为len,每次成功匹配len最多+1,最多匹配成功|T|次,所以-1也不超过|T|次。时间复杂度就是O(|T|log|S|),其中log|S|是查询的复杂度。

/*
Author: QAQ-Automaton
LANG: C++
PROG: name.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define inf 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	inline bool read (signed &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}

	inline bool read (long long &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}
	inline bool read (char &x) {
		x=gc();
		return x!=EOF;
	}
	inline bool read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
		while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
		*x=0;
		return 1;
	}
	template<typename A,typename ...B>
	inline bool read(A &x,B &...y){
		return read(x)&&read(y...);
	}
	// print a signed integer
	inline bool write (signed x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}

	inline bool write (long long x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}
	inline bool write (char x) {
		putc(x);
		return 0;
	}
	inline bool write(const char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	inline bool write(char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	template<typename A,typename ...B>
	inline bool write(A x,B ...y){
		return write(x)||write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
char s[500005],t[500005];
int rt[1000005],ls[20000005],rs[20000005],c,n;
void merge(int &x,int y){
	if(!y)return;
	if(!x){x=y;return;}
	++c;
	ls[c]=ls[x];rs[c]=rs[x];
	x=c;
	merge(ls[x],ls[y]);
	merge(rs[x],rs[y]);
}
int insert(int x,int l=1,int r=n){
	int cur=++c;
	if(l==r)return cur;	
	int mid=(l+r)>>1;
	if(x<=mid)ls[cur]=insert(x,l,mid);
	else rs[cur]=insert(x,mid+1,r);
	return cur;
}
int query(int x,int l,int r,int sl=1,int sr=n){
	if(!x)return 0;
	if(l<=sl && sr<=r)return 1;
	int mid=(sl+sr)>>1;
	if(l<=mid)if(query(ls[x],l,r,sl,mid))return 1;
	if(mid<r)if(query(rs[x],l,r,mid+1,sr))return 1;
	return 0;
}
namespace A{
	int to[1000005][26],fail[1000005],len[1000005],now=1,t=1,l;
	void extend(int x){
		int nw=++t,i=now;
		len[nw]=++l;
		for(;i&&!to[i][x];i=fail[i])to[i][x]=nw;
		if(!i)fail[nw]=1;
		else if(len[i]+1==len[to[i][x]])fail[nw]=to[i][x];
		else{
			int o=to[i][x],n=++t;
			len[n]=len[i]+1;
			fail[n]=fail[o];
			for(int j=0;j<26;++j)to[n][j]=to[o][j];
			fail[o]=fail[nw]=n;
			for(;i&&to[i][x]==o;i=fail[i])to[i][x]=n;
		}
		now=nw;
		rt[nw]=insert(l);
	}
	int a[1000005],b[1000005];
	void work(){
		for(int i=1;i<=t;++i)++a[len[i]];	
		for(int i=1;i<=l;++i)a[i]+=a[i-1];
		for(int i=t;i;--i){
			b[a[len[i]]]=i;
			--a[len[i]];
		}
		for(int i=t;i;--i)merge(rt[fail[b[i]]],rt[b[i]]);
	}
}
namespace B{
	int to[1000005][26],fail[1000005],len[1000005],now,t,l;
	void init(){
		now=t=1;l=0;
		for(int i=0;i<26;++i)to[1][i]=0;
		fail[1]=0;
	}
	int extend(int x,int mx){
		int nw=++t,i=now;
		fail[nw]=0;
		for(int j=0;j<26;++j)to[nw][j]=0;
		len[nw]=++l;
		for(;i&&!to[i][x];i=fail[i])to[i][x]=nw;
		if(!i)fail[nw]=1;
		else if(len[i]+1==len[to[i][x]])fail[nw]=to[i][x];
		else{
			int o=to[i][x],n=++t;
			len[n]=len[i]+1;
			fail[n]=fail[o];
			for(int j=0;j<26;++j)to[n][j]=to[o][j];
			fail[o]=fail[nw]=n;
			for(;i&&to[i][x]==o;i=fail[i])to[i][x]=n;
		}
		now=nw;
		return len[nw]-max(len[fail[nw]],mx);
	}
}

int main(){
	freopen("name.in","r",stdin);
	freopen("name.out","w",stdout);
	read(s);
	n=strlen(s);
	for(char *i=s;*i;++i)A::extend(*i-'a');
	A::work();
	int q,l,r;
	read(q);
	for(;q;--q){
		read(t);
		read(l,r);
		int at=1,len=0;
		ll ans=0;
		B::init();
		for(char *i=t;*i;++i){
			for(;at!=1 && !A::to[at][*i-'a'];at=A::fail[at]){
				len=A::len[A::fail[at]];
			}
			if(A::to[at][*i-'a']){++len;at=A::to[at][*i-'a'];}
			while(len&&!query(rt[at],l+len-1,r)){
				--len;
				if(A::len[A::fail[at]]==len)at=A::fail[at];
			}
			ans+=B::extend(*i-'a',len);
		}
		write(ans,'\n');
	}
	return 0;
}