Submission #1001864

# Submission time Handle Problem Language Result Execution time Memory
1001864 2024-06-19T08:08:56 Z vjudge1 Dabbeh (INOI20_dabbeh) C++17
0 / 100
2000 ms 74380 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define inf 0x3F3F3F3F3F3F3F3F

const int MXN = 3e5 + 5;
const int LOG = 20;
const __int128 mod = 1000000000000000009;
__int128 b1, b2;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

__int128 add(__int128 a, __int128 b)
{
  return (a + b + mod) % mod;
}
__int128 mult(__int128 a, __int128 b)
{
  return (a * b) % mod;
}
__int128 pw(__int128 a, __int128 b, __int128 c)
{
  a %= c;
  __int128 res = 1;
  while (b)
  {
    if (b & 1) res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

__int128 ph[2][MXN], p[2][MXN], invp[2][MXN];

__int128 get_hash(int l, int r, int id)
{
  return mult(add(ph[id][r], -ph[id][l - 1]), invp[id][l]);
}

void _HASH(string &s, int id)
{
  for (int i = 1; i < s.length(); i++)
  {
    ph[id][i] = add(ph[id][i - 1], mult(p[id][i], s[i] - 'a' + 1));
  }
}

map<array<__int128, 2>, int> mp;
int R[MXN];
int ans[MXN];
vector<array<int, 2>> q[MXN];
int n, k, m;
string s, t[MXN];
int par[LOG][MXN];

int inter(array<int, 2> x, array<int, 2> y)
{
  return max(x[0], y[0]) <= min(x[1], y[1]);
}
signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  b1 = rng() % (mod - 1) + 1, b2 = rng() % (mod - 1) + 1;
  p[0][0] = p[1][0] = 1, invp[0][0] = invp[1][0] = 1;
  for (int i = 1; i < MXN; i++) 
  {
    p[0][i] = mult(p[0][i - 1], b1);
    p[1][i] = mult(p[1][i - 1], b2);
    invp[0][i] = pw(p[0][i], mod - 2, mod);
    invp[1][i] = pw(p[1][i], mod - 2, mod);
  }
  cin >> k >> m;
  for (int i = 0; i < k; i++) cin >> t[i];
  cin >> s;
  n = s.length();
  s = "#" + s;
  _HASH(s, 0);
  _HASH(s, 1);
  for (int i = 0; i < k; i++)
  {
    __int128 h1 = 0, h2 = 0;
    for (int j = 0; j < t[i].length(); j++)
    {
      h1 = add(h1, mult(t[i][j] - 'a' + 1, p[0][j]));
      h2 = add(h2, mult(t[i][j] - 'a' + 1, p[1][j]));
      mp[{h1, h2}] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
  {
    int l = i, r = n;
    if (mp.find({get_hash(i, i, 0), get_hash(i, i, 1)}) == mp.end()) continue;
    while (l < r)
    {
      int mid = (l + r + 1) >> 1;
      if (mp.find({get_hash(i, mid, 0), get_hash(i, mid, 1)}) != mp.end()) l = mid;
      else r = mid - 1;
    }
    R[i] = l + 1;
  }
  // for (int i = 1; i <= n; i++ )cout << R[i] << ' ';
  // cout << '\n';
  vector<int> v;
  for (int i = n; i >= 1; i--)
  {
  //   cout << i << ' '  <<R[i]<<": ";
  //   for (int i : v) cout << i << ' ';
  //   cout << '\n';
    if (v.empty() || !R[i])
    {
      par[0][i] = 0;
    }
    else
    {
      int l = 0, r = (int)v.size() - 1;
      while (l < r)
      {
        int mid = (l + r) >> 1;
        if (v[mid] > R[i]) l = mid + 1;
        else r = mid;
      }
      // cout << "*" << v[r] << '\n';
      if (v[r] <= R[i]) par[0][i] = v[r];
    }
    if (R[i])
    {
      while (!v.empty() && R[v.back()] <= R[i]) v.pop_back();
      v.push_back(i);
    }
  }
  // for (int i = 1; i <= n; i++) cout << par[0][i] << ' ';
  // cout << '\n';
  for (int i = 1; i < LOG; i++) for (int j = 1; j <= n; j++) par[i][j] = par[i - 1][par[i - 1][j]];
  for (int i = 0; i < m; i++)
  {
    int l, r, res = 0;
    cin >> l >> r;
    l++;
    for (int i = LOG - 1; i >= 0; i--)
    {
      if (!par[i][l] || par[i][l] > r) continue;
      l = par[i][l];
      res |= (1LL << i);
    }
    if (R[l] <= r) cout << -1 << '\n';
    else cout << res + 1 << '\n';
  }
}

Compilation message

Main.cpp: In function 'void _HASH(std::string&, long long int)':
Main.cpp:45:21: 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]
   45 |   for (int i = 1; i < s.length(); i++)
      |                   ~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:86:23: 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]
   86 |     for (int j = 0; j < t[i].length(); j++)
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 381 ms 35848 KB Output is correct
2 Incorrect 617 ms 74380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2081 ms 62776 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 381 ms 35848 KB Output is correct
2 Incorrect 617 ms 74380 KB Output isn't correct
3 Halted 0 ms 0 KB -