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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef unsigned long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int MX = 200010;
ll pw[MX], pws[MX], hs[MX];
int n, q;
string s;

map<pi, char> iv;

void addiv(int l, int r, char c) {
  while (1) {
    auto it = iv.lower_bound(pi(r, 0));
    if (it == iv.begin())
      break;
    --it;
    int a = it->first.first, b = it->first.second;
    if (a >= r || b <= l)
      break;

    if (b > r)
      iv[pi(r, b)] = it->second;
    if (a < l)
      iv[pi(a, l)] = it->second;
    iv.erase(it);
  }
  iv[pi(l, r)] = c;
}
char findiv(int l, int r) {
  auto it = iv.lower_bound(pi(r, 0));
  if (it == iv.begin())
    return 0;
  --it;
  if (it->first.first <= l && r <= it->first.second)
    return it->second;
  return 0;
}
void findpos(int l, int r, set<int> &res) {
  auto it = iv.lower_bound(pi(l, 0));
  if (it != iv.begin()) {
    --it;
    if (it->first.second <= l)
      ++it;
  }
  for (; it != iv.end() && it->first.first < r; ++it) {
    if (it->first.first >= l)
      res.insert(it->first.first - l);
    if (it->first.second < r)
      res.insert(it->first.second - l);
  }
}

