Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

class SuffixArray {
public:
  string &s;
  vector<int> sa;
  int n;
  SuffixArray(string &str) : s(str) { n = s.size(); }
  void construct_sa() {
    sa.resize(n + 1);
    vector<int> rank(n + 1);
    rep(i, n + 1) {
      sa[i] = i;
      rank[i] = (i < n) ? s[i] : (-1);
    }
    vector<int> tmp(n + 1, 0);
    for (int k = 1; k <= n; k *= 2) {
      auto comp = [&](int i, int j) {
        if (rank[i] != rank[j])
          return rank[i] < rank[j];
        int ri = (i + k <= n) ? (rank[i + k]) : -1;
        int rj = (j + k <= n) ? (rank[j + k]) : -1;
        return ri < rj;
      };
      sort(all(sa), comp);
      tmp[sa[0]] = 0;
      repl(i, 1, n + 1) {
        tmp[sa[i]] = tmp[sa[i - 1]];
        if (comp(sa[i - 1], sa[i]))
          tmp[sa[i]]++;
      }
      swap(tmp, rank);
    }
  }
  bool contain(const string &t) {
    int m = t.size();
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (s.compare(sa[mid], m, t) < 0)
        l = mid;
      else
        r = mid;
    }
    return s.compare(sa[r], m, t) == 0;
  }
};

int main() {
  string t;
  int q;
  cin >> t >> q;

  SuffixArray sa(t);
  sa.construct_sa();

  rep(_, q) {
    string p;
    cin >> p;
    cout << sa.contain(p) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

class SuffixArray {
public:
  string &s;
  vector<int> sa;
  int n;
  SuffixArray(string &str) : s(str) { n = s.size(); }
  void construct_sa() {
    sa.resize(n + 1);
    vector<int> rank(n + 1);
    rep(i, n + 1) {
      sa[i] = i;
      rank[i] = (i < n) ? s[i] : (-1);
    }
    for (int k = 1; k <= n; k *= 2) {
      function<bool(int, int)> comp = [&](int i, int j) {
        if (rank[i] != rank[j])
          return rank[i] < rank[j];
        int ri = (i + k <= n) ? (rank[i + k]) : -1;
        int rj = (j + k <= n) ? (rank[j + k]) : -1;
        return ri < rj;
      };
      sort(all(sa), comp);
      vector<int> tmp(n + 1, 0);
      tmp[sa[0]] = 0;
      repl(i, 1, n + 1) {
        tmp[sa[i]] = tmp[sa[i - 1]];
        if (comp(sa[i - 1], sa[i]))
          tmp[sa[i]]++;
      }
      swap(tmp, rank);
    }
  }
  bool contain(const string &t) {
    int m = t.size();
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (s.compare(sa[mid], m, t) < 0)
        l = mid;
      else
        r = mid;
    }
    return s.compare(sa[r], m, t) == 0;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string t;
  int q;
  cin >> t >> q;

  SuffixArray sa(t);
  sa.construct_sa();

  rep(_, q) {
    string p;
    cin >> p;
    cout << sa.contain(p) << endl;
  }

  return 0;
}