#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//#pragma GCC optimize("O3")
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
ll mod = 998244353, pr = 69;
const int mxn = 3e5 + 5, mxq = 1e5 + 5, sq = 1005, mxv = 5e4 + 1, lg = 60;
//const int base = (1 <<18);
const ll inf = 2e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rr(ll l, ll r){
return(uniform_int_distribution<ll>(l, r)(rng));
}
// have fun!
struct Hash{
vt<ll>hsh, pw;
void init(string s){
pw.resize(sz(s) + 1, 0); hsh.resize(sz(s) + 1, 0);
for(int i = 0; i < sz(s); i++){
hsh[i + 1] = (hsh[i] * pr + (s[i] - 'a' + 1)) % mod;
}
pw[0] = 1;
for(int i = 1; i <= sz(s); i++){
pw[i] = (pw[i - 1] * pr) % mod;
}
}
ll get(int l, int r){
return((hsh[r + 1] - hsh[l] * pw[r - l + 1] + mod * mod) % mod);
}
};
Hash ht[mxn + 1], hs;
int n, m;
string t[mxn + 1], s;
pii get(int ids, int idt){
int l = 1, r = min(sz(s) - ids, sz(t[idt])), ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(hs.get(ids, ids + mid - 1) == ht[idt].hsh[mid]){
ans = mid; l = mid + 1;
}else{
r = mid - 1;
}
}
if(ans == min(sz(s) - ids, sz(t[idt]))){
return(mpp(ans, sz(s) - ids < sz(t[idt])));
}else{
return(mpp(ans, s[ids + ans] < t[idt][ans]));
}
}
int up[mxn + 1][19];
pii mx[mxn + 1][19];
int getmx(int l, int r){
int lg = __lg(r - l + 1);
return(max(mx[l][lg], mx[r - (1 << lg) + 1][lg]).se);
}
void solve(){
cin >> n >> m;
mod = rr(1e8, 1e9);
for(int i = 1; i <= n; i++){
cin >> t[i];
}
sort(t + 1, t + n + 1);
for(int i = 1; i <= n; i++)ht[i].init(t[i]);
cin >> s; hs.init(s);
for(int i = 0; i < sz(s); i++){
int l = 1, r = n, ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
pii cand = get(i, mid);
ans = max(ans, cand.fi);
if(cand.se == 0){
l = mid + 1;
}else{
r = mid - 1;
}
}
up[i][0] = i + ans;
mx[i][0] = mpp(up[i][0], i);
}
for(int i = 1; i < 19; i++){
for(int j = 0; j + (1 << i) - 1 < sz(s); j++){
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
}
}
up[sz(s)][0] = sz(s);
for(int i = 1; i < 19; i++){
deque<int>dq;
for(int j = sz(s); j >= 0; j--){
while(sz(dq) && up[j][i - 1] >= up[dq.back()][i - 1])dq.pop_back();
dq.pb(j);
int l = 0, r = sz(dq) - 1, ans;
while(l <= r){
int mid = (l + r) >> 1;
if(dq[mid] <= up[j][i - 1]){
ans = mid; r = mid - 1;
}else{
l = mid + 1;
}
}
up[j][i] = up[dq[ans]][i - 1];
}
}
while(m--){
int l, r; cin >> l >> r; r--;
int ans = 0;
for(int i = 18; i >= 0; i--){
if(up[l][i] <= r){
ans += (1 << i); l = getmx(l, up[l][i]);
}
}
cout << ((ans == ((1 << 19) - 1)) ? -1 : ans + 1) << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("TESTROOMS.inp", "r", stdin);
//freopen("TESTROOMS.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26968 KB |
Output is correct |
2 |
Correct |
63 ms |
34436 KB |
Output is correct |
3 |
Correct |
72 ms |
32736 KB |
Output is correct |
4 |
Correct |
93 ms |
37460 KB |
Output is correct |
5 |
Correct |
75 ms |
36172 KB |
Output is correct |
6 |
Correct |
79 ms |
38484 KB |
Output is correct |
7 |
Correct |
81 ms |
38900 KB |
Output is correct |
8 |
Correct |
76 ms |
38996 KB |
Output is correct |
9 |
Correct |
72 ms |
37548 KB |
Output is correct |
10 |
Correct |
61 ms |
32084 KB |
Output is correct |
11 |
Correct |
54 ms |
37564 KB |
Output is correct |
12 |
Correct |
49 ms |
33560 KB |
Output is correct |
13 |
Correct |
54 ms |
35924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
99976 KB |
Output is correct |
2 |
Correct |
339 ms |
102820 KB |
Output is correct |
3 |
Correct |
321 ms |
100324 KB |
Output is correct |
4 |
Correct |
305 ms |
98892 KB |
Output is correct |
5 |
Correct |
358 ms |
98828 KB |
Output is correct |
6 |
Correct |
342 ms |
102696 KB |
Output is correct |
7 |
Correct |
363 ms |
103908 KB |
Output is correct |
8 |
Correct |
336 ms |
102096 KB |
Output is correct |
9 |
Correct |
338 ms |
103024 KB |
Output is correct |
10 |
Correct |
366 ms |
103808 KB |
Output is correct |
11 |
Correct |
327 ms |
100720 KB |
Output is correct |
12 |
Correct |
323 ms |
100116 KB |
Output is correct |
13 |
Correct |
324 ms |
104536 KB |
Output is correct |
14 |
Correct |
329 ms |
103452 KB |
Output is correct |
15 |
Correct |
294 ms |
98260 KB |
Output is correct |
16 |
Correct |
113 ms |
103816 KB |
Output is correct |
17 |
Correct |
115 ms |
98996 KB |
Output is correct |
18 |
Correct |
111 ms |
99000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26968 KB |
Output is correct |
2 |
Correct |
63 ms |
34436 KB |
Output is correct |
3 |
Correct |
72 ms |
32736 KB |
Output is correct |
4 |
Correct |
93 ms |
37460 KB |
Output is correct |
5 |
Correct |
75 ms |
36172 KB |
Output is correct |
6 |
Correct |
79 ms |
38484 KB |
Output is correct |
7 |
Correct |
81 ms |
38900 KB |
Output is correct |
8 |
Correct |
76 ms |
38996 KB |
Output is correct |
9 |
Correct |
72 ms |
37548 KB |
Output is correct |
10 |
Correct |
61 ms |
32084 KB |
Output is correct |
11 |
Correct |
54 ms |
37564 KB |
Output is correct |
12 |
Correct |
49 ms |
33560 KB |
Output is correct |
13 |
Correct |
54 ms |
35924 KB |
Output is correct |
14 |
Correct |
339 ms |
99976 KB |
Output is correct |
15 |
Correct |
339 ms |
102820 KB |
Output is correct |
16 |
Correct |
321 ms |
100324 KB |
Output is correct |
17 |
Correct |
305 ms |
98892 KB |
Output is correct |
18 |
Correct |
358 ms |
98828 KB |
Output is correct |
19 |
Correct |
342 ms |
102696 KB |
Output is correct |
20 |
Correct |
363 ms |
103908 KB |
Output is correct |
21 |
Correct |
336 ms |
102096 KB |
Output is correct |
22 |
Correct |
338 ms |
103024 KB |
Output is correct |
23 |
Correct |
366 ms |
103808 KB |
Output is correct |
24 |
Correct |
327 ms |
100720 KB |
Output is correct |
25 |
Correct |
323 ms |
100116 KB |
Output is correct |
26 |
Correct |
324 ms |
104536 KB |
Output is correct |
27 |
Correct |
329 ms |
103452 KB |
Output is correct |
28 |
Correct |
294 ms |
98260 KB |
Output is correct |
29 |
Correct |
113 ms |
103816 KB |
Output is correct |
30 |
Correct |
115 ms |
98996 KB |
Output is correct |
31 |
Correct |
111 ms |
99000 KB |
Output is correct |
32 |
Correct |
454 ms |
80760 KB |
Output is correct |
33 |
Correct |
417 ms |
84724 KB |
Output is correct |
34 |
Correct |
434 ms |
86808 KB |
Output is correct |
35 |
Correct |
416 ms |
85780 KB |
Output is correct |
36 |
Correct |
350 ms |
87472 KB |
Output is correct |
37 |
Correct |
479 ms |
81084 KB |
Output is correct |
38 |
Correct |
755 ms |
109640 KB |
Output is correct |
39 |
Correct |
651 ms |
105644 KB |
Output is correct |
40 |
Correct |
651 ms |
111164 KB |
Output is correct |
41 |
Correct |
582 ms |
111108 KB |
Output is correct |
42 |
Correct |
798 ms |
107100 KB |
Output is correct |
43 |
Correct |
275 ms |
64036 KB |
Output is correct |
44 |
Correct |
341 ms |
63860 KB |
Output is correct |
45 |
Correct |
269 ms |
62792 KB |
Output is correct |
46 |
Correct |
288 ms |
67012 KB |
Output is correct |
47 |
Correct |
728 ms |
103924 KB |
Output is correct |
48 |
Correct |
792 ms |
106140 KB |
Output is correct |
49 |
Correct |
789 ms |
104340 KB |
Output is correct |
50 |
Correct |
789 ms |
109808 KB |
Output is correct |
51 |
Correct |
214 ms |
102196 KB |
Output is correct |
52 |
Correct |
172 ms |
103168 KB |
Output is correct |
53 |
Correct |
215 ms |
101784 KB |
Output is correct |