题解:P11823 [湖北省选模拟 2025] 最后的台词 / lines
前言
好难。
思路
首先我们考虑一下
假设当前在
然后考虑一下
代码
简直是史。
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define fire signed
#define il inline
template<class T> il void print(T x) {
if(x<0) putchar('-'),x=-x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
int T=1;
const int N=2e6+10;
vector<int>v[N];
string st;
struct node{
int ml;
int fa;
int c[26];
}s[N];
vector<array<int,3>>qy[N];
vector<int>my[N];
#define ull unsigned long long
const int ba=131;
ull ha[N],jc[N];
ull has(int l,int r) {
return ha[r]-ha[l-1]*jc[r-l+1];
}
int lst=1,idx=1,len=0;
int ed[N],mn[N],pos[N];
void insert(int c) {
idx++;
len++;
s[idx].ml=len;
ed[idx]=len;
pos[len]=idx;
int p=lst;
for(;p&&!s[p].c[c];p=s[p].fa) s[p].c[c]=idx;
lst=idx;
if(!p) {
s[idx].fa=1;
}else {
int q=s[p].c[c];
if(s[q].ml==s[p].ml+1) {
s[idx].fa=q;
}else {
int ip=++idx;
s[ip]=s[q];
s[ip].ml=s[p].ml+1;
s[lst].fa=s[q].fa=ip;
for(;p&&s[p].c[c]==q;p=s[p].fa) s[p].c[c]=ip;
}
}
}
int q;
void init() {
rep(i,2,idx) v[s[i].fa].pb(i);
}
int f[N][20],mx[N];
void dfs(int x,int fa) {
f[x][0]=fa;
for(auto to:v[x]) {
if(to==fa) continue;
dfs(to,x);
mx[x]=max(mx[x],mx[to]);
mn[x]=min(mn[x],mn[to]);
}
if(ed[x]) {
mn[x]=min(mn[x],ed[x]);
mx[x]=max(mx[x],ed[x]);
}
}
int ans[N];
int fir[N];
array<int,2>out[N];
int tag[N];
int bl[N],L[N],R[N],bb;
void rebuild(int it,int ll,int rr) {
rep(j,ll,rr) {
int nxt=min(fir[j],tag[it]);
if(nxt<L[it]) {
out[j][0]=nxt;
out[j][1]=1;
}else if(out[nxt][0]<L[it]) {
if(out[j][1]>out[nxt][1]+1||(out[j][1]==out[nxt][1]+1&&out[j][0]>out[nxt][0])) {
out[j][0]=out[nxt][0];
out[j][1]=out[nxt][1]+1;
}
}
}
}
void modify(int l,int r,int k) {
int itl=bl[l],itr=bl[r];
if(itl==itr) {
rep(j,l,r) fir[j]=min(fir[j],k);
rebuild(itl,l,R[itl]);
}else {
rep(j,l,R[itl]) fir[j]=min(fir[j],k);
rebuild(itl,l,R[itl]);
rep(j,L[itr],r) {
fir[j]=min(fir[j],k);
if(out[j][1]==1) {
out[j][0]=min(out[j][0],k);
}else{
out[j][0]=k;
out[j][1]=1;
}
}
rebuild(itr,L[itr],R[itr]);
rep(i,itl+1,itr-1) {
tag[i]=min(tag[i],k);
}
}
}
int n;
int bound(int x,int y) {
int res=false;
if(x<=y) return false;
while(bl[x]>bl[y]) {
int kk=tag[bl[x]];
if(kk>=L[bl[x]]||out[x][1]==1) kk=min(kk,out[x][0]);
if(kk>=L[bl[x]]) return -1;
if(kk==tag[bl[x]]) {
res++;
x=kk;
}else {
res+=out[x][1];
x=kk;
}
}
if(x<=y) return res;
while(x>y) {
int lst=x;
x=min(fir[x],tag[bl[x]]);
if(x==lst) return -1;
res++;
}
return res;
}
void solve() {
cin>>st;
for(auto to:st) insert(to-'a');
n=st.size();
init();
st=" "+st;
memset(mn,0x3f,sizeof mn);
jc[0]=1;
rep(i,1,n) {
jc[i]=jc[i-1]*ba;
ha[i]=(ha[i-1]*ba+st[i]-'a');
}
cin>>q;
dfs(1,0);
rep(j,1,19) rep(i,1,idx) f[i][j]=f[f[i][j-1]][j-1];
rep(i,1,q) {
int a,b,c,d,k;
cin>>a>>b>>c>>d>>k;
if(has(a,b)==has(c,d)&&b-a+1==d-c+1) {
ans[i]=1;
}else if(a<=b-k+1&&has(b-k+1,b)==has(c,c+k-1)&&c+k-1<=d) {
ans[i]=2;
}else {
int x=pos[b];
rep1(j,19,0) {
if(f[x][j]&&s[f[x][j]].ml>=k) {
x=f[x][j];
}
}
int y=pos[c+k-1];
rep1(j,19,0) {
if(f[y][j]&&s[f[y][j]].ml>=k) {
y=f[y][j];
}
}
qy[k].pb({mn[x],mx[y],i});
}
}
rep(i,1,idx) my[s[i].ml].pb(i);
bb=sqrt(n);
rep(i,1,n) bl[i]=(i-1)/bb+1;
rep(i,1,n) {
if(!L[bl[i]]) L[bl[i]]=i;
R[bl[i]]=i;
fir[i]=i;
out[i]={n+1,n+1};
tag[i]=n+1;
}
rep1(i,n,1) {
for(auto to:my[i]) {
modify(mn[to],mx[to],mn[to]);
}
for(auto to:qy[i]) {
int tt=bound(to[0],to[1]);
if(tt<0) ans[to[2]]=-1;
else ans[to[2]]=tt+3;
}
}
rep(i,1,q) print(ans[i]),putchar('\n');
}
fire main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(T--) {
solve();
}
return false;
}