/*pragram by mangoyang*/
#include<cstdio>
#include<cstring>
#include<vector>
#include<ctype.h>
#include<algorithm>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
int buff[40];
template<class M> inline void putd(M x){
int p = 0;
if(x < 0){ putchar('-'); x = -x; }
do{
buff[p++] = x % 10;
x /= 10;
}while(x);
for(int i = p - 1; i >= 0; i--) putchar(buff[i] + '0');
putchar('\n');
}
const int N = 3500005, MAXN = 1500005;
char s[N]; int rt[N], buf[N], n;
struct SegmentTree{
int lc[MAXN*25], rc[MAXN*25], sz[MAXN*25], size;
inline SegmentTree(){ size = 1; }
inline void ins(int &u, int l, int r, int pos){
if(!u) u = ++size;
if(l == r) return (void) (sz[u]++);
int mid = l + r >> 1;
if(pos <= mid) ins(lc[u], l, mid, pos);
else ins(rc[u], mid + 1, r, pos);
sz[u] = sz[lc[u]] + sz[rc[u]];
}
inline int merge(int x, int y, int l, int r){
if(!x || !y) return x + y;
int mid = l + r >> 1, o = ++size;
if(l == r) sz[o] = sz[x] + sz[y];
else{
lc[o] = merge(lc[x], lc[y], l, mid);
rc[o] = merge(rc[x], rc[y], mid + 1, r);
sz[o] = sz[lc[o]] + sz[rc[o]];
}
return o;
}
inline int query(int u, int l, int r, int pos){
if(!sz[u]) return 0;
if(l == r) return l;
int mid = l + r >> 1;
if(pos <= mid) return query(lc[u], l, mid, pos);
int rans = query(rc[u], mid + 1, r, pos);
return rans ? rans : query(lc[u], l, mid, pos);
}
}Seg;
struct SuffixAutomaton{
vector<int> g[N], v; ll dep[N];
int ch[N][26], fa[N], vis[N], mx[N], mn[N], tail, size;
inline SuffixAutomaton(){ size = tail = 1, rt[1] = 1; }
inline int newnode(int x){ return dep[++size] = x, size; }
inline void ins(int c, int ff, int pos){
int p = tail, np = newnode(dep[p] + 1);
if(ff) v.push_back(np); else{
Seg.ins(rt[np], 1, n, pos);
mx[np] = mn[np] = pos;
}
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
if(!p) return (void) (fa[np] = 1, tail = np);
int q = ch[p][c];
if(dep[q] == dep[p] + 1) fa[np] = q;
else{
int nq = newnode(dep[p] + 1);
fa[nq] = fa[q], fa[np] = fa[q] = nq;
if(ff) rt[nq] = rt[q], mx[nq] = mx[q], mn[nq] = mn[q];
for(int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
for(; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
}tail = np;
}
inline void addedge(){
for(int i = 2; i <= size; i++) g[fa[i]].push_back(i);
}
inline void dfs(int u){
for(int i = 0; i < g[u].size(); i++){
int v = g[u][i];
dfs(v), rt[u] = Seg.merge(rt[u], rt[v], 1, n);
mx[u] = Max(mx[u], mx[v]), mn[u] = Min(mn[u], mn[v]);
}
}
inline void prepare(char *s){
for(int i = 0; i < n; i++) ins(s[i] - 'a', 0, i + 1);
addedge(), dfs(1);
}
inline ll calc(char *s, int l, int r){
tail = 1; v.clear();
ll len = strlen(s), ans = 0, all = 0;
for(int i = 0; i < len; i++) ins(s[i] - 'a', 1, 0);
for(int i = 0; i < v.size(); i++){
int u = v[i];
for(int p = u; p > 1; p = fa[p]) {
if(vis[p]) break; int OK = 0;
all += dep[p] - dep[fa[p]], vis[p] = 1;
if(rt[p]){
if((l == 1 && r == n) || OK)
{ ans += dep[p] - dep[fa[p]]; continue; }
if(mx[p] < l || mn[p] > r) continue;
int mxlen = mx[p] <= r ? mx[p] - l + 1 : Seg.query(rt[p], 1, n, r) - l + 1;
if(mxlen > dep[fa[p]])
ans += Min(dep[p], mxlen) - dep[fa[p]];
if(mxlen >= dep[p]) OK = 1;
}
}
}
for(int i = 0; i < v.size(); i++){
int u = v[i];
for(int p = u; p > 1; p = fa[p]){
if(!vis[p]) break; vis[p] = 0;
}
}
return all - ans;
}
}van;
int main(){
scanf("%s", s); n = strlen(s);
int Q; read(Q), van.prepare(s);
while(Q--){
int l, r;
scanf("%s", s), read(l), read(r);
putd(van.calc(s, l, r));
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 18.025 ms | 87 MB + 700 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 18.069 ms | 88 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 18.386 ms | 88 MB + 368 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 135.213 ms | 178 MB + 544 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 131.09 ms | 175 MB + 656 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 605.904 ms | 509 MB + 252 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 606.15 ms | 509 MB + 264 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 173.151 ms | 170 MB + 588 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 112.205 ms | 167 MB + 300 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 382.212 ms | 262 MB + 532 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 251.789 ms | 262 MB + 848 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 660.777 ms | 359 MB + 124 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 415.806 ms | 359 MB + 780 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 908.479 ms | 457 MB + 788 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 595.116 ms | 456 MB + 776 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 1.169 s | 556 MB + 748 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 780.939 ms | 551 MB + 612 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 1.301 s | 363 MB + 76 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 1.4 s | 426 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 1.684 s | 491 MB + 156 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 1.657 s | 556 MB + 728 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 1.976 s | 556 MB + 924 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 1.933 s | 556 MB + 468 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 1.756 s | 557 MB + 28 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 1.639 s | 556 MB + 308 KB | Accepted | Score: 4 | 显示更多 |