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
| #include <bits/stdc++.h>
using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); typedef long long LL; const int N = 2e5 + 10; const int M = 1e6 + 10; const int INF = 0x3f3f3f3f; const LL LINF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-3; char s[N]; int n, m; int Next[N]; void getNext() { memset(Next, 0, sizeof Next); for (int i = 2, j = 0; i <= n; i ++) { while(j && s[i] != s[j + 1]) j = Next[j]; if(s[i] == s[j + 1]) j++; Next[i] = j; } }
int f1[N], ne1[N], v1[N]; int f2[N], ne2[N], v2[N]; int dfn1[N], siz1[N], dfs_idx1; int dfn2[N], siz2[N], dfs_idx2;
void dfs1(int u) { dfn1[u] = ++dfs_idx1; siz1[u] = 1; for (int i = f1[u]; i; i = ne1[i]) { dfs1(v1[i]); siz1[u] += siz1[v1[i]]; } }
void dfs2(int u) { dfn2[u] = ++dfs_idx2; siz2[u] = 1; for (int i = f2[u]; i; i = ne2[i]) { dfs2(v2[i]); siz2[u] += siz2[v2[i]]; } }
struct node { int x, y, res, c, id, w; bool operator < (const node &p) const { if(x != p.x) return x < p.x; if(y != p.y) return y < p.y; return w > p.w; } }a[M], x[M]; int ans[N];
void merge_sort(int l, int r) { if(l >= r) return; int mid = l + r >> 1; merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = l; long long sum = 0; while(i <= mid && j <= r) { if(a[i].y <= a[j].y) sum += a[i].w, x[k++] = a[i++]; else a[j].res += sum, x[k++] = a[j++]; } while(i <= mid) sum += a[i].w, x[k++] = a[i++]; while(j <= r) a[j].res += sum, x[k++] = a[j++]; for (i = l; i <= r; i++) a[i] = x[i]; }
void solve(){ dfs_idx1 = dfs_idx2 = 0; scanf("%d%d", &n, &m); scanf("%s", s + 1); getNext(); int idx = 0; for (int i = 1; i <= n; i++) v1[++idx] = i, ne1[idx] = f1[Next[i]], f1[Next[i]] = idx; reverse(s + 1, s + n + 1); getNext(); idx = 0; for (int i = 1; i <= n; i++) v2[++idx] = n - i, ne2[idx] = f2[n - Next[i]], f2[n - Next[i]] = idx; dfs1(0); dfs2(n); idx = 0; for (int i = 1; i <= n; i++) { int x = dfn1[i], y = dfn2[i]; a[++idx] = {x, y, 0, 0, 0, 1}; } for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); int l1, r1, l2, r2; l1 = dfn1[x], l2 = dfn1[x] + siz1[x] - 1; r1 = dfn2[n - y], r2 = dfn2[n - y] + siz2[n - y] - 1; a[++idx] = {l2, r2, 0, 1, i, 0}; a[++idx] = {l1 - 1, r2, 0, -1, i, 0}; a[++idx] = {l2, r1 - 1, 0, -1, i, 0}; a[++idx] = {l1 - 1, r1 - 1, 0, 1, i, 0}; } sort(a + 1, a + idx + 1); merge_sort(1, idx); for (int i = 1; i <= idx; i++) { ans[a[i].id] += a[i].c * a[i].res; } for (int i = 1; i <= m; i++) { printf("%d\n", ans[i]); ans[i] = 0; } for (int i = 0; i <= n; i++) { f1[i] = f2[i] = 0; } } int main(){ int t; cin >> t; while(t--){ solve(); } return 0; }
|