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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "bitset" #include "queue" using namespace std; #define MAXN 100006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define chkmn( a , b ) ( (a) = ( (a) < (b) ? (a) : (b) ) ) #define chkmx( a , b ) ( (a) = ( (a) > (b) ? (a) : (b) ) ) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) #define P 998244353 typedef long long ll; int n , q , L , blo;
char ch[MAXN]; int len[MAXN];
int son[MAXN][26] , cnt , bac[MAXN]; vi ns[MAXN]; void ins( char* s , int id ) { int len = strlen( s ) , cur = 0; rep( i , 0 , len - 1 ) { int v = s[i] - 'a'; if( !son[cur][v] ) son[cur][v] = ++ cnt; cur = son[cur][v] , ns[id].pb( cur ); } bac[id] = cur; } queue<int> Q; int fail[MAXN]; void build( ) { int u; rep( i , 0 , 25 ) if( son[0][i] ) Q.push( son[0][i] ); while( !Q.empty( ) ) { u = Q.front(); Q.pop(); rep( i , 0 , 25 ) { if( son[u][i] ) fail[son[u][i]] = son[fail[u]][i] , Q.push( son[u][i] ); else son[u][i] = son[fail[u]][i]; } } }
vi G[MAXN]; void addall( ) { rep( i , 1 , cnt ) G[fail[i]].pb( i ); }
int dfn[MAXN] , R[MAXN] , clo; void dfs( int u ) { dfn[u] = ++ clo; for( int& v : G[u] ) dfs( v ); R[u] = clo; }
int cn[MAXN]; void work( int u ) { for( int& v : G[u] ) work( v ) , cn[u] += cn[v]; }
int BLO; namespace kuai { int T[MAXN] , lz[500]; void add( int x , int c ) { if( !x ) return; int w = ( x - 1 ) / BLO; per( i , x , w * BLO + 1 ) T[i] += c; rep( i , 1 , w ) lz[i] += c; } int get( int x ) { return T[x] + lz[( x - 1 ) / BLO + 1]; } }
struct orzzh { int l , r , bp; }; vector<orzzh> que[MAXN] , quel[MAXN];
long long S[MAXN] , ans[MAXN]; void solve() { cin >> n >> q; rep( i , 1 , n ) { scanf("%s",ch); ins( ch , i ); len[i] = strlen( ch ); L += len[i]; } blo = sqrt( L ); BLO = sqrt( cnt ); build( ); int l , r , k; rep( i , 1 , q ) { scanf("%d%d%d",&l,&r,&k); if( len[k] > blo ) { que[k].eb( (orzzh) { l , r , i } ); } else { quel[l - 1].eb( (orzzh) { k , -1 , i } ); quel[r].eb( (orzzh) { k , 1 , i } ); } } addall( ); rep( i , 1 , n ) if( !que[i].empty() ) { rep( j , 1 , cnt ) cn[j] = 0 , S[j] = 0; for( int x : ns[i] ) ++ cn[x]; work( 0 ); rep( j , 1 , n ) S[j] = S[j - 1] + cn[bac[j]]; for( auto& t : que[i] ) ans[t.bp] += S[t.r] - S[t.l - 1]; } dfs( 0 ); rep( i , 1 , n ) { kuai::add( R[bac[i]] , 1 ) , kuai::add( dfn[bac[i]] - 1 , -1 );
for( auto& t : quel[i] ) { long long s = 0; for( int& x : ns[t.l] ) s += kuai::get( dfn[x] ); ans[t.bp] += t.r * s; } } rep( i , 1 , q ) printf("%lld\n",ans[i]); }
signed main() {
solve(); }
|