#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 3e5 + 10, mods[6] = {(int) 1e9 + 7, 998244353, 1100000123, (int) 1e9 + 9, 1610612741, 805306457};
int md[2];
struct Hash {
int x, y;
inline Hash (const int &_x = 0, const int &_y = 0) : x(_x), y(_y) {}
inline ll operator ()(Hash a) const { return x * 31LL + y; }
friend bool operator<(const Hash &a, const Hash &b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
friend bool operator==(const Hash &a, const Hash &b) { return (a.x == b.x && a.y == b.y); }
friend Hash operator*(const Hash &a, const int &b) { return Hash((1ll * a.x * b) % md[0], (1ll * a.y * b) % md[1]); }
friend Hash operator+(const Hash &a, const int &b) { return Hash((a.x + b) % md[0], (a.y + b) % md[1]); }
friend Hash operator+(const Hash &a, const Hash &b) { return Hash((a.x + b.x) % md[0], (a.y + b.y) % md[1]); }
friend Hash operator*(const Hash &a, const Hash &b) { return Hash((1ll * a.x * b.x) % md[0], (1ll * a.y * b.y) % md[1]); }
friend Hash operator-(const Hash &a, const Hash &b) { return Hash((a.x - b.x + md[0]) % md[0], (a.y - b.y + md[1]) % md[1]); }
} base (97, 53);
int dp[20][N], rmq[20][20][N], lg[N], n, m, q; Hash pw[N], H[N];
char S[N * 2];
inline void decide () {
md[0] = mods[rng() % 6];
md[1] = mods[rng() % 6];
if (md[0] == md[1]) md[1] = (md[0] != mods[4] ? mods[4] : mods[5]);
}
unordered_set <Hash, Hash> ost;
Hash get_hash(int l, int r) {
return H[r] - (H[l - 1] * pw[r - l + 1]);
}
void build_rmq(int id) {
for (int i = 1; i <= n; i++) {
rmq[id][0][i] = dp[id][i];
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
if (j + (1 << i) <= n + 1) {
rmq[id][i][j] = max(rmq[id][i - 1][j], rmq[id][i - 1][j + (1 << (i - 1))]);
}
}
}
}
int get_max(int id, int l, int r) {
if (r <= l) return l;
r = min(r, n + 1);
int x = lg[r - l];
return max(rmq[id][x][l], rmq[id][x][r - (1 << x)]);
}
int main() {
decide();
for (int i = 2; i < N; i++)
lg[i] = lg[i >> 1] + 1;
pw[0] = Hash(1, 1);
for (int i = 1; i < N; i++)
pw[i] = pw[i - 1] * base;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%s", S);
Hash h(0, 0);
for (int j = 0; S[j]; j++) {
h = (h * base) + (S[j] - 'a' + 1);
ost.insert(h);
}
}
scanf("%s", S + 1); m = strlen(S + 1);
for (int i = 1; i <= m; i++) {
H[i] = (H[i - 1] * base) + (S[i] - 'a' + 1);
}
swap(n, m);
for (int i = 1; i <= n; i++) {
int l = i - 1, r = n + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (ost.find(get_hash(i, mid)) != ost.end()) l = mid;
else r = mid;
}
dp[0][i] = l + 1;
}
for (int j = 1; j < 20; j++) {
build_rmq(j - 1);
for (int i = 1; i <= n; i++) {
dp[j][i] = get_max(j - 1, i, dp[j - 1][i] + 1);
}
}
for (build_rmq(19); q; q--) {
int l, r; scanf("%d%d", &l, &r); l++;
int ret = 0, p = l;
for (int i = 19; ~i; i--) {
if (get_max(i, l, p + 1) <= r) ret += 1 << i, p = get_max(i, l, p + 1);
}
printf("%d\n", ret > n ? -1 : ret + 1);
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%s", S);
| ~~~~~^~~~~~~~~
Main.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%s", S + 1); m = strlen(S + 1);
| ~~~~~^~~~~~~~~~~~~
Main.cpp:98:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | int l, r; scanf("%d%d", &l, &r); l++;
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6732 KB |
Output is correct |
2 |
Execution timed out |
2070 ms |
7404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2062 ms |
7292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6732 KB |
Output is correct |
2 |
Execution timed out |
2070 ms |
7404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |