#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
namespace hsg {
mt19937_64 rng(chrono::steady_clock().now().time_since_epoch().count());
const long long mod = (long long)1e18+9;
long long R=rng()%(mod);
struct PolyHash {
vector<long long> pref,pw;
int n;
PolyHash(string s) {
n = s.size();
pref.resize(n+1);
pw.resize(n+1);
pw[0]=1;
for(int i = 1;i<=n;i++) {
pref[i] = ((__int128_t)pref[i-1]*R)%mod;
pref[i]+=s[i-1];
pref[i]%=mod;
pw[i] = ((__int128_t)pw[i-1]*R)%mod;
}
}
long long get(int l, int r) {
long long hs=(pref[r]-((__int128_t)pref[l-1]*pw[r-l+1])%mod + mod)%mod;
return hs;
}
};
};
const int LL = 3e5+10;
const int LG = 20;
int up[LL][LG];
int nxt[LL];
bool cmp(const string& A, const string &B) {
return A.size() < B.size();
};
int get(int l, int r) {
int at = l;
long long ans=1;
if(nxt[at] >= r)return 1;
for(int i = LG-1;i>=0;i--) {
if(nxt[up[at][i]] < r) {
at = up[at][i];
ans+=(1ll<<i);
}
}
if(nxt[up[at][0]] < r)return -1;
return ans+1;
}
int get2(int l, int r) {
int at = l;
int ans=1;
while(nxt[at] < r) {
if(at == up[at][0]) {
return -1;
}
at = up[at][0];
ans++;
}
return ans;
}
int main () {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
string s[n];
for(int i =0;i<n;i++) {
cin >> s[i];
}
sort(s, s+n, cmp);
string S;
cin >> S;
int L = S.size();
vector<pair<int,int>> inv;
memset(nxt, -1, sizeof nxt);
hsg::PolyHash hs(S);
vector<long long> mp[L+1];
for(int i = 0;i<n;i++) {
hsg::PolyHash hss(s[i]);
for(int j = 0 ;j<min(L, (int)s[i].size());j++) {
mp[j+1].push_back(hss.get(1, j+1));
}
}
for(int i = 0;i<=L;i++)sort(mp[i].begin(), mp[i].end());
for(int i = 0;i<L;i++)nxt[i]=i;
for(int i = 0;i<L;i++) {
nxt[i]=i;
int lo = 1, hi = L-i;
while(lo <= hi) {
int mid = (lo+hi)/2;
long long hsss = hs.get(i+1, i+mid);
auto it = lower_bound(mp[mid].begin(), mp[mid].end(), hsss);
if(it != mp[mid].end() and (*it) == hsss) {
nxt[i] = min(L, i+mid);
lo = mid+1;
}
else {
hi=mid-1;
}
}
}
{
vector<int> v[L+1];
for(int i= 0;i<L;i++) {
v[nxt[i]].push_back(i);
}
vector<int> stk;
for(int i = 0;i<=L;i++) {
while(stk.size() and nxt[stk.back()] <= nxt[i]) {
stk.pop_back();
}
stk.push_back(i);
for(int j:v[i]){
int idx = (*lower_bound(stk.begin(), stk.end(), j));
up[j][0] = idx;
}
}
for(int i = L-1;i>=0;i--) {
if(up[i][0] == 0)up[i][0] = i;
}
for(int i = L-1;i>=0;i--) {
for(int j = 1;j<LG;j++)up[i][j] = up[up[i][j-1]][j-1];
}
}
while(m--) {
int l, r;
cin >> l >> r;
int res = get(l, r);
cout << res << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
68 ms |
7764 KB |
Output is correct |
3 |
Correct |
71 ms |
8052 KB |
Output is correct |
4 |
Correct |
87 ms |
9556 KB |
Output is correct |
5 |
Correct |
74 ms |
9044 KB |
Output is correct |
6 |
Correct |
99 ms |
10320 KB |
Output is correct |
7 |
Correct |
67 ms |
6612 KB |
Output is correct |
8 |
Correct |
122 ms |
10580 KB |
Output is correct |
9 |
Correct |
75 ms |
10016 KB |
Output is correct |
10 |
Correct |
58 ms |
6228 KB |
Output is correct |
11 |
Correct |
47 ms |
9968 KB |
Output is correct |
12 |
Correct |
42 ms |
5604 KB |
Output is correct |
13 |
Correct |
63 ms |
7916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
54468 KB |
Output is correct |
2 |
Correct |
297 ms |
55636 KB |
Output is correct |
3 |
Correct |
284 ms |
54020 KB |
Output is correct |
4 |
Correct |
247 ms |
52484 KB |
Output is correct |
5 |
Correct |
253 ms |
52332 KB |
Output is correct |
6 |
Correct |
291 ms |
55352 KB |
Output is correct |
7 |
Correct |
320 ms |
56040 KB |
Output is correct |
8 |
Correct |
302 ms |
55380 KB |
Output is correct |
9 |
Correct |
304 ms |
55148 KB |
Output is correct |
10 |
Correct |
320 ms |
55888 KB |
Output is correct |
11 |
Correct |
285 ms |
54764 KB |
Output is correct |
12 |
Correct |
266 ms |
53600 KB |
Output is correct |
13 |
Correct |
296 ms |
57176 KB |
Output is correct |
14 |
Correct |
264 ms |
56044 KB |
Output is correct |
15 |
Correct |
195 ms |
51964 KB |
Output is correct |
16 |
Correct |
103 ms |
57700 KB |
Output is correct |
17 |
Correct |
94 ms |
55204 KB |
Output is correct |
18 |
Correct |
103 ms |
54864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
68 ms |
7764 KB |
Output is correct |
3 |
Correct |
71 ms |
8052 KB |
Output is correct |
4 |
Correct |
87 ms |
9556 KB |
Output is correct |
5 |
Correct |
74 ms |
9044 KB |
Output is correct |
6 |
Correct |
99 ms |
10320 KB |
Output is correct |
7 |
Correct |
67 ms |
6612 KB |
Output is correct |
8 |
Correct |
122 ms |
10580 KB |
Output is correct |
9 |
Correct |
75 ms |
10016 KB |
Output is correct |
10 |
Correct |
58 ms |
6228 KB |
Output is correct |
11 |
Correct |
47 ms |
9968 KB |
Output is correct |
12 |
Correct |
42 ms |
5604 KB |
Output is correct |
13 |
Correct |
63 ms |
7916 KB |
Output is correct |
14 |
Correct |
271 ms |
54468 KB |
Output is correct |
15 |
Correct |
297 ms |
55636 KB |
Output is correct |
16 |
Correct |
284 ms |
54020 KB |
Output is correct |
17 |
Correct |
247 ms |
52484 KB |
Output is correct |
18 |
Correct |
253 ms |
52332 KB |
Output is correct |
19 |
Correct |
291 ms |
55352 KB |
Output is correct |
20 |
Correct |
320 ms |
56040 KB |
Output is correct |
21 |
Correct |
302 ms |
55380 KB |
Output is correct |
22 |
Correct |
304 ms |
55148 KB |
Output is correct |
23 |
Correct |
320 ms |
55888 KB |
Output is correct |
24 |
Correct |
285 ms |
54764 KB |
Output is correct |
25 |
Correct |
266 ms |
53600 KB |
Output is correct |
26 |
Correct |
296 ms |
57176 KB |
Output is correct |
27 |
Correct |
264 ms |
56044 KB |
Output is correct |
28 |
Correct |
195 ms |
51964 KB |
Output is correct |
29 |
Correct |
103 ms |
57700 KB |
Output is correct |
30 |
Correct |
94 ms |
55204 KB |
Output is correct |
31 |
Correct |
103 ms |
54864 KB |
Output is correct |
32 |
Correct |
234 ms |
39408 KB |
Output is correct |
33 |
Correct |
304 ms |
42048 KB |
Output is correct |
34 |
Correct |
316 ms |
43060 KB |
Output is correct |
35 |
Correct |
329 ms |
42508 KB |
Output is correct |
36 |
Correct |
326 ms |
44028 KB |
Output is correct |
37 |
Correct |
259 ms |
39652 KB |
Output is correct |
38 |
Correct |
540 ms |
62396 KB |
Output is correct |
39 |
Correct |
445 ms |
59416 KB |
Output is correct |
40 |
Correct |
524 ms |
62880 KB |
Output is correct |
41 |
Correct |
486 ms |
62204 KB |
Output is correct |
42 |
Correct |
462 ms |
61676 KB |
Output is correct |
43 |
Correct |
234 ms |
27216 KB |
Output is correct |
44 |
Correct |
232 ms |
26656 KB |
Output is correct |
45 |
Correct |
226 ms |
26140 KB |
Output is correct |
46 |
Correct |
283 ms |
28344 KB |
Output is correct |
47 |
Correct |
412 ms |
58720 KB |
Output is correct |
48 |
Correct |
445 ms |
60308 KB |
Output is correct |
49 |
Correct |
386 ms |
58372 KB |
Output is correct |
50 |
Correct |
489 ms |
63144 KB |
Output is correct |
51 |
Correct |
191 ms |
58452 KB |
Output is correct |
52 |
Correct |
152 ms |
60184 KB |
Output is correct |
53 |
Correct |
169 ms |
58896 KB |
Output is correct |