1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <bits/stdc++.h> #define ll long long #define db double #define PII pair <int, int> #define pb push_back #define fi first #define se second #define MP make_pair
using namespace std;
const int N = 1002000;
int n, qq, m, rk[N], sa[N], cnt[N], c[N]; int len[N], pos[N], h[N], st[N][25], lg2[N], ans[N]; char s[N]; string S; char A[N]; int Lp[N], Rp[N], tot;
struct node { int x, y, id; } a[N], b[N];
struct Query { int x, y, id, typ; bool operator < (const Query &t) const { return x == t.x ? y < t.y : x < t.x; } } Q[N * 4];
int lcp(int l, int r) { if(l > r) return -1; int k = lg2[r - l + 1]; return min(st[l][k], st[r - (1 << k) + 1][k]); }
int lb(int x) { return x & (-x); }
int add(int x, int d) { for(int i = x; i <= m; i += lb(i)) c[i] += d; }
int sum(int x) { int ret = 0; for(int i = x; i; i -= lb(i)) ret += c[i]; return ret; }
int main() { scanf("%d %d", &n, &qq); int nowlen = 1; for(int i = 1; i <= n; i++) { pos[i] = nowlen; scanf("%s", s + 1); len[i] = strlen(s + 1); for(int j = 1; j <= len[i]; j++) S += s[j]; nowlen += len[i] + 1; S += '#'; } m = S.length(); pos[n + 1] = m + 1; A[0] = '#'; for(int i = 0; i < m; i++) A[i + 1] = S[i]; for(int i = 1; i <= m; i++) cnt[A[i]]++; for(int i = 1; i <= 256; i++) cnt[i] += cnt[i - 1]; for(int i = 1; i <= m; i++) rk[i] = cnt[A[i]]; for(int L = 1; L <= m; L <<= 1) { for(int i = 1; i <= m; i++) { a[i].x = rk[i], a[i].y = rk[i + L]; a[i].id = i; } for(int i = 1; i <= m; i++) cnt[i] = 0; for(int i = 1; i <= m; i++) cnt[a[i].y]++; for(int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; for(int i = 1; i <= m; i++) b[cnt[a[i].y]--] = a[i]; for(int i = 1; i <= m; i++) cnt[i] = 0; for(int i = 1; i <= m; i++) cnt[b[i].x]++; for(int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; for(int i = m; i >= 1; i--) a[cnt[b[i].x]--] = b[i]; for(int i = 1; i <= m; i++) { if(a[i].x == a[i - 1].x && a[i].y == a[i - 1].y) rk[a[i].id] = rk[a[i - 1].id]; else rk[a[i].id] = rk[a[i - 1].id] + 1; } } for(int i = 2; i <= m; i++) lg2[i] = lg2[i >> 1] + 1; for(int i = 1; i <= m; i++) sa[rk[i]] = i; int k = 0; for(int i = 1; i <= m; i++) { if(k) k--; int j = sa[rk[i] - 1]; while(i + k <= m && j + k <= m && A[i + k] == A[j + k]) k++; h[rk[i]] = k; } for(int i = 1; i <= m; i++) st[i][0] = h[i]; for(int j = 1; (1 << j) <= m; j++) for(int i = 1; i + (1 << j) - 1 <= m; i++) st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); for(int i = 1; i <= n; i++) { int pp = pos[i]; int l = rk[pp] + 1, r = m; Lp[i] = Rp[i] = rk[pp]; while(l <= r) { int mid = (l + r) >> 1; if(lcp(rk[pp] + 1, mid) >= len[i]) { Rp[i] = mid; l = mid + 1; } else r = mid - 1; } l = 1, r = rk[pp]; while(l <= r) { int mid = (l + r) >> 1; if(lcp(mid + 1, rk[pp]) >= len[i]) { Lp[i] = mid; r = mid - 1; } else l = mid + 1; } } for(int i = 1; i <= qq; i++) { int l, r, k; scanf("%d %d %d", &l, &r, &k); Q[++tot] = {pos[l] - 1, Lp[k] - 1, i, 1}; Q[++tot] = {pos[r + 1] - 1, Lp[k] - 1, i, -1}; Q[++tot] = {pos[l] - 1, Rp[k], i, -1}; Q[++tot] = {pos[r + 1] - 1, Rp[k], i, 1}; } int P = 1; sort(Q + 1, Q + tot + 1); for(int i = 1; i <= tot; i++) { while(P <= Q[i].x && P <= m) { add(rk[P], 1); P++; } ans[Q[i].id] += Q[i].typ * sum(Q[i].y); } for(int i = 1; i <= qq; i++) printf("%d\n", ans[i]); return 0; }
|