Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast") // ������������ ���������, �� ��� ������
- #pragma GCC optimize("no-stack-protector") //�����
- #pragma GCC optimize("unroll-loops") // � ���� ���� ��� �� 100 �� ������ ����� � ��������� �� ��� � 100 ��� �� ������ ��������
- #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") // ��� ����� ����� �� ��� 03 02 ��������� � ������ ������� �������� ��� ���� ������������
- #pragma GCC optimize("fast-math")
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <list>
- #include <unordered_map>
- #include <unordered_set>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef short sh;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define sqrt sqrtl
- #define F first
- #define S second
- #define endl '\n'
- #define all(vc666) vc666.begin(), vc666.end()
- #define allr(vc666) vc666.rbegin(), vc666.rend()
- #define int long long
- #define degug(x) cerr (#x) << " " << (x) << endl;
- const ll INF = 2e18;
- const ll inf = 2e9 + 3;
- const ll ONE = 1, ZERO = 0;
- const ll mod = 998244353;
- const ll m1 = 1e9 + 575179;
- const ll m2 = 1e9 + 87;
- const ll LG = 19;
- const ll k = 10000;
- const ll p = 79;
- ld EPS = 1e-9;
- ld PI = 3.1415926535897932384;
- ld phi = (sqrt(5) + 1.0) / 2.0;
- mt19937_64 rnd(4906);
- struct Query {
- int l, r, idx;
- };
- const int N = 1e5 + 3;
- pair <ll, ll> pw[N];
- pair <ll, ll> h[N];
- vector <ll> keys[N];
- vector <short> base[N], base2[N];
- int ans[N];
- Query Q[N];
- pair <ll, ll> get_h(int l, int r) {
- return { (h[r + 1].first - (h[l].first * pw[r - l + 1].first) % m1 + m1) % m1, (h[r + 1].second - (h[l].second * pw[r - l + 1].second) % m2 + m2) % m2 };
- }
- bool cmp(const Query& a, const Query& b) {
- if (a.l / k != b.l / k) {
- return a.l < b.l;
- }
- else {
- return a.r < b.r;
- }
- }
- void solve() {
- string t, s;
- int n, len_t, len_s, len, i, j, m, l, r, q, res, lst = -1;
- cin >> t;
- len_t = t.size();
- pw[0] = { 1, 1 };
- h[0] = { 0, 0 };
- for (i = 1; i <= len_t; i++) {
- pw[i].first = (pw[i - 1].first * p) % m1;
- pw[i].second = (pw[i - 1].second * p) % m2;
- h[i].first = (h[i - 1].first * p + (t[i - 1] - 'a' + ONE)) % m1;
- h[i].second = (h[i - 1].second * p + (t[i - 1] - 'a' + ONE)) % m2;
- }
- pair <ll, ll> hesh;
- vector <int> rzl;
- cin >> n;
- for (i = 0; i < n; i++) {
- cin >> s;
- base[i].reserve(320);
- base2[i].reserve(320);
- len_s = s.size();
- rzl.push_back(len_s);
- hesh = { 0, 0 };
- for (j = 0; j < len_s; j++) {
- hesh.first = (hesh.first * p + (s[j] - 'a' + ONE)) % m1;
- hesh.second = (hesh.second * p + (s[j] - 'a' + ONE)) % m2;
- }
- keys[len_s].push_back(hesh.first * m1 + hesh.second);
- }
- sort(all(rzl));
- rzl.resize(unique(all(rzl)) - rzl.begin());
- m = rzl.size();
- for (j = 0; j < m; j++) {
- sort(all(keys[rzl[j]]));
- }
- for (i = 0; i < len_t; i++) {
- for (j = 0; j < m; j++) {
- len = rzl[j];
- if (i + len - 1 < len_t) {
- hesh = get_h(i, i + len - 1);
- if (binary_search(all(keys[len]), hesh.first * m1 + hesh.second)) {
- base[i].push_back(len);
- base2[i + len - 1].push_back(len);
- }
- }
- }
- }
- for (i = 0; i < len_t; i++) {
- sort(all(base2[i]));
- }
- cin >> q;
- for (i = 0; i < q; i++) {
- cin >> Q[i].l >> Q[i].r;
- Q[i].idx = i;
- Q[i].l--;
- Q[i].r--;
- }
- sort(Q, Q + q, cmp);
- int left, right, last_r, last_l;
- for (i = 0; i < q; i++) {
- if (Q[i].l / k != lst) {
- lst = Q[i].l / k;
- l = Q[i].l;
- r = Q[i].l - 1;
- res = 0;
- }
- last_r = r;
- last_l = l;
- for (r; r < Q[i].r; r++) {
- left = -1, right = base2[r + 1].size();
- while (right - left > 1) {
- m = (left + right) / 2;
- if (r + 1 - base2[r + 1][m] + 1 >= last_l) {
- left = m;
- }
- else {
- right = m;
- }
- }
- res += right;
- }
- for (l; l > Q[i].l; l--) {
- left = -1, right = base[l - 1].size();
- while (right - left > 1) {
- m = (left + right) / 2;
- if (l - 1 + base[l - 1][m] - 1 <= r) {
- left = m;
- }
- else {
- right = m;
- }
- }
- res += right;
- }
- for (l; l < Q[i].l; l++) {
- left = -1, right = base[l].size();
- while (right - left > 1) {
- m = (left + right) / 2;
- if (l + base[l][m] - 1 <= r) {
- left = m;
- }
- else {
- right = m;
- }
- }
- res -= right;
- }
- ans[Q[i].idx] = res;
- }
- for (i = 0; i < q; i++) {
- cout << ans[i] << " ";
- }
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r ", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- //cin >> t;
- while (t--) solve();
- }
- //Deisgned by skimono
- //Клуб "Кольца(Серьги)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement