Submission #1002070

# Submission time Handle Problem Language Result Execution time Memory
1002070 2024-06-19T09:40:34 Z Nelt Dabbeh (INOI20_dabbeh) C++17
100 / 100
1288 ms 91132 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"
 
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll p = 37, lg = 20;
const ll N = 5e5 + 5;
ll mod[] = {998244353, (ll)1e9 + 9};
long long pw[3][N], invpw[3][N];
ll up[lg][N];
ll binpow(ll n, ll p, ll mod)
{
    n %= mod;
    ll ans = 1;
    while (p)
    {
        ans = 1ll * ans * (p & 1 ? n : 1) % mod;
        p >>= 1;
        n = 1ll * n * n % mod;
    }
    return ans;
}
ll inv(ll n, ll mod)
{
    return binpow(n, mod - 2, mod);
}
struct SegTree
{
    vector<pair<ll, ll>> st;
    ll n;
    SegTree(ll sz = 0)
    {
        n = sz;
        st.resize(n << 1, make_pair(0, 0));
    }
    void set(ll i, ll v)
    {
        st[i + n] = make_pair(v, i);
    }
    void build()
    {
        for (ll i = n - 1; i >= 1; i--) st[i] = max(st[i << 1], st[i << 1 | 1]);
    }
    pair<ll, ll> query(ll l, ll r)
    {
        pair<ll, ll> ans = make_pair(-1, -1);
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1)
        {
            if (l & 1) ans = max(ans, st[l++]);
            if (r & 1) ans = max(ans, st[--r]);
        }
        return ans;
    }
};
ll pref[2][N];
array<int, 2> gethash(ll l, ll r)
{
    if (l > r) return array<int, 2>{0, 0};
    array<int, 2> ans = {0, 0};
    for (ll i = 0; i < 2; i++)
        ans[i] = 1ll * (pref[i][r] - (l - 1 < 0 ? 0 : pref[i][l - 1]) + mod[i]) % mod[i] * invpw[i][l] % mod[i];
    return ans;
}
void solve()
{
    ll n, q;
    cin >> n >> q;
    string t[n];
    for (string &i : t) cin >> i;
    for (ll x = 0; x < 2; x++) pw[x][0] = invpw[x][0] = 1;
    for (ll x = 0; x < 2; x++)
    {
        ll invp = inv(p, mod[x]);
        for (ll i = 1; i < N; i++) pw[x][i] = 1ll * pw[x][i - 1] * p % mod[x];
        for (ll i = 1; i < N; i++) invpw[x][i] = 1ll * invpw[x][i - 1] * invp % mod[x];
    }
    vector<array<int, 2>> hs;
    hs.push_back({0, 0});
  	string s;
    cin >> s;
    for (string e : t)
    {
        array<int, 2> cur = {0, 0};
        for (ll j = 0; j < e.size(); j++)
        { 
            for (ll x = 0; x < 2; x++)
            cur[x] = (cur[x] + 1ll * (e[j] - 'a' + 1) * pw[x][j]) % mod[x];
            hs.push_back(cur);
        }
    }
    sort(hs.begin(), hs.end());
    for (ll x = 0; x < 2; x++)
    for (ll i = 0; i < s.size(); i++)
        pref[x][i] = ((i - 1 < 0 ? 0 : pref[x][i - 1]) + 1ll * (s[i] - 'a' + 1) * pw[x][i]) % mod[x];
    ll rig[s.size()];
    for (ll i = 0; i < s.size(); i++)
    {
        ll l = i, r = s.size() - 1, mid, pos;
        array<int, 2> p;
        rig[i] = i;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            p = gethash(i, mid);
            pos = lower_bound(hs.begin(), hs.end(), p) - hs.begin();
            if (pos != hs.size() and hs[pos] == p) rig[i] = mid + 1, l = mid + 1;
            else r = mid - 1;
        }
    }
    SegTree st(s.size() + 1);
    for (ll i = 0; i < s.size(); i++) st.set(i, rig[i]);
    st.set(s.size(), s.size());
    st.build();
    for (ll i = 0; i < s.size(); i++) up[0][i] = rig[i] == i ? i : st.query(i, rig[i]).second;
    up[0][s.size()] = s.size();
    for (ll bit = 1; bit < lg; bit++)
        for (ll i = 0; i <= s.size(); i++) up[bit][i] = up[bit - 1][up[bit - 1][i]];
    while (q--)
    {
        ll l, r;
        cin >> l >> r;
        if (rig[l] >= r) cout << "1\n";
        else
        {
            ll ans = 1;
            for (ll bit = lg - 1; bit >= 0; bit--) if (rig[up[bit][l]] < r) ans += 1 << bit, l = up[bit][l];
            if (rig[up[0][l]] < r) ans = -1;
            else ans++;
            cout << ans << endl;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:89:26: 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]
   89 |         for (ll j = 0; j < e.size(); j++)
      |                        ~~^~~~~~~~~~
Main.cpp:98:22: 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]
   98 |     for (ll i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
Main.cpp:101:22: 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]
  101 |     for (ll i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
Main.cpp:111:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |             if (pos != hs.size() and hs[pos] == p) rig[i] = mid + 1, l = mid + 1;
      |                 ~~~~^~~~~~~~~~~~
Main.cpp:116:22: 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]
  116 |     for (ll i = 0; i < s.size(); i++) st.set(i, rig[i]);
      |                    ~~^~~~~~~~~~
