题意
Sol
只会后缀数组+暴躁莫队套set\(n \sqrt{n} \log n\)但绝对跑不过去。
正解是SAM + set启发式合并 + 二维数点/ SAM + LCT
但是我只会第一种qwq
首先一个性质是两个前缀的最长公共后缀就是他们再parent树上的LCA的len
那么我们考虑每个LCA的贡献。
把询问离线下来按右端点排序,对于当前点的子树中的点有一个显然的性质。
若存在四个点\(l, x, y, r\)满足\(l < x < y < r\),那么显然\(l, r\)这对点是没有意义的(因为每对点产生的贡献都相同)。也就说我们在处理子树的时候实际上有一堆点对用不到。我们可以通过set启发式合并来合并子树,也就是说我现在有一堆点集,然后我考虑加入一个新点之后哪些点对会有用,显然只有它与它的前驱/后继这两个点对是有用的。
因为合并的时候是启发式合并,所以总复杂度不会超过\(n \log^2 n\)
然后处理完之后就是一个二维数点取max问题了。
调起来有点自闭qwq
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define pb push_back
//#define int long long
#define LL long long
#define ull unsigned long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, ans[MAXN];
char s[MAXN];
vector<Pair> Q[MAXN], P[MAXN];
vector<int> v[MAXN];
set<int> st[MAXN];
int ch[MAXN][2], len[MAXN], fa[MAXN], las = 1, root = 1, tot = 1;
void insert(int x, int id) {
int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1;
st[now].insert(id);
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[q] == len[pre] + 1) fa[now] = q;
else {
int nq = ++tot;
fa[nq] = fa[q]; len[nq] = len[pre] + 1;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
fa[now] = fa[q] = nq;
}
}
}
void dfs(int x) {
set<int> &S = st[x];
for(auto &to : v[x]) {
dfs(to);
set<int> &Sto = st[to];
if(Sto.size() > S.size()) swap(Sto, S);
for(auto &nxt: Sto) {
auto pos = S.insert(nxt).fi;
if(pos != S.begin()) {
auto pre = --pos; pos++;
// printf("%d %d %d\n", *pos, *pre, len[x]);
P[*pos].pb({*pre, len[x]});
}
if((++pos) != S.end()) {
pos--;
auto nxt = ++pos; pos--;
//printf("%d %d\n", *pos, *nxt);
P[*nxt].pb({*pos, len[x]});
}
S.erase(nxt);
}
for(auto &nxt: Sto) S.insert(nxt);
}
}
void Build() {
for(int i = 1; i <= tot; i++) v[fa[i]].push_back(i);
dfs(1);
for(int i = 1; i <= N; i++) sort(Q[i].begin(), Q[i].end()), sort(P[i].begin(), P[i].end());
}
int mx[MAXN];
#define lb(x) (x & (-x))
void Add(int x, int val) {
x = N - x + 1;
while(x <= N) chmax(mx[x], val), x += lb(x);
}
int Query(int x) {
x = N - x + 1;
int ans = 0;
while(x) chmax(ans, mx[x]), x -= lb(x);
return ans;
}
void solve() {
for(int i = 1; i <= N; i++) {
int cur = Q[i].size() - 1;
for(int j = P[i].size() - 1; j >= 0; j--) {
while((~cur) && Q[i][cur].fi > P[i][j].fi) ans[Q[i][cur].se] = Query(Q[i][cur].fi), cur--;
Add(P[i][j].fi, P[i][j].se);
}
while(~cur)
ans[Q[i][cur].se] = Query(Q[i][cur].fi), cur--;
}
}
signed main() {
//freopen("a.in", "r", stdin);
N = read(); M = read();
scanf("%s", s + 1);
for(int i = 1; i <= N; i++) insert(s[i] - '0', i);
for(int i = 1; i <= M; i++) {
int l = read(), r = read();
Q[r].pb({l, i});
}
Build();
solve();
for(int i = 1; i <= M; i++) cout << ans[i] << '\n';
return 0;
}