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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| #include <bits/stdc++.h> using namespace std;
const int N = 1e5 + 10; const int M = 23; int n, q; char s[N]; struct DA { int t1[N], t2[N], c[N]; int sa[N]; int Rank[N]; int height[N]; int str[N]; int n, m; void init(char *s, int m, int n) { this->m = m; this->n = n; for (int i = 0; i < n; ++i) str[i] = s[i]; str[n] = 0; } bool cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void work() { ++n; int i, j, p, *x = t1, *y = t2; for (i = 0; i < m; ++i) c[i] = 0; for (i = 0; i < n; ++i) c[x[i] = str[i]]++; for (i = 1; i < m; ++i) c[i] += c[i - 1]; for (i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i; for (j = 1; j <= n; j <<= 1) { p = 0; for (i = n - j; i < n; ++i) { y[p++] = i; } for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < m; ++i) c[i] = 0; for (i = 0; i < n; ++i) c[x[y[i]]]++; for (i = 1; i < m; ++i) c[i] += c[i - 1]; for (i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for (i = 1; i < n; ++i) { x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } if (p >= n) break; m = p; } int k = 0; --n; for (i = 0; i <= n; ++i) Rank[sa[i]] = i; for (i = 0; i < n; ++i) { if (k) --k; j = sa[Rank[i] - 1]; while (str[i + k] == str[j + k]) ++k; height[Rank[i]] = k; } } struct RMQ { int Min[N][M]; int mm[N]; void init(int n, int *b) { mm[0] = -1; for (int i = 1; i <= n; ++i) { mm[i] = ((i & (i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1]; Min[i][0] = b[i]; } for (int j = 1; j <= mm[n]; ++j) { for (int i = 1; i + (1 << j) - 1 <= n; ++i) { Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]); } } } int queryMin(int x, int y) { int k = mm[y - x + 1]; return min(Min[x][k], Min[y - (1 << k) + 1][k]); } }rmq; void initrmq() { rmq.init(n, height); } int lcp(int x, int y) { if (x == y) return 1e9; if (x > y) swap(x, y); ++x; return rmq.queryMin(x, y); } }da;
int getl(int x, int len) { int l = 1, r = x, res = x; while (r - l >= 0) { int mid = (l + r) >> 1; if (da.lcp(x, mid) >= len) { res = mid; r = mid - 1; } else { l = mid + 1; } } return res; }
int getr(int x, int len) { int l = x, r = n, res = x; while (r - l >= 0) { int mid = (l + r) >> 1; if (da.lcp(x, mid) >= len) { res = mid; l = mid + 1; } else { r = mid - 1; } } return res; }
struct SEG { struct node { int sum, ls, rs; node() { ls = rs = sum = 0; } }t[N * 50]; int rt[N], cnt; void init() { cnt = 0; memset(rt, 0, sizeof rt); } void build(int &id, int l, int r) { id = ++cnt; t[id] = node(); if (l == r) return; int mid = (l + r) >> 1; build(t[id].ls, l, mid); build(t[id].rs, mid + 1, r); } void up(int id) { int ls = t[id].ls, rs = t[id].rs; t[id].sum = 0; if (ls) t[id].sum += t[ls].sum; if (rs) t[id].sum += t[rs].sum; } void update(int &now, int pre, int l, int r, int pos) { now = ++cnt; t[now] = t[pre]; if (l == r) { ++t[now].sum; return; } int mid = (l + r) >> 1; if (pos <= mid) update(t[now].ls, t[pre].ls, l, mid, pos); else update(t[now].rs, t[pre].rs, mid + 1, r, pos); up(now); } int query(int tl, int tr, int l, int r, int k) { if (l == r) return l; int mid = (l + r) >> 1; int lsum = t[t[tr].ls].sum - t[t[tl].ls].sum; if (lsum >= k) { return query(t[tl].ls, t[tr].ls, l, mid, k); } else { return query(t[tl].rs, t[tr].rs, mid + 1, r, k - lsum); } } }seg;
int main() { int _T; scanf("%d", &_T); while (_T--) { scanf("%d%d", &n, &q); scanf("%s", s); da.init(s, 220, n); da.work(); da.initrmq(); seg.init(); seg.build(seg.rt[0], 1, n); for (int i = 1; i <= n; ++i) seg.update(seg.rt[i], seg.rt[i - 1], 1, n, da.sa[i] + 1); for (int i = 1, x, l, r, k, len; i <= q; ++i) { scanf("%d%d%d", &l, &r, &k); len = r - l + 1; x = da.Rank[l - 1]; int L = getl(x, len); int R = getr(x, len); if (R - L + 1 < k) puts("-1"); else printf("%d\n", seg.query(seg.rt[L - 1], seg.rt[R], 1, n, k)); } } return 0; }
|