#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
struct Node {
int a, b, id;
};
struct Seg {
int l, r, v;
Seg *ls, *rs;
void ch(int k, int v) {
if (l == r) {
this->v = v;
return;
}
if (k <= ls->r)
ls->ch(k, v);
else
rs->ch(k, v);
this->v = max(ls->v, rs->v);
}
int qry(int l, int r) const {
if (this->l == l && this->r == r)
return v;
if (r <= ls->r)
return ls->qry(l, r);
if (l >= rs->l)
return rs->qry(l, r);
return max(ls->qry(l, ls->r), rs->qry(rs->l, r));
}
};
const int Q = 100010, N = 2000010, LGN = 21;
int n;
char tmp[N];
int s[N], cnt[N], rk[N], sa[N], ht[N], id[N];
int lgt[N];
int pre[N], suf[N];
int st[N][LGN];
int ql[Q], qr[Q], sl[Q], sr[Q], cur[Q];
ll ans[Q];
Node p[N], q[N];
Seg* seg;
void ins();
int qry(int l, int r);
Seg* build(int l, int r, int v);
int main() {
int qc;
ins();
scanf("%d", &qc);
for (int i = 1; i <= qc; ++i) {
ins();
scanf("%d%d", &ql[i], &qr[i]);
}
for (int i = 1; i <= n; ++i)
cnt[s[i]] = 1;
for (int i = 1; i <= 26 + qc; ++i)
cnt[i] += cnt[i - 1];
for (int i = 1; i <= n; ++i)
rk[i] = cnt[s[i]];
for (int t = 1; t <= n; t <<= 1) {
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; ++i) {
p[i].id = i;
p[i].a = rk[i];
p[i].b = i + t > n ? 0 : rk[i + t];
}
for (int i = 1; i <= n; ++i)
++cnt[p[i].b];
for (int i = 1; i <= n; ++i)
cnt[i] += cnt[i - 1];
for (int i = n; i; --i)
q[cnt[p[i].b]--] = p[i];
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; ++i)
++cnt[q[i].a];
for (int i = 1; i <= n; ++i)
cnt[i] += cnt[i - 1];
for (int i = n; i; --i)
p[cnt[q[i].a]--] = q[i];
int c = 0;
for (int i = 1; i <= n; ++i) {
if (p[i].a != p[i - 1].a || p[i].b != p[i - 1].b)
++c;
rk[p[i].id] = c;
}
if (c == n)
break;
}
for (int i = 1; i <= n; ++i)
sa[rk[i]] = i;
int hh = 0;
for (int i = 1; i <= n; ++i) {
hh = max(hh - 1, 0);
while (s[i + hh] == s[sa[rk[i] + 1] + hh])
++hh;
ht[rk[i]] = hh;
}
for (int i = 1; i <= qc; ++i)
for (int j = sl[i]; j <= sr[i]; ++j)
id[rk[j]] = i;
for (int i = 2; i <= n; ++i)
lgt[i] = lgt[i >> 1] + 1;
for (int i = 1; i < n; ++i) {
st[i][0] = ht[i];
for (int j = 1; (1 << j) <= i; ++j)
st[i][j] = min(st[i][j - 1], st[i - (1 << (j - 1))][j - 1]);
}
seg = build(1, n, 0);
for (int i = 1; i <= n; ++i) {
if (sa[i] <= sr[0])
seg->ch(sa[i], i);
else if (id[i])
pre[i] = seg->qry(ql[id[i]], qr[id[i]]);
}
seg = build(1, n, -n - 1);
for (int i = n; i; --i) {
if (sa[i] <= sr[0])
seg->ch(sa[i], -i);
else if (id[i]) {
suf[i] = -seg->qry(ql[id[i]], qr[id[i]]);
if (suf[i] == n + 1)
suf[i] = 0;
}
}
for(int i = 1; i <= n; ++i) {
if (!id[i])
continue;
int lim = 0;
if (pre[i])
lim = max(lim, qry(pre[i], i));
if (suf[i])
lim = max(lim, qry(i, suf[i]));
if (cur[id[i]])
lim = max(lim, qry(cur[id[i]], i));
ans[id[i]] -= lim;
cur[id[i]] = i;
}
for (int i = 1; i <= qc; ++i)
printf("%lld\n", ans[i]);
return 0;
}
Seg* build(int l, int r, int v) {
static Seg pool[N * 4];
static Seg* ptop = pool;
Seg* p = ptop;
++ptop;
p->l = l;
p->r = r;
p->v = v;
if (l == r)
return p;
int mid = (l + r) / 2;
p->ls = build(l, mid, v);
p->rs = build(mid + 1, r, v);
return p;
}
int qry(int l, int r) {
--r;
int lv = lgt[r - l + 1];
return min(st[r][lv], st[l + (1 << lv) - 1][lv]);
}
void ins() {
static int id = 0;
if (!id)
s[++n] = id + 26;
scanf("%s", tmp);
sl[id] = n + 1;
for (char* c = tmp; *c; ++c)
s[++n] = *c - 'a' + 1;
sr[id] = n;
ans[id] = (sr[id] - sl[id] + 1) * (sr[id] - sl[id] + 2LL) / 2;
++id;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 9.47 ms | 17 MB + 216 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #2 | 10.009 ms | 17 MB + 468 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #3 | 10.916 ms | 17 MB + 844 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #4 | 135.735 ms | 130 MB + 668 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #5 | 133.339 ms | 127 MB + 76 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #6 | 968.627 ms | 263 MB + 800 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #7 | 970.661 ms | 263 MB + 800 KB | Accepted | Score: 4 | 显示更多 |
| Testcase #8 | 141.111 ms | 84 MB + 244 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #9 | 109.854 ms | 84 MB + 596 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #10 | 323.964 ms | 160 MB + 832 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #11 | 276.172 ms | 161 MB + 580 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 545.616 ms | 237 MB + 396 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #13 | 520.47 ms | 238 MB + 496 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #14 | 785.025 ms | 313 MB + 960 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #15 | 775.544 ms | 315 MB + 480 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 1.083 s | 390 MB + 572 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 1.014 s | 392 MB + 356 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 1.188 s | 313 MB + 620 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 1.413 s | 339 MB + 256 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #20 | 1.629 s | 364 MB + 932 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #21 | 1.858 s | 390 MB + 528 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #22 | 1.865 s | 390 MB + 576 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #23 | 1.862 s | 390 MB + 584 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #24 | 1.865 s | 390 MB + 564 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #25 | 1.855 s | 390 MB + 572 KB | Wrong Answer | Score: 0 | 显示更多 |