Submission #795848

# Submission time Handle Problem Language Result Execution time Memory
795848 2023-07-27T17:41:00 Z RushB Dabbeh (INOI20_dabbeh) C++17
50 / 100
2000 ms 274572 KB
#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 = 1e6 + 5;
const int MOD[] = {(int) 1e9 + 7, (int) 1e9 + 9};
const int BASE = 37;
const int LG = 22;
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()) {
      |                 ^~~
# Verdict Execution time Memory Grader output
1 Correct 815 ms 110100 KB Output is correct
2 Correct 907 ms 126340 KB Output is correct
3 Correct 907 ms 122656 KB Output is correct
4 Correct 951 ms 127140 KB Output is correct
5 Correct 934 ms 126040 KB Output is correct
6 Correct 949 ms 129452 KB Output is correct
7 Correct 923 ms 133596 KB Output is correct
8 Correct 927 ms 130700 KB Output is correct
9 Correct 916 ms 129152 KB Output is correct
10 Correct 891 ms 117528 KB Output is correct
11 Correct 898 ms 134352 KB Output is correct
12 Correct 881 ms 123096 KB Output is correct
13 Correct 894 ms 129292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1176 ms 257096 KB Output is correct
2 Correct 1225 ms 261500 KB Output is correct
3 Correct 1176 ms 257824 KB Output is correct
4 Correct 1130 ms 254216 KB Output is correct
5 Correct 1165 ms 254712 KB Output is correct
6 Correct 1210 ms 261536 KB Output is correct
7 Correct 1261 ms 263784 KB Output is correct
8 Correct 1184 ms 259984 KB Output is correct
9 Correct 1207 ms 262444 KB Output is correct
10 Correct 1230 ms 265084 KB Output is correct
11 Correct 1189 ms 257980 KB Output is correct
12 Correct 1162 ms 256796 KB Output is correct
13 Correct 1228 ms 266396 KB Output is correct
14 Correct 1212 ms 264892 KB Output is correct
15 Correct 1117 ms 253416 KB Output is correct
16 Correct 995 ms 271180 KB Output is correct
17 Correct 943 ms 256792 KB Output is correct
18 Correct 938 ms 256556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 815 ms 110100 KB Output is correct
2 Correct 907 ms 126340 KB Output is correct
3 Correct 907 ms 122656 KB Output is correct
4 Correct 951 ms 127140 KB Output is correct
5 Correct 934 ms 126040 KB Output is correct
6 Correct 949 ms 129452 KB Output is correct
7 Correct 923 ms 133596 KB Output is correct
8 Correct 927 ms 130700 KB Output is correct
9 Correct 916 ms 129152 KB Output is correct
10 Correct 891 ms 117528 KB Output is correct
11 Correct 898 ms 134352 KB Output is correct
12 Correct 881 ms 123096 KB Output is correct
13 Correct 894 ms 129292 KB Output is correct
14 Correct 1176 ms 257096 KB Output is correct
15 Correct 1225 ms 261500 KB Output is correct
16 Correct 1176 ms 257824 KB Output is correct
17 Correct 1130 ms 254216 KB Output is correct
18 Correct 1165 ms 254712 KB Output is correct
19 Correct 1210 ms 261536 KB Output is correct
20 Correct 1261 ms 263784 KB Output is correct
21 Correct 1184 ms 259984 KB Output is correct
22 Correct 1207 ms 262444 KB Output is correct
23 Correct 1230 ms 265084 KB Output is correct
24 Correct 1189 ms 257980 KB Output is correct
25 Correct 1162 ms 256796 KB Output is correct
26 Correct 1228 ms 266396 KB Output is correct
27 Correct 1212 ms 264892 KB Output is correct
28 Correct 1117 ms 253416 KB Output is correct
29 Correct 995 ms 271180 KB Output is correct
30 Correct 943 ms 256792 KB Output is correct
31 Correct 938 ms 256556 KB Output is correct
32 Correct 1434 ms 208260 KB Output is correct
33 Correct 1496 ms 215908 KB Output is correct
34 Correct 1502 ms 219808 KB Output is correct
35 Correct 1519 ms 218108 KB Output is correct
36 Correct 1475 ms 223544 KB Output is correct
37 Correct 1441 ms 208116 KB Output is correct
38 Correct 1994 ms 270836 KB Output is correct
39 Correct 1878 ms 263136 KB Output is correct
40 Correct 1975 ms 273716 KB Output is correct
41 Correct 1852 ms 274572 KB Output is correct
42 Correct 1908 ms 266964 KB Output is correct
43 Correct 1371 ms 169820 KB Output is correct
44 Correct 1331 ms 169148 KB Output is correct
45 Correct 1291 ms 168736 KB Output is correct
46 Correct 1382 ms 175928 KB Output is correct
47 Correct 1835 ms 259244 KB Output is correct
48 Correct 1887 ms 264104 KB Output is correct
49 Correct 1882 ms 260060 KB Output is correct
50 Execution timed out 2047 ms 272152 KB Time limit exceeded
51 Halted 0 ms 0 KB -