void build() {
  for (auto i : iv)
    for (int j = i.first.first; j < i.first.second; j++)
      s[j] = i.second;
  iv.clear();
  rep(i, n) hs[i + 1] = hs[i] + (s[i] - 'a' + 1) * pw[i];
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  pw[0] = 1;
  rep(i, MX - 1) {
    pw[i + 1] = pw[i] * 29;
    pws[i + 1] = pws[i] + pw[i];
  }
  cin >> n >> s >> q;
  const int B = 500;
  rep(it, q) {
    if (it % B == 0)
      build();
    string inst;
    int x, y, z, w;
    cin >> inst >> x >> y;
    if (inst[0] == 's') {
      char c;
      cin >> c;
      x--;
#if 0
			for(int i = x; i < y; i++) s[i] = c;
#endif
      addiv(x, y, c);
      /*
      for(auto i : iv){
              cerr<<i.first.first<<" "<<i.first.second <<" : " <<(i.second ?
      i.second : '0')<<endl;
      }
      cerr<<endl;
      */
      continue;
    }
    cin >> z >> w;
    x--;
    z--;
#if 0
		for(int i = x, j = z; ; i++, j++){
			if(i >= y && j >= w){
				cout << 'e' << endl;
				break;
			}
			else if(i >= y){
				cout << 's' << endl;
				break;
			}
			else if(j >= w){
				cout << 't' << endl;
				break;
			}
			if(s[i] != s[j]){
				cout << (s[i] < s[j] ? 's' : 't') << endl;
				break;
			}
		}
#endif
    auto calc = [&]() {
      set<int> pos = {0, y - x, w - z};
      findpos(x, y, pos);
      findpos(z, w, pos);
      vi p(all(pos));
      ll ha = 0, hb = 0;
      ll curha, curhb;
      rep(i, p.size() - 1) {
        int cur = p[i], next = p[i + 1];

        if (cur == y - x) {
          if (cur == w - z)
            assert(0);
          return -1;
        } else if (cur == w - z)
          return 1;

        char ca = findiv(x + cur, x + next);
        char cb = findiv(z + cur, z + next);

        auto calc_ha = [&](int l, int r, char c) {
          if (c == 0)
            return hs[r] - hs[l];
          return pws[r - l] * (c - 'a' + 1) * pw[l];
        };

        curha = calc_ha(x + cur, x + next, ca);
        curhb = calc_ha(z + cur, z + next, cb);

        if (0) {
          char ca = findiv(x + cur, x + next);
          char cb = findiv(z + cur, z + next);
          string sa, sb;
          rep(j, next - cur) {
            sa += ca ? ca : s[x + cur + j];
            sb += cb ? cb : s[z + cur + j];
          }
          dbg(sa, sb, x, z);
          dbg(curha, curhb);
          dbg(cur, next, ca, cb);
        }

        if (curha * pw[n - x - cur] == curhb * pw[n - z - cur]) {
          ha += curha;
          hb += curhb;
          continue;
        }

        int lo = 0, hi = next - cur, mid;
        while (lo + 1 < hi) {
          mid = (lo + hi) / 2;
          ll h1 = calc_ha(x + cur, x + cur + mid, ca);
          ll h2 = calc_ha(z + cur, z + cur + mid, cb);
          if (h1 * pw[n - x - cur] == h2 * pw[n - z - cur])
            lo = mid;
          else
            hi = mid;
        }

        if ((ca ? ca : s[x + cur + lo]) < (cb ? cb : s[z + cur + lo]))
          return -1;
        return 1;
        /*
        rep(j, next - cur){
                char a = ca ? ca : s[x + cur + j];
                char b = cb ? cb : s[z + cur + j];
                if(j < lo) assert(a == b);
                else assert(a != b);
                if(a != b) return a < b ? -1 : 1;
        }
        */
        assert(0);
      }
      return 0;
    };
    int res = calc();
    cout << (res == 0 ? 'e' : res < 0 ? 's' : 't') << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef unsigned long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int MX = 200010;
ll pw[MX], pws[MX], hs[MX];
int n, q;
string s;

map<pi, char> iv;

void addiv(int l, int r, char c) {
  while (1) {
    auto it = iv.lower_bound(pi(r, 0));
    if (it == iv.begin())
      break;
    --it;
    int a = it->first.first, b = it->first.second;
    if (a >= r || b <= l)
      break;

    if (b > r)
      iv[pi(r, b)] = it->second;
    if (a < l)
      iv[pi(a, l)] = it->second;
    iv.erase(it);
  }
  iv[pi(l, r)] = c;
}
char findiv(int l, int r) {
  auto it = iv.lower_bound(pi(r, 0));
  if (it == iv.begin())
    return 0;
  --it;
  if (it->first.first <= l && r <= it->first.second)
    return it->second;
  return 0;
}
void findpos(int l, int r, set<int> &res) {
  auto it = iv.lower_bound(pi(l, 0));
  if (it != iv.begin()) {
    --it;
    if (it->first.second <= l)
      ++it;
  }
  for (; it != iv.end() && it->first.first < r; ++it) {
    if (it->first.first >= l)
      res.insert(it->first.first - l);
    if (it->first.second < r)
      res.insert(it->first.second - l);
  }
}

void build() {
  for (auto i : iv)
    for (int j = i.first.first; j < i.first.second; j++)
      s[j] = i.second;
  iv.clear();
  rep(i, n) hs[i + 1] = hs[i] + (s[i] - 'a' + 1) * pw[i];
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  pw[0] = 1;
  rep(i, MX - 1) {
    pw[i + 1] = pw[i] * 29;
    pws[i + 1] = pws[i] + pw[i];
  }
  cin >> n >> s >> q;
  const int B = 500;
  rep(it, q) {
    if (it % B == 0)
      build();
    string inst;
    int x, y, z, w;
    cin >> inst >> x >> y;
    if (inst[0] == 's') {
      char c;
      cin >> c;
      x--;
#if 0
			for(int i = x; i < y; i++) s[i] = c;
#endif
      addiv(x, y, c);
      /*
      for(auto i : iv){
              cerr<<i.first.first<<" "<<i.first.second <<" : " <<(i.second ?
      i.second : '0')<<endl;
      }
      cerr<<endl;
      */
      continue;
    }
    cin >> z >> w;
    x--;
    z--;
#if 0
		for(int i = x, j = z; ; i++, j++){
			if(i >= y && j >= w){
				cout << 'e' << endl;
				break;
			}
			else if(i >= y){
				cout << 's' << endl;
				break;
			}
			else if(j >= w){
				cout << 't' << endl;
				break;
			}
			if(s[i] != s[j]){
				cout << (s[i] < s[j] ? 's' : 't') << endl;
				break;
			}
		}
#endif
    auto calc = [&]() {
      set<int> pos = {0, y - x, w - z};
      findpos(x, y, pos);
      findpos(z, w, pos);
      vi p(all(pos));
      ll ha = 0, hb = 0;
      ll curha, curhb;
      rep(i, p.size() - 1) {
        int cur = p[i], next = p[i + 1];

        if (cur == y - x) {
          if (cur == w - z)
            assert(0);
          return -1;
        } else if (cur == w - z)
          return 1;

        char ca = findiv(x + cur, x + next);
        char cb = findiv(z + cur, z + next);

        auto calc_ha = [&](int l, int r, char c) {
          if (c == 0)
            return hs[r] - hs[l];
          return pws[r - l] * (c - 'a' + 1) * pw[l];
        };

        curha = calc_ha(x + cur, x + next, ca);
        curhb = calc_ha(z + cur, z + next, cb);

        if (0) {
          char ca = findiv(x + cur, x + next);
          char cb = findiv(z + cur, z + next);
          string sa, sb;
          rep(j, next - cur) {
            sa += ca ? ca : s[x + cur + j];
            sb += cb ? cb : s[z + cur + j];
          }
          dbg(sa, sb, x, z);
          dbg(curha, curhb);
          dbg(cur, next, ca, cb);
        }

        if (curha * pw[n - x - cur] == curhb * pw[n - z - cur]) {
          ha += curha;
          hb += curhb;
          continue;
        }

        int lo = 0, hi = next - cur, mid;
        while (lo + 1 < hi) {
          mid = (lo + hi) / 2;
          ll h1 = calc_ha(x + cur, x + cur + mid, ca);
          ll h2 = calc_ha(z + cur, z + cur + mid, cb);
          if (h1 * pw[n - x - cur] == h2 * pw[n - z - cur])
            lo = mid;
          else
            hi = mid;
        }
        /*
        if((ca ? ca : s[x + cur + lo]) < (cb ? cb : s[z + cur + lo])) return -1;
        return 1;
        */
        rep(j, next - cur) {
          char a = ca ? ca : s[x + cur + j];
          char b = cb ? cb : s[z + cur + j];
          if (j < lo)
            assert(a == b);
          else
            assert(a != b);
          if (a != b)
            return a < b ? -1 : 1;
        }

        assert(0);
      }
      return 0;
    };
    int res = calc();
    cout << (res == 0 ? 'e' : res < 0 ? 's' : 't') << endl;
  }
  return 0;
}