#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define int long long
const int64_t INF = 1ll << 60;
const int N = 5e5 + 5;
const int MOD[] = {(int) 1e9 + 7, (int) 1e9 + 9};
const int BASE = 37;
const int LG = 19;
vector<array<int, 2>> hs[N], h[N];
int INV[2][N], lcp[N], P[2][N], n, m, H[2][N], dp[LG][N];
string L, s[N];
array<int, 2> rmq[LG][N];
int inv(int x, const int MOD) {
int re = 1;
for (int b = MOD - 2; b; b >>= 1, x = x * x % MOD) if (b & 1)
re = re * x % MOD;
return re;
}
int get_hash(int i, int l, int r) {
int hsh = H[i][r] - (l ? H[i][l - 1] : 0);
hsh *= INV[i][l];
hsh %= MOD[i];
if (hsh < 0)
hsh += MOD[i];
return hsh;
}
bool check(int l, int r) {
return binary_search(hs[r - l].begin(), hs[r - l].end(), array<int, 2>{get_hash(0, l, r), get_hash(1, l, r)});
}
void build_lcp() {
FOR(i, 0, L.size()) {
int l = i - 1, r = L.size();
while (r - l > 1) {
int m = l + r >> 1;
if (check(i, m))
l = m;
else
r = m;
}
lcp[i] = r;
}
}
void build_rmq() {
FOR(i, 0, L.size())
rmq[0][i] = {lcp[i], i};
FOR(j, 0, LG - 1) {
FOR(i, 0, 1 + (int) L.size() - (2 << j)) {
rmq[j + 1][i] = max(rmq[j][i], rmq[j][i + (1 << j)]);
}
}
}
int get(int l, int r) { //[l, r)
int t = __lg(r - l);
return max(rmq[t][l], rmq[t][r - (1 << t)])[1];
}
void build_dp() {
copy(lcp, lcp + L.size(), dp[0]);
for (int j = 1; j < LG; j++) {
FOR(i, 0, L.size()) {
dp[j][i] = dp[j - 1][i];
if (dp[j][i] < L.size()) {
int mx_ind = get(i, dp[j][i] + 1);
dp[j][i] = dp[j - 1][mx_ind];
}
}
}
}
int calc(int l, int r) {
int tl = l;
int ans = 0;
for (int j = LG - 1; j >= 0; j--) {
if (dp[j][l] < r) {
ans |= 1 << j;
l = get(tl, dp[j][l] + 1);
}
}
return (dp[0][l] >= r ? ans + 1 : -1);
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
P[0][0] = P[1][0] = INV[0][0] = INV[1][0] = 1;
FOR(j, 0, 2) {
FOR(i, 1, N) {
P[j][i] = (P[j][i - 1] * BASE) % MOD[j];
INV[j][i] = inv(P[j][i], MOD[j]);
}
}
cin >> n >> m;
FOR(i, 0, n) {
cin >> s[i];
h[i].resize(s[i].size());
h[i][0] = {s[i][0] - 'a' + 1, s[i][0] - 'a' + 1};
FOR(k, 0, 2) {
FOR(j, 1, s[i].size()) {
h[i][j][k] = (h[i][j - 1][k] + (s[i][j] - 'a' + 1) * P[k][j] % MOD[k]) % MOD[k];
}
}
FOR(j, 0, s[i].size())
hs[j].push_back(h[i][j]);
}
FOR(i, 0, N) {
sort(hs[i].begin(), hs[i].end());
}
cin >> L;
FOR(k, 0, 2) {
H[k][0] = L[0] - 'a' + 1;
FOR(i, 1, L.size()) {
H[k][i] = (H[k][i - 1] + (L[i] - 'a' + 1) * P[k][i] % MOD[k]) % MOD[k];
}
}
build_lcp();
build_rmq();
build_dp();
FOR(i, 0, m) {
int l, r;
cin >> l >> r;
cout << calc(l, r) << '\n';
}
}
//13:00:40
Compilation message
Main.cpp: In function 'void build_lcp()':
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:36:9: note: in expansion of macro 'FOR'
36 | FOR(i, 0, L.size()) {
| ^~~
Main.cpp:39:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void build_rmq()':
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:49:9: note: in expansion of macro 'FOR'
49 | FOR(i, 0, L.size())
| ^~~
Main.cpp: In function 'void build_dp()':
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:64:17: note: in expansion of macro 'FOR'
64 | FOR(i, 0, L.size()) {
| ^~~
Main.cpp:66:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if (dp[j][i] < L.size()) {
| ~~~~~~~~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:100:25: note: in expansion of macro 'FOR'
100 | FOR(j, 1, s[i].size()) {
| ^~~
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:104:17: note: in expansion of macro 'FOR'
104 | FOR(j, 0, s[i].size())
| ^~~
Main.cpp:3:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, a, b) for (int i = (a); i < (b); i++)
| ^
Main.cpp:113:17: note: in expansion of macro 'FOR'
113 | FOR(i, 1, L.size()) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
55232 KB |
Output is correct |
2 |
Correct |
509 ms |
69996 KB |
Output is correct |
3 |
Correct |
500 ms |
66212 KB |
Output is correct |
4 |
Correct |
515 ms |
70312 KB |
Output is correct |
5 |
Correct |
523 ms |
69272 KB |
Output is correct |
6 |
Correct |
523 ms |
72544 KB |
Output is correct |
7 |
Correct |
520 ms |
76744 KB |
Output is correct |
8 |
Correct |
524 ms |
73684 KB |
Output is correct |
9 |
Correct |
510 ms |
72348 KB |
Output is correct |
10 |
Correct |
482 ms |
60940 KB |
Output is correct |
11 |
Correct |
496 ms |
77468 KB |
Output is correct |
12 |
Correct |
490 ms |
66436 KB |
Output is correct |
13 |
Correct |
483 ms |
72536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
734 ms |
194656 KB |
Output is correct |
2 |
Correct |
783 ms |
199064 KB |
Output is correct |
3 |
Correct |
734 ms |
195512 KB |
Output is correct |
4 |
Correct |
746 ms |
191912 KB |
Output is correct |
5 |
Correct |
732 ms |
192276 KB |
Output is correct |
6 |
Correct |
810 ms |
198948 KB |
Output is correct |
7 |
Correct |
801 ms |
201348 KB |
Output is correct |
8 |
Correct |
782 ms |
197640 KB |
Output is correct |
9 |
Correct |
790 ms |
199936 KB |
Output is correct |
10 |
Correct |
814 ms |
202448 KB |
Output is correct |
11 |
Correct |
740 ms |
195512 KB |
Output is correct |
12 |
Correct |
734 ms |
194468 KB |
Output is correct |
13 |
Correct |
811 ms |
203856 KB |
Output is correct |
14 |
Correct |
784 ms |
202344 KB |
Output is correct |
15 |
Correct |
692 ms |
191152 KB |
Output is correct |
16 |
Correct |
545 ms |
208528 KB |
Output is correct |
17 |
Correct |
520 ms |
194548 KB |
Output is correct |
18 |
Correct |
518 ms |
194292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
55232 KB |
Output is correct |
2 |
Correct |
509 ms |
69996 KB |
Output is correct |
3 |
Correct |
500 ms |
66212 KB |
Output is correct |
4 |
Correct |
515 ms |
70312 KB |
Output is correct |
5 |
Correct |
523 ms |
69272 KB |
Output is correct |
6 |
Correct |
523 ms |
72544 KB |
Output is correct |
7 |
Correct |
520 ms |
76744 KB |
Output is correct |
8 |
Correct |
524 ms |
73684 KB |
Output is correct |
9 |
Correct |
510 ms |
72348 KB |
Output is correct |
10 |
Correct |
482 ms |
60940 KB |
Output is correct |
11 |
Correct |
496 ms |
77468 KB |
Output is correct |
12 |
Correct |
490 ms |
66436 KB |
Output is correct |
13 |
Correct |
483 ms |
72536 KB |
Output is correct |
14 |
Correct |
734 ms |
194656 KB |
Output is correct |
15 |
Correct |
783 ms |
199064 KB |
Output is correct |
16 |
Correct |
734 ms |
195512 KB |
Output is correct |
17 |
Correct |
746 ms |
191912 KB |
Output is correct |
18 |
Correct |
732 ms |
192276 KB |
Output is correct |
19 |
Correct |
810 ms |
198948 KB |
Output is correct |
20 |
Correct |
801 ms |
201348 KB |
Output is correct |
21 |
Correct |
782 ms |
197640 KB |
Output is correct |
22 |
Correct |
790 ms |
199936 KB |
Output is correct |
23 |
Correct |
814 ms |
202448 KB |
Output is correct |
24 |
Correct |
740 ms |
195512 KB |
Output is correct |
25 |
Correct |
734 ms |
194468 KB |
Output is correct |
26 |
Correct |
811 ms |
203856 KB |
Output is correct |
27 |
Correct |
784 ms |
202344 KB |
Output is correct |
28 |
Correct |
692 ms |
191152 KB |
Output is correct |
29 |
Correct |
545 ms |
208528 KB |
Output is correct |
30 |
Correct |
520 ms |
194548 KB |
Output is correct |
31 |
Correct |
518 ms |
194292 KB |
Output is correct |
32 |
Correct |
1075 ms |
146404 KB |
Output is correct |
33 |
Correct |
1038 ms |
153728 KB |
Output is correct |
34 |
Correct |
1042 ms |
157520 KB |
Output is correct |
35 |
Correct |
1074 ms |
156044 KB |
Output is correct |
36 |
Correct |
1032 ms |
161268 KB |
Output is correct |
37 |
Correct |
973 ms |
146144 KB |
Output is correct |
38 |
Correct |
1549 ms |
204784 KB |
Output is correct |
39 |
Correct |
1457 ms |
197332 KB |
Output is correct |
40 |
Correct |
1506 ms |
207304 KB |
Output is correct |
41 |
Correct |
1409 ms |
208248 KB |
Output is correct |
42 |
Correct |
1483 ms |
200796 KB |
Output is correct |
43 |
Correct |
861 ms |
108968 KB |
Output is correct |
44 |
Correct |
898 ms |
108520 KB |
Output is correct |
45 |
Correct |
885 ms |
107960 KB |
Output is correct |
46 |
Correct |
885 ms |
114860 KB |
Output is correct |
47 |
Correct |
1381 ms |
193112 KB |
Output is correct |
48 |
Correct |
1493 ms |
198116 KB |
Output is correct |
49 |
Correct |
1405 ms |
193844 KB |
Output is correct |
50 |
Correct |
1584 ms |
205772 KB |
Output is correct |
51 |
Correct |
702 ms |
195368 KB |
Output is correct |
52 |
Correct |
678 ms |
200464 KB |
Output is correct |
53 |
Correct |
715 ms |
195840 KB |
Output is correct |