#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define PB push_back
#define ll long long
#define ld long double
#define _sz(e) (int)e.size()
#define pii pair <int , int>
#define _all(e) e.begin() , e.end()
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#pragma GCC target("sse,avx,avx2")
#pragma GCC optimize("O3,unroll-loops")
const ll maxn = 3e5 + 4 , N = 1e4 + 4 , base = 307 , mod = 1e9 + 7 , INF = 1e9 , lg = 19;
struct node {
int mx[lg];
};
node seg[4 * maxn];
string t[N];
ll pbase[maxn];
vector <ll> h[N];
int n , m , ql[maxn] , qr[maxn] , SZ , dp[lg][maxn];
void pre() {
pbase[0] = 1;
for (int i = 1; i < maxn; ++i) {
pbase[i] = (pbase[i - 1] * base) % mod;
}
for (int i = 0; i <= n; ++i) {
h[i].PB(0);
for (int j = 1; j <= _sz(t[i]); ++j) {
int val = (h[i].back() * base + t[i][j - 1]) % mod;
h[i].PB(val);
}
}
}
inline int HASH(int i , int l , int r) {
return (h[i][r] - (h[i][l] * pbase[r - l] % mod) + mod) % mod;
}
inline int LCP(int x , int i) {
if(t[0][x] != t[i][0]) return 0;
int l = 1 , r = min(SZ - x , _sz(t[i])) + 1;
while(l < r - 1) {
int mm = (l + r) / 2;
if(HASH(0 , x , x + mm) == HASH(i , 0 , mm)) l = mm;
else r = mm;
}
return l;
}
inline bool check(int x , int i) {
int lcp = LCP(x , i);
if(x + lcp == SZ) return (_sz(t[i]) > lcp);
if(_sz(t[i]) == lcp) return 0;
return t[0][x + lcp] < t[i][lcp];
}
inline int bs(int x) {
if(check(x , 1)) return 1;
int l = 1 , r = n + 1;
while(l < r - 1) {
int mm = (l + r) / 2;
if(check(x , mm)) r = mm;
else l = mm;
}
return r;
}
void add(int i , int p , int x , int v = 1 , int s = 0 , int e = SZ) {
seg[v].mx[i] = max(seg[v].mx[i] , x);
if(e - s == 1) return ;
if(p < mid) {
add(i , p , x , lc , s , mid);
return ;
}
add(i , p , x , rc , mid , e);
}
int get(int i , int l , int r , int v = 1 , int s = 0 , int e = SZ) {
if(l >= e || s >= r) return 0;
if(l <= s && e <= r) return seg[v].mx[i];
return max(get(i , l , r , lc , s , mid) , get(i , l , r , rc , mid , e));
}
int main() {
FAST;
// freopen("2-01.in" , "r" , stdin);
// freopen("2-01.out" , "w" , stdout);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> t[i];
}
cin >> t[0];
sort(t + 1 , t + 1 + n);
for (int i = 0; i < m; ++i) {
cin >> ql[i] >> qr[i];
}
pre();
SZ = _sz(t[0]);
for (int i = 0; i < SZ; ++i) {
int p = bs(i);
if(p <= n) dp[0][i] = max(dp[0][i] , i + LCP(i , p));
if(p > 0) dp[0][i] = max(dp[0][i] , i + LCP(i , p - 1));
add(0 , i , dp[0][i]);
}
for (int i = 1; i < lg; ++i) {
for (int j = 0; j < SZ; ++j) {
dp[i][j] = get(i - 1 , j , dp[i - 1][j] + 1);
add(i , j , dp[i][j]);
}
}
for (int i = 0; i < m; ++i) {
int cost = 0 , mx = ql[i];
for (int j = lg - 1; j >= 0; --j) {
int idx = get(j , ql[i] , mx + 1);
if(idx >= qr[i]) continue;
cost += (1 << j) , mx = idx;
}
cout << (get(0 , ql[i] , mx + 1) >= qr[i] ? cost + 1 : -1) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3276 KB |
Output is correct |
2 |
Correct |
427 ms |
11456 KB |
Output is correct |
3 |
Correct |
613 ms |
10128 KB |
Output is correct |
4 |
Correct |
638 ms |
11972 KB |
Output is correct |
5 |
Correct |
651 ms |
11304 KB |
Output is correct |
6 |
Correct |
669 ms |
12744 KB |
Output is correct |
7 |
Correct |
684 ms |
12400 KB |
Output is correct |
8 |
Correct |
634 ms |
13252 KB |
Output is correct |
9 |
Correct |
611 ms |
12496 KB |
Output is correct |
10 |
Correct |
551 ms |
8632 KB |
Output is correct |
11 |
Correct |
427 ms |
11132 KB |
Output is correct |
12 |
Correct |
479 ms |
8936 KB |
Output is correct |
13 |
Correct |
449 ms |
10852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1900 ms |
108504 KB |
Output is correct |
2 |
Correct |
1823 ms |
110500 KB |
Output is correct |
3 |
Correct |
1797 ms |
108844 KB |
Output is correct |
4 |
Correct |
1763 ms |
107780 KB |
Output is correct |
5 |
Correct |
1749 ms |
108132 KB |
Output is correct |
6 |
Correct |
1794 ms |
110364 KB |
Output is correct |
7 |
Correct |
1823 ms |
111480 KB |
Output is correct |
8 |
Correct |
1822 ms |
110076 KB |
Output is correct |
9 |
Correct |
1846 ms |
110724 KB |
Output is correct |
10 |
Correct |
1970 ms |
111452 KB |
Output is correct |
11 |
Correct |
1760 ms |
109068 KB |
Output is correct |
12 |
Correct |
1786 ms |
108836 KB |
Output is correct |
13 |
Correct |
1845 ms |
112240 KB |
Output is correct |
14 |
Correct |
1837 ms |
111448 KB |
Output is correct |
15 |
Correct |
1752 ms |
107564 KB |
Output is correct |
16 |
Correct |
1268 ms |
110552 KB |
Output is correct |
17 |
Correct |
1277 ms |
108764 KB |
Output is correct |
18 |
Correct |
1246 ms |
108716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3276 KB |
Output is correct |
2 |
Correct |
427 ms |
11456 KB |
Output is correct |
3 |
Correct |
613 ms |
10128 KB |
Output is correct |
4 |
Correct |
638 ms |
11972 KB |
Output is correct |
5 |
Correct |
651 ms |
11304 KB |
Output is correct |
6 |
Correct |
669 ms |
12744 KB |
Output is correct |
7 |
Correct |
684 ms |
12400 KB |
Output is correct |
8 |
Correct |
634 ms |
13252 KB |
Output is correct |
9 |
Correct |
611 ms |
12496 KB |
Output is correct |
10 |
Correct |
551 ms |
8632 KB |
Output is correct |
11 |
Correct |
427 ms |
11132 KB |
Output is correct |
12 |
Correct |
479 ms |
8936 KB |
Output is correct |
13 |
Correct |
449 ms |
10852 KB |
Output is correct |
14 |
Correct |
1900 ms |
108504 KB |
Output is correct |
15 |
Correct |
1823 ms |
110500 KB |
Output is correct |
16 |
Correct |
1797 ms |
108844 KB |
Output is correct |
17 |
Correct |
1763 ms |
107780 KB |
Output is correct |
18 |
Correct |
1749 ms |
108132 KB |
Output is correct |
19 |
Correct |
1794 ms |
110364 KB |
Output is correct |
20 |
Correct |
1823 ms |
111480 KB |
Output is correct |
21 |
Correct |
1822 ms |
110076 KB |
Output is correct |
22 |
Correct |
1846 ms |
110724 KB |
Output is correct |
23 |
Correct |
1970 ms |
111452 KB |
Output is correct |
24 |
Correct |
1760 ms |
109068 KB |
Output is correct |
25 |
Correct |
1786 ms |
108836 KB |
Output is correct |
26 |
Correct |
1845 ms |
112240 KB |
Output is correct |
27 |
Correct |
1837 ms |
111448 KB |
Output is correct |
28 |
Correct |
1752 ms |
107564 KB |
Output is correct |
29 |
Correct |
1268 ms |
110552 KB |
Output is correct |
30 |
Correct |
1277 ms |
108764 KB |
Output is correct |
31 |
Correct |
1246 ms |
108716 KB |
Output is correct |
32 |
Execution timed out |
2084 ms |
65076 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |