Problem p03793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const ll INF = 1e7;
const ll MOD = (1e9) + 7;
int main() {
  string s;
  cin >> s;
  vector<vector<ll>> nextc(
      'z' - 'a' + 2,
      vector<ll>(s.size(), INF)); // nextc['x'-'a'][l] = next_x[l]
  const ll mu = 'z' - 'a' + 1;
  for (ll i = s.size() - 1; i >= 0; i--) {
    nextc[s[i] - 'a'][i] = i + 1;
    for (char c = s[i] + 1 - 'a'; c <= 'z' - 'a' + 1; c++) {
      if (nextc[c - 1][i] < s.size())
        nextc[c][i] = nextc[c - 1][nextc[c - 1][i]];
    }

    for (char c = 0; c < s[i] - 'a'; c++) {
      if (nextc[mu][i] < s.size()) {
        nextc[c][i] = nextc[c][nextc[mu][i]];
      }
    }
  }
  vector<vector<ll>> nextl(s.size());
  for (ll i = s.size() - 1; i >= 0; i--) {
    ll to = nextc[mu][i];
    ll count = 0;
    while (to != INF) {
      nextl[i].push_back(to);
      if (to >= nextl.size() || count >= nextl[to].size())
        break;
      to = nextl[to][count];
      count++;
    }
  }
  ll Q;
  cin >> Q;
  for (ll q = 0; q < Q; q++) {
    ll l, r;
    cin >> l >> r;
    l--;
    while (l < nextl.size() && !nextl[l].empty() && l != r) {
      ll lf = 0;
      ll rf = nextl[l].size() - 1;
      if (nextl[l][rf] <= r)
        l = nextl[l][rf];
      else {
        while (rf - lf > 1) {
          ll m = (lf + rf) / 2;
          if (nextl[l][m] <= r)
            lf = m;
          else
            rf = m;
        }
        l = nextl[l][lf];
      }
    }
    if (l == r)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const ll INF = 1e7;
const ll MOD = (1e9) + 7;
int main() {
  string s;
  cin >> s;
  vector<vector<ll>> nextc(
      'z' - 'a' + 2,
      vector<ll>(s.size(), INF)); // nextc['x'-'a'][l] = next_x[l]
  const ll mu = 'z' - 'a' + 1;
  for (ll i = s.size() - 1; i >= 0; i--) {
    nextc[s[i] - 'a'][i] = i + 1;
    for (char c = s[i] + 1 - 'a'; c <= 'z' - 'a' + 1; c++) {
      if (nextc[c - 1][i] < s.size())
        nextc[c][i] = nextc[c - 1][nextc[c - 1][i]];
    }

    for (char c = 0; c < s[i] - 'a'; c++) {
      if (nextc[mu][i] < s.size()) {
        nextc[c][i] = nextc[c][nextc[mu][i]];
      }
    }
  }
  vector<vector<ll>> nextl(s.size());
  for (ll i = s.size() - 1; i >= 0; i--) {
    ll to = nextc[mu][i];
    ll count = 0;
    while (to != INF) {
      nextl[i].push_back(to);
      if (to >= nextl.size() || count >= nextl[to].size())
        break;
      to = nextl[to][count];
      count++;
    }
  }
  ll Q;
  cin >> Q;
  for (ll q = 0; q < Q; q++) {
    ll l, r;
    cin >> l >> r;
    l--;
    ll to = 0;
    while (to + 1 < nextl[l].size() && nextl[l][to + 1] <= r)
      to++;
    if (to < nextl[l].size())
      l = nextl[l][to];
    while (to > 0) {
      to--;
      if (l < nextl.size() && to < nextl[l].size() && nextl[l][to] <= r)
        l = nextl[l][to];
    }
    if (l == r)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}