Problem p01814 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long Hash;

// h[x] = str[0..x)
vector<Hash> diffs;

Hash h[100010];
Hash p[100010];
Hash w = 8999;

// [l,r)
Hash substr(int l, int r) { return h[r] - h[l] * p[r - l]; }
Hash concat(Hash a, Hash b, int blen) { return a * p[blen] + b; }

Hash makestr(int l, int r, int t) {
  int len = r - l;
  int Q = len / t;
  int R = len % t;
  Hash res = 0;

  if (l + t <= r) {

    Hash x = substr(l, l + t);
    int xlen = t;

    for (int i = 0; xlen <= len; i++) {
      if (Q >> i & 1) {
        res = concat(res, x, xlen);
      }

      x = concat(x, x, xlen);
      xlen *= 2;
    }
  }

  res = concat(res, substr(l, l + R), R);
  // cout << res << endl;
  return res;
}

Hash makestr2(int l, int r, int t) {
  int len = r - l;
  int Q = len / t;
  int R = len % t;
  int xlen = t;
  Hash x = substr(l + t, l + 2 * t);
  Hash res = 0;
  for (int i = 0; xlen <= len; i++) {
    if (Q >> i & 1) {
      res = concat(res, x, xlen);
    }

    x = concat(x, x, xlen);
    xlen *= 2;
  }

  res = concat(res, substr(l + t, l + t + R), R);
  // cout << res << endl;
  return res;
}

int main() {
  p[0] = 1;
  for (int i = 1; i <= 100000; i++)
    p[i] = p[i - 1] * w;
  string S;
  cin >> S;
  h[0] = 0;
  for (int i = 0; i < S.size(); i++) {
    h[i + 1] = h[i] * w + S[i];
  }

  diffs.push_back(0);
  for (char c = 0; c < 26; c++) {
    for (int i = 0; i < 100000; i++) {
      diffs.push_back(c * p[i]);
      diffs.push_back(-c * p[i]);
    }
  }
  sort(diffs.begin(), diffs.end());

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int l, r, t;
    cin >> l >> r >> t;
    --l;
    // cout << substr(l,r) - makestr(l,r,t)<< endl;

    if (binary_search(diffs.begin(), diffs.end(),
                      makestr(l, r, t) - substr(l, r))) {
      cout << "Yes" << endl;

    } else if (l + 2 * t <= S.size() and
               binary_search(diffs.begin(), diffs.end(),
                             makestr2(l, r, t) - substr(l, r))) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long Hash;

// h[x] = str[0..x)
vector<Hash> diffs;

Hash h[100010];
Hash p[100010];
Hash w = 8999;

// [l,r)
Hash substr(int l, int r) { return h[r] - h[l] * p[r - l]; }
Hash concat(Hash a, Hash b, int blen) { return a * p[blen] + b; }

Hash makestr(int l, int r, int t) {
  int len = r - l;
  int Q = len / t;
  int R = len % t;
  int xlen = t;
  Hash x = substr(l, l + t);
  Hash res = 0;
  for (int i = 0; i < 17; i++) {
    if (Q >> i & 1) {
      res = concat(res, x, xlen);
    }

    x = concat(x, x, xlen);
    xlen *= 2;
  }

  res = concat(res, substr(l, l + R), R);
  // cout << res << endl;
  return res;
}

Hash makestr2(int l, int r, int t) {
  int len = r - l;
  int Q = len / t;
  int R = len % t;
  int xlen = t;
  Hash x = substr(l + t, l + 2 * t);
  Hash res = 0;
  for (int i = 0; i < 17; i++) {
    if (Q >> i & 1) {
      res = concat(res, x, xlen);
    }

    x = concat(x, x, xlen);
    xlen *= 2;
  }

  res = concat(res, substr(l, l + R), R);
  // cout << res << endl;
  return res;
}

int main() {
  p[0] = 1;
  for (int i = 1; i <= 100000; i++)
    p[i] = p[i - 1] * w;
  string S;
  cin >> S;
  h[0] = 0;
  for (int i = 0; i < S.size(); i++) {
    h[i + 1] = h[i] * w + S[i];
  }

  diffs.push_back(0);
  for (char c = 0; c < 26; c++) {
    for (int i = 0; i < 100000; i++) {
      diffs.push_back(c * p[i]);
      diffs.push_back(-c * p[i]);
    }
  }
  sort(diffs.begin(), diffs.end());

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int l, r, t;
    cin >> l >> r >> t;
    --l;
    // cout << substr(l,r) - makestr(l,r,t)<< endl;
    if (binary_search(diffs.begin(), diffs.end(),
                      makestr(l, r, t) - substr(l, r))) {
      cout << "Yes" << endl;
    } else if (l + 2 * t <= S.size() and
               binary_search(diffs.begin(), diffs.end(),
                             makestr2(l, r, t) - substr(l, r))) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}