题解:P6152 [集训队作业2018] 后缀树节点数
chenxinuo1008 · · 题解
include <bits/stdc++.h>
define gc getchar
define ll long long
using namespace std; int rd() { int x = 0, f = 1; char c = gc(); while(c < '0' || c > '9') { if(c == '-') f = (- 1); c = gc(); } while(c >= '0' && c <= '9') { x = x 10 + (c - '0'); c = gc(); } return (x f); } const int N = 100000, M = 300000; const ll Base = 23571113; const ll P = (((ll)1) << 61) - 1; inline ll Add(ll x, ll y) { return x + y >= P ? x + y - P : x + y; } inline ll Sub(ll x, ll y) { return x < y ? x + P - y : x - y; } inline ll Mod(int128 x) { return Sub((x & P) + (x >> 61), P); } // ? inline ll Mul(int128 x, ll y) { return Mod(x y); } // ? int n, m; int s[N + 1]; ll pw[N + 1], hsh[N + 1]; inline ll Hsh(int l, int r) { return Sub(hsh[r], Mul(hsh[l - 1], pw[r - l + 1])); } unordered_map < ll, int > h; // ? struct Qry { int l, id; Qry(): l(0), id(0){} Qry(int L, int Id): l(L), id(Id){} }; vector < vector < Qry > > qry(N + 1); // (N + 1) int ans[M + 1]; struct Mdf { int x, c; Mdf(): x(0), c(0){} Mdf(int X, int C): x(X), c(C){} }; vector < vector < Mdf > > mdf(N + 1); // (N + 1) int lst[N << 1]; namespace BIT{ int sum[N + 1]; void Add(int p, int v) { for(; p <= n; p += (p & -p)) sum[p] += v; } int Sum(int p) { int res = 0; for(; p >= 1; p -= (p & -p)) res += sum[p]; return res; } } namespace SAM { int tot, lst, len[N << 1], fa[N << 1]; map < int, int > ch[N << 1]; // ? int cstpos[N << 1], pos[N << 1], ord[N << 1], buc[N + 1]; vector < vector < int > > g(N << 1); set < int > st[N << 1]; int siz[N << 1], son[N << 1]; void Init() { tot = lst = 1; } void Ext(int c) { int p = lst, np = ++ tot; lst = np; len[np] = len[p] + 1; // while(p && ch[p][c] == 0) ch[p][c] = np, p = fa[p]; // p = fa[p] if(! p) fa[np] = 1; else { int q = ch[p][c]; if(len[q] == len[p] + 1) fa[np] = q; else { int nq = ++ tot; len[nq] = len[p] + 1; fa[nq] = fa[q]; ch[nq] = ch[q]; fa[q] = fa[np] = nq; while(p && ch[p][c] == q) ch[p][c] = nq, p = fa[p]; // p = fa[p] } } } int Nxt(int u, int p) { auto it = st[u].upper_bound(p); if(it != st[u].end()) return (it); else return 0; } int Lst(int u, int p) { auto it = st[u].lower_bound(p); if(it == st[u].begin()) return 0; // it --; // return (*it); } void init_dfs(int u) { siz[u] = 1; for(int v : g[u]) { init_dfs(v); siz[u] += siz[v]; if(siz[v] > siz[son[u]]) son[u] = v; } } void dfs(int u) { if(! son[u]) { if(cstpos[u]) st[u].insert(cstpos[u]); } else { dfs(son[u]); swap(st[u], st[son[u]]); // ??? for(int v : g[u]) { if(v == son[u]) continue; dfs(v); for(int p : st[v]) { // ? int x = Nxt(u, p); if(x > 0) mdf[x].emplace_back(Mdf(p - len[u], u)); x = Lst(u, p); if(x > 0) mdf[p].emplace_back(Mdf(x - len[u], u)); } for(int p : st[v]) st[u].insert(p); // ? } if(cstpos[u]) { int p = cstpos[u]; int x = Nxt(u, p); if(x > 0) mdf[x].emplace_back(Mdf(p - len[u], u)); x = Lst(u, p); if(x > 0) mdf[p].emplace_back(Mdf(x - len[u], u)); st[u].insert(p); } } } void Bd() { for(int u = 1; u <= tot; ++ u) ++ buc[len[u]]; for(int i = 1; i <= n; ++ i) buc[i] += buc[i - 1]; for(int u = tot; u >= 1; -- u) ord[buc[len[u]] --] = u; for(int u = 1, i = 1; i <= n; ++ i) { u = ch[u][s[i]]; cstpos[u] = pos[u] = i; } for(int i = tot; i >= 2; -- i) { int u = ord[i]; pos[fa[u]] = pos[u]; } for(int u = 2; u <= tot; ++ u) h[Hsh(pos[u] - len[u] + 1, pos[u])] = u; // l,r 不要搞反了 for(int u = 2; u <= tot; ++ u) g[fa[u]].emplace_back(u); init_dfs(1); for(int u : g[1]) dfs(u); // 1(根)不能被纳入考虑 } } int main() { n = rd(); m = rd(); for(int i = 1; i <= n; ++ i) { s[i] = rd(); ++ s[i]; } // ++ s[i],防止字符串哈希前导 0 的问题 reverse(s + 1, s + n + 1); pw[0] = 1; for(int i = 1; i <= n; ++ i) pw[i] = Mul(pw[i - 1], Base); hsh[0] = 0; for(int i = 1; i <= n; ++ i) hsh[i] = Add(Mul(hsh[i - 1], Base), s[i]); for(int i = 1; i <= m; ++ i) { int l = rd(); int r = rd(); l = n - l + 1; r = n - r + 1; swap(l, r); qry[r].emplace_back(Qry(l, i)); } SAM::Init(); for(int i = 1; i <= n; ++ i) SAM::Ext(s[i]); SAM::Bd(); for(int i = 1; i <= n; ++ i) { for(Mdf j : mdf[i]) { if(lst[j.c] >= j.x) continue; BIT::Add(lst[j.c] + 1, 1); BIT::Add(j.x + 1, - 1); lst[j.c] = j.x; } for(Qry j : qry[i]) { ans[j.id] = i - j.l + 1 + (BIT::Sum(j.l)); int l = j.l, r = i, res = j.l - 1; while(l <= r) { int mid = ((l + r) >> 1); int u = h[Hsh(j.l, mid)]; if(! u || lst[u] < j.l) r = mid - 1; // || ((int)(SAM::g[u].size())) <= 1 ? else { l = mid + 1; res = mid; } } ans[j.id] -= (res - j.l + 1); } } for(int i = 1; i <= m; ++ i) printf("%d\n", ans[i]); return 0; } // SAM 开一车 map 可以吗? // 哈希表用 unordered_map 可以吗? // set 启发式合并的空间咋办?开一车 set 可以吗? // set 初始时是空的? // 不要忘了预处理哈希。 // 哈希里某些位为 0 可以吗?似乎不行,因为前导 0 的问题。小心。 // swap set 复杂度? // 不要忘了写启发式合并(不要写成暴力合并了)。