#include #include #include #include #include #include #include using namespace std; int opmin(int a,int b){return min(a,b);} int emin(){return(int)1e9;} using dat=pair; dat opsum(dat a,dat b) { a.first+=b.first; a.second+=b.second; return a; } dat esum(){return make_pair(0LL,0);} dat mp(int f,dat x) { x.first+=(long long)f*x.second; return x; } int cmp(int f,int g){return f+g;} int id(){return 0;} int N,Q; string S; int inv[2<<17]; int L[1<<17],R[1<<17]; long long ans[1<<17]; vectorQid[1<<17]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>N>>Q>>S; vectorSA=atcoder::suffix_array(S); vectorLCP=atcoder::lcp_array(S,SA); for(int i=0;ilcp_seg(LCP); vectorlen_init(N); for(int i=0;ilen_seg(len_init); for(int i=0;i>L[i]>>R[i]; L[i]--; int idx=inv[L[i]]; R[i]=R[i]-L[i];//len int l=lcp_seg.min_left(idx,[&i](int x){return x>=R[i];}); L[i]=l; Qid[l].push_back(i); ans[i]=len_seg.prod(0,l).first; } vectorinit(N,make_pair(0,1)); atcoder::lazy_segtreeseg(init); vector >st; for(int i=N;i--;) { if(i+1=len) { int c=st.back().first; seg.apply(len,st.back().second,-c); cnt+=c; st.pop_back(); } if(cnt>0) { st.push_back(make_pair(cnt,len)); } } seg.apply(0,N-SA[i],1); st.push_back(make_pair(1,N-SA[i])); for(int id:Qid[i]) { ans[id]+=seg.prod(1,R[id]).first; } } for(int i=0;i