#include<bits/stdc++.h>
using namespace std;
template <typename T> void chmin(T &x,const T &y)
{
if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
if(x<y)x=y;
}
typedef long long s64;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
const int N=5e5+1e6+1e5+5;
int s[N],sa[N],qa[2][N],*rk=qa[0],*tmp=qa[1],tot;
int reads()
{
string str;
cin>>str;
int n=str.size();
rep(i,1,n)s[tot+i]=str[i-1]-'a'+1;
tot+=n+1;
s[tot]=26+tot;
return n;
}
int cnt[N+26],h[N];
void init_sa(int n)
{
rep(i,1,n)++cnt[s[i]];
rep(i,1,n+26)cnt[i]+=cnt[i-1];
rep(i,1,n)sa[cnt[s[i]]--]=i;
rk[sa[1]]=1;
rep(i,2,n)rk[sa[i]]=rk[sa[i-1]]+(s[sa[i]]!=s[sa[i-1]]);
for(int l=1;rk[sa[n]]<n;l<<=1)
{
int top=0;
rep(i,n-l+1,n)tmp[++top]=i;
rep(i,1,n)
if(sa[i]>l)tmp[++top]=sa[i]-l;
rep(i,1,rk[sa[n]])cnt[i]=0;
rep(i,1,n)++cnt[rk[i]];
rep(i,1,rk[sa[n]])cnt[i]+=cnt[i-1];
per(i,n,1)
{
int x=tmp[i];
sa[cnt[rk[x]]--]=x;
}
swap(tmp,rk);
rk[sa[1]]=1;
rep(i,2,n)rk[sa[i]]=rk[sa[i-1]]+(tmp[sa[i]]!=tmp[sa[i-1]]||tmp[sa[i]+l]!=tmp[sa[i-1]+l]);
}
rep(i,1,n)
{
int l=max(0,h[rk[i-1]]-1);
while(s[i+l]==s[sa[rk[i]+1]+l])++l;
h[rk[i]]=l;
}
}
#define cl (i*2)
#define cr (cl+1)
namespace ZKW
{
const int T=N*3;
int n,d;
int mnh[T];
namespace T1
{
int mni[T];
void add(int i,int x)
{
for(i+=d;mni[i]>x;i>>=1)mni[i]=x;
}
int pre_(int i,int r)
{
int lcp=n,ans=0;
for(i+=d;i>1;i>>=1)
if(i%2)
{
if(!(r-mni[i-1]+1>=min(lcp,mnh[i-1])))
{
chmax(ans,r-mni[i-1]+1);
chmin(lcp,mnh[i-1]);
continue;
}
--i;
while(i<=d)
if(r-mni[cr]+1>=min(lcp,mnh[cr]))
{
i=cr;
}
else
{
i=cl;
chmax(ans,r-mni[i+1]+1);
chmin(lcp,mnh[i+1]);
}
return max(ans,min(min(lcp,mnh[i]),r-mni[i]+1));
}
return ans;
}
int suf_(int i,int r)
{
i+=d;
int lcp=mnh[i],ans=0;
for(;i>1;i>>=1)
if(i%2==0)
{
if(!(r-mni[i+1]+1>=min(lcp,mnh[i+1])))
{
chmax(ans,r-mni[i+1]+1);
chmin(lcp,mnh[i+1]);
continue;
}
++i;
while(i<=d)
if(r-mni[cl]+1>=min(lcp,mnh[cl]))
{
i=cl;
}
else
{
i=cr;
chmax(ans,r-mni[i-1]+1);
chmin(lcp,mnh[i-1]);
}
return max(ans,min(lcp,r-mni[i]+1));
}
return ans;
}
int qiu(int i,int r)
{
// cerr<<pre_(i,r)<<endl;
// cerr<<suf_(i,r)<<endl;
return max(pre_(i,r),suf_(i,r));
}
};
namespace T2
{
bool mark[T];
void add(int i,bool w)
{
for(i+=d;i;i>>=1)mark[i]=w;
}
int pre_(int i)
{
int ans=n;
for(i+=d;i>1;i>>=1)
if(i%2)
{
if(!mark[i-1]){chmin(ans,mnh[i-1]);continue;}
--i;
while(i<=d)
if(mark[cr])i=cr;
else
{
i=cl;chmin(ans,mnh[i+1]);
}
return min(ans,mnh[i]);
}
return 0;
}
int suf_(int i)
{
i+=d;
int ans=mnh[i];
for(;i>1;i>>=1)
if(i%2==0)
{
if(!mark[i+1]){chmin(ans,mnh[i+1]);continue;}
--i;
while(i<=d)
if(mark[cl])i=cl;
else
{
i=cr;chmin(ans,mnh[i-1]);
}
return ans;
}
return 0;
}
int qiu(int i)
{
return max(pre_(i),suf_(i));
}
};
void init(int _n)
{
n=_n;
d=1;
while(d<=n)d*=2;
--d;
rep(i,1,n){mnh[i+d]=h[i];T1::mni[i+d]=n+1;}
per(i,d,1)
{
mnh[i]=min(mnh[cl],mnh[cr]);
T1::mni[i]=n+1;
}
}
};
int t[N];
const int M=1e5+5;
struct Query
{
int fir,len,r,next;s64 ans;
};
Query query[M];
s64 solve(const Query &x)
{
s64 ans=0;
rep(i,1,x.len)
{
int id=rk[x.fir+i];
// cerr<<ZKW::T1::qiu(id,x.r)<<endl;
// cerr<<ZKW::T2::qiu(id)<<endl;
ans+=x.len-i+1 -max(ZKW::T1::qiu(id,x.r),ZKW::T2::qiu(id));
ZKW::T2::add(id,1);
}
rep(i,1,x.len)ZKW::T2::add(rk[x.fir+i],0);
return ans;
}
int main()
{
freopen("1.in","r",stdin);
int n=reads();
int m;cin>>m;
rep(i,1,m)
{
query[i].fir=tot;
query[i].len=reads();
int l;
scanf("%d%d",&l,&query[i].r);
query[i].next=t[l];t[l]=i;
}
init_sa(tot);
ZKW::init(tot);
per(l,n,1)
{
ZKW::T1::add(rk[l],l);
for(int i=t[l];i;i=query[i].next)query[i].ans=solve(query[i]);
}
rep(i,1,m)printf("%lld\n",query[i].ans);
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 15.921 ms | 1 MB + 832 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #2 | 14.716 ms | 1 MB + 872 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #3 | 15.362 ms | 1 MB + 916 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #4 | 278.321 ms | 19 MB + 736 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #5 | 268.459 ms | 19 MB + 260 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #6 | 581.61 ms | 41 MB + 960 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #7 | 586.607 ms | 41 MB + 960 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #8 | 118.021 ms | 14 MB + 272 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #9 | 83.436 ms | 14 MB + 376 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #10 | 275.09 ms | 28 MB + 400 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #11 | 209.694 ms | 28 MB + 656 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 512.38 ms | 38 MB + 656 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #13 | 461.592 ms | 39 MB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #14 | 829.912 ms | 56 MB + 672 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #15 | 732.772 ms | 57 MB + 164 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 1.199 s | 67 MB + 144 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 1.017 s | 67 MB + 772 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 947.032 ms | 57 MB + 196 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 1.035 s | 61 MB + 140 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #20 | 1.136 s | 64 MB + 888 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #21 | 1.24 s | 69 MB + 48 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #22 | 1.258 s | 69 MB + 48 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #23 | 1.252 s | 69 MB + 48 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #24 | 1.24 s | 69 MB + 48 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #25 | 1.237 s | 69 MB + 48 KB | Wrong Answer | Score: 0 | 显示更多 |