Main.cpp:119:22: 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]
  119 |     for (ll i = 0; i < s.size(); i++) up[0][i] = rig[i] == i ? i : st.query(i, rig[i]).second;
      |                    ~~^~~~~~~~~~
Main.cpp:122:26: 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]
  122 |         for (ll i = 0; i <= s.size(); i++) up[bit][i] = up[bit - 1][up[bit - 1][i]];
      |                        ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 16220 KB Output is correct
2 Correct 99 ms 22776 KB Output is correct
3 Correct 95 ms 22176 KB Output is correct
4 Correct 104 ms 23736 KB Output is correct
5 Correct 108 ms 22972 KB Output is correct
6 Correct 112 ms 24260 KB Output is correct
7 Correct 116 ms 24364 KB Output is correct
8 Correct 122 ms 24504 KB Output is correct
9 Correct 109 ms 23732 KB Output is correct
10 Correct 73 ms 20612 KB Output is correct
11 Correct 87 ms 23496 KB Output is correct
12 Correct 61 ms 21124 KB Output is correct
13 Correct 75 ms 22672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 732 ms 82144 KB Output is correct
2 Correct 798 ms 83772 KB Output is correct
3 Correct 700 ms 82396 KB Output is correct
4 Correct 640 ms 81444 KB Output is correct
5 Correct 612 ms 81644 KB Output is correct
6 Correct 797 ms 83604 KB Output is correct
7 Correct 808 ms 84240 KB Output is correct
8 Correct 698 ms 83264 KB Output is correct
9 Correct 762 ms 83724 KB Output is correct
10 Correct 785 ms 84252 KB Output is correct
11 Correct 650 ms 82364 KB Output is correct
12 Correct 655 ms 82212 KB Output is correct
13 Correct 739 ms 84468 KB Output is correct
14 Correct 776 ms 84044 KB Output is correct
15 Correct 501 ms 81364 KB Output is correct
16 Correct 702 ms 84944 KB Output is correct
17 Correct 477 ms 81784 KB Output is correct
18 Correct 512 ms 81464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 16220 KB Output is correct
2 Correct 99 ms 22776 KB Output is correct
3 Correct 95 ms 22176 KB Output is correct
4 Correct 104 ms 23736 KB Output is correct
5 Correct 108 ms 22972 KB Output is correct
6 Correct 112 ms 24260 KB Output is correct
7 Correct 116 ms 24364 KB Output is correct
8 Correct 122 ms 24504 KB Output is correct
9 Correct 109 ms 23732 KB Output is correct
10 Correct 73 ms 20612 KB Output is correct
11 Correct 87 ms 23496 KB Output is correct
12 Correct 61 ms 21124 KB Output is correct
13 Correct 75 ms 22672 KB Output is correct
14 Correct 732 ms 82144 KB Output is correct
15 Correct 798 ms 83772 KB Output is correct
16 Correct 700 ms 82396 KB Output is correct
17 Correct 640 ms 81444 KB Output is correct
18 Correct 612 ms 81644 KB Output is correct
19 Correct 797 ms 83604 KB Output is correct
20 Correct 808 ms 84240 KB Output is correct
21 Correct 698 ms 83264 KB Output is correct
22 Correct 762 ms 83724 KB Output is correct
23 Correct 785 ms 84252 KB Output is correct
24 Correct 650 ms 82364 KB Output is correct
25 Correct 655 ms 82212 KB Output is correct
26 Correct 739 ms 84468 KB Output is correct
27 Correct 776 ms 84044 KB Output is correct
28 Correct 501 ms 81364 KB Output is correct
29 Correct 702 ms 84944 KB Output is correct
30 Correct 477 ms 81784 KB Output is correct
31 Correct 512 ms 81464 KB Output is correct
32 Correct 474 ms 63148 KB Output is correct
33 Correct 635 ms 65648 KB Output is correct
34 Correct 662 ms 66612 KB Output is correct
35 Correct 654 ms 65988 KB Output is correct
36 Correct 669 ms 66940 KB Output is correct
37 Correct 471 ms 63364 KB Output is correct
38 Correct 1118 ms 90328 KB Output is correct
39 Correct 948 ms 88116 KB Output is correct
40 Correct 1162 ms 91132 KB Output is correct
41 Correct 1117 ms 90896 KB Output is correct
42 Correct 1057 ms 88792 KB Output is correct
43 Correct 430 ms 45752 KB Output is correct
44 Correct 446 ms 45708 KB Output is correct
45 Correct 416 ms 44956 KB Output is correct
46 Correct 434 ms 47392 KB Output is correct
47 Correct 874 ms 86784 KB Output is correct
48 Correct 1144 ms 88252 KB Output is correct
49 Correct 1012 ms 87060 KB Output is correct
50 Correct 1288 ms 90180 KB Output is correct
51 Correct 428 ms 85188 KB Output is correct
52 Correct 698 ms 86284 KB Output is correct
53 Correct 458 ms 85552 KB Output is correct