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
| #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define ROF(i, a, b) for (int i = b; i >= a; --i) #define SZ(x) ((int)x.size()) #define all(x,N) x.begin()+1,x.begin()+N+1 #define all1(x) x.begin(),x.end()
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; typedef array<ll,3> arr; typedef double DB; const ll MAXN=static_cast<ll>(1e5)+10; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f; } inline string reads() { string str=""; char ch=getchar(); while(ch==' ' || ch=='\n' || ch=='\r') { ch=getchar(); } while(ch!=' ' && ch!='\n' && ch!='\r') { str+=ch; ch=getchar(); } return str; } inline void write(int x){ if(x<0) x=-x,putchar('-'); if(x>9) write(x/10); putchar(x%10+'0'); } int N,T; int nex[MAXN*10][28]; int color[MAXN*10]; int tr[MAXN]; int tree[MAXN]; int idx; int Len[MAXN]; int Ans[MAXN]; string S[MAXN]; struct Query{ int l,r,id; bool operator<(const Query &other) const{ return r<other.r; } }; vector<Query> que(MAXN); inline int lowbit(int x){ return x&(-x); } inline void add2(int p,int x){ while (p<=N) { tree[p]+=x; p+=lowbit(p); } } inline int query2(int p){ int res=0; while (p>0) { res+=tree[p]; p-=lowbit(p); } return res; } inline void add(int p,int x){ while (p<=N) { tr[p]=x; for(int i=1;i<lowbit(p);i*=2){ tr[p]=max(tr[p],tr[p-i]); } p+=lowbit(p); } } inline int query(int l,int r){ int res=0; while (l<=r) { if(r-lowbit(r)<l){ res=max(Len[r],res); r-=1; }else { res=max(tr[r],res); r-=lowbit(r); } } return res; } inline void buildTree(string &s,int c){ int node=0; int len=SZ(s); FOR(i, 0, len-1){ if(nex[node][s[i]-'a']==0){ nex[node][s[i]-'a']=++idx; node=idx; add2(c,1); color[node]=c; }else{ node=nex[node][s[i]-'a'];
add2(color[node], -1); add2(c, 1);
color[node]=c; } } int begin=lower_bound(all(que,T),(Query){0,c,0})-que.begin(); int end=upper_bound(all(que,T),(Query){MAXN,c,MAXN})-que.begin(); FOR(j, begin, end-1){ int res=query2(que[j].r)-query2(que[j].l-1); Ans[que[j].id]=res*2-query(que[j].l, que[j].r); } }
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); N=read();T=read(); FOR(i, 1, N){ S[i]=reads(); Len[i]=SZ(S[i]); add(i,Len[i]); } FOR(i, 1, T){ que[i].l=read(); que[i].r=read(); que[i].id=i; } sort(all(que,T)); FOR(i, 1, N){ buildTree(S[i], i); } FOR(i, 1, T){ write(Ans[i]); putchar('\n'); } return 0; }
|