[HEOI2016/TJOI2016]字符串 [SAM,线段树合并]
难调死了
// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); }
Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN {
char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 0;
while ((ch = GETC()) < 48) sign ^= (ch == 45);
x = (ch ^ 48);
while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48);
x = sign ? -x : x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 1 << 22;
char quq[SZ], ST[233];
int sz, O;
~FILEOUT() { flush(); }
void flush() { fwrite(quq, 1, O, stdout), fflush(stdout), O = 0; }
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 45;
x = -x;
}
do {
ST[++sz] = x % 10 ^ 48;
x /= 10;
} while (x);
while (sz) quq[O++] = ST[sz--];
}
FILEOUT& operator<<(int x) { return write(x), *this; }
} out;
int n, m;
const int maxn = 2e5 + 52;
char s[maxn];
int pos[maxn];
int rt[maxn], f[maxn][22];
struct SMT {
int cnt, ls[maxn << 6], rs[maxn << 6], sum[maxn << 6];
SMT() { cnt = 0; }
void pushup(int p) { sum[p] = sum[ls[p]] + sum[rs[p]]; }
void upd(int& p, int l, int r, int x) {
if (!p) p = ++cnt;
if (l == r) {
sum[p]++;
return;
}
int mid = l + r >> 1;
if (x <= mid)
upd(ls[p], l, mid, x);
else
upd(rs[p], mid + 1, r, x);
pushup(p);
}
int merge(int x, int y, int l, int r) {
if (!x || !y) return x | y;
if (l == r) {
sum[x] += sum[y];
return x;
}
int mid = l + r >> 1, p = ++cnt;
ls[p] = merge(ls[x], ls[y], l, mid);
rs[p] = merge(rs[x], rs[y], mid + 1, r);
pushup(p);
return p;
}
int qry(int p, int a, int b, int l, int r) {
if (!p) return 0;
if (a <= l && r <= b) return sum[p];
int mid = l + r >> 1, ans = 0;
if (a <= mid) ans += qry(ls[p], a, b, l, mid);
if (b > mid) ans += qry(rs[p], a, b, mid + 1, r);
return ans;
}
} smt;
struct SAM {
int las, cnt, ch[maxn][26], len[maxn], fa[maxn];
SAM() { las = cnt = 1; }
void ins(int c, int id) {
int p = las, np = las = ++cnt;
len[np] = len[p] + 1, pos[id] = np;
for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
if (!p) {
fa[np] = 1;
} else {
int q = ch[p][c];
if (len[q] == len[p] + 1) {
fa[np] = q;
} else {
int nq = ++cnt;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;
for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
}
}
}
int buc[maxn], rk[maxn];
void build() {
rep(i, 1, cnt) buc[len[i]]++;
rep(i, 1, cnt) buc[i] += buc[i - 1];
rep(i, 1, cnt) rk[buc[len[i]]--] = i;
rep(i, 1, n) smt.upd(rt[pos[i]], 1, n, i);
Rep(i, cnt, 1) if (fa[rk[i]]) rt[fa[rk[i]]] = smt.merge(rt[fa[rk[i]]], rt[rk[i]], 1, n);
rep(i, 1, cnt) f[i][0] = fa[i];
rep(j, 1, 20) rep(i, 1, cnt) f[i][j] = f[f[i][j - 1]][j - 1];
}
} sam;
bool chk(int mid, int x, int l, int r) {
Rep(i, 20, 0) if (f[x][i] && sam.len[f[x][i]] >= mid) x = f[x][i];
return smt.qry(rt[x], l + mid - 1, r, 1, n);
}
int qry(int a, int b, int c, int d) {
int l = 0, r = min(b - a + 1, d - c + 1), ans = 0;
while (l <= r) {
int mid = l + r >> 1;
if (chk(mid, pos[d], a, b))
l = (ans = mid) + 1;
else
r = mid - 1;
}
return ans;
}
signed main() {
// code begin.
in >> n >> m;
rep(i, 1, n) { in >> s[i]; }
Rep(i, n, 1) { sam.ins(s[i] - 'a', n - i + 1); }
sam.build();
while (m--) {
int a, b, c, d;
in >> a >> b >> c >> d;
a = n - a + 1, b = n - b + 1, c = n - c + 1, d = n - d + 1;
a ^= b ^= a ^= b, c ^= d ^= c ^= d;
out << qry(a, b, c, d) << '\n';
}
return 0;
// code end.
}