#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } //////////////////////////////////////////////////////////////////////////////// // SA-IS // String: string, vector, vector // if sigma <= n, O(n) time, O(n) space // if sigma > n, O(n log n) time, O(n) space template vector suffixArrayRec(const String &as) { const int n = as.size(); if (n == 0) return {}; const auto minmaxA = minmax_element(as.begin(), as.end()); const auto minA = *minmaxA.first, maxA = *minmaxA.second; if (static_cast(maxA) - minA >= static_cast(n)) { vector us(n); for (int u = 0; u < n; ++u) us[u] = u; std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (as[u] < as[v]); }); int b = 0; vector bs(n, 0); for (int i = 1; i < n; ++i) { if (as[us[i - 1]] != as[us[i]]) ++b; bs[us[i]] = b; } return suffixArrayRec(bs); } const int sigma = maxA - minA + 1; vector pt(sigma + 1, 0), poss(sigma); for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1]; for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a]; // cmp[u] := (as[u, n) < as[u + 1, n)) vector cmp(n); cmp[n - 1] = false; for (int u = n - 1; --u >= 0; ) { cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1]; } // ><, nn - id (0 <= id < n) int nn = 0; vector ids(n, 0); int last = n; vector nxt(n, 0); // put ><, from the tail of each bucket vector us(n, 0); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) { ids[u] = ++nn; nxt[u] = last; last = u; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } if (nn) { int vsLen = 0; vector vs(nn); for (const int u : us) if (ids[u]) vs[vsLen++] = u; int b = 0; vector bs(nn, 0); for (int j = 1; j < nn; ++j) { // as[v1, w1] (< or =) as[v0, w0] int v1 = vs[j - 1], v0 = vs[j]; const int w1 = nxt[v1], w0 = nxt[v0]; if (w1 - v1 != w0 - v0) { ++b; } else { for (; ; ++v1, ++v0) { if (v1 == n) { ++b; break; } if (as[v1] != as[v0]) { ++b; break; } if (v1 == w1) break; } } bs[nn - ids[vs[j]]] = b; } for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u; const auto sub = suffixArrayRec(bs); // put ><, from the tail of each bucket memset(us.data(), 0, n * sizeof(int)); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int j = nn; --j >= 0; ) { const int u = vs[sub[j]]; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } } return us; } // us[i]: i-th suffix // su[u]: index of as[u, n) // hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n) struct SuffixArray { int n; bool rmq; vector us, su, hs, bsr; SuffixArray() : n(0), rmq(false) {} SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector &as, bool rmq_) : rmq(rmq_) { build(as); } template void build(const String &as) { n = as.size(); us = suffixArrayRec(as); su.resize(n + 1); for (int i = 0; i < n; ++i) su[us[i]] = i; su[n] = -1; hs.assign(n, 0); for (int h = 0, u = 0; u < n; ++u) if (su[u]) { for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {} hs[su[u]] = h; if (h) --h; } if (rmq) { const int logN = n ? (31 - __builtin_clz(n)) : 0; hs.resize((logN + 1) * n - (1 << logN) + 1); for (int e = 0; e < logN; ++e) { int *hes = hs.data() + e * n; for (int i = 0; i <= n - (1 << (e + 1)); ++i) { hes[n + i] = min(hes[i], hes[i + (1 << e)]); } } bsr.resize(n + 1); bsr[0] = -1; for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1; } } // Returns longest common prefix of as[u, n) and as[v, n). // 0 <= u, v <= n // Assumes rmq. inline int lcp(int u, int v) const { if (u == v) return n - u; int i = su[u], j = su[v]; if (i > j) swap(i, j); const int e = bsr[j - i]; return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]); } }; //////////////////////////////////////////////////////////////////////////////// // root: min (tie-break: left) struct MinCartesianTree { int n, rt; vector par, lef, rig; template void build(int n_, T *as) { assert(n_ >= 1); n = n_; rt = 0; par.assign(n, -1); lef.assign(n, -1); rig.assign(n, -1); int top = 0; vector stack(n, 0); for (int u = 1; u < n; ++u) { if (as[stack[top]] > as[u]) { // > for (; top >= 1 && as[stack[top - 1]] > as[u]; --top) {} // > if (top == 0) { rt = par[lef[u] = stack[top]] = u; } else { par[lef[u] = stack[top]] = u; rig[par[u] = stack[top - 1]] = u; } stack[top] = u; } else { rig[par[u] = stack[top]] = u; stack[++top] = u; } } } template void build(const T &as) { build(as.size(), as.data()); } }; int N, Q; char S[200'010]; vector L, R; vector ans; SuffixArray sa; MinCartesianTree ct; vector>> qss(N); Int now; void dfs(int u, int l, int r, int d) { const int dd = (~u) ? sa.hs[u] : (N - sa.us[l]); // cerr<<"dfs "< "<(sa.hs.begin(), sa.hs.begin() + N)); // cerr<<"sa.hs = "<= len) ? hi : lo) = mid; } qss[hi].emplace_back(len, q); } for (int i = 0; i < N; ++i) { sort(qss[i].begin(), qss[i].end(), greater>{}); } // cerr<<"qss = "<