Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

map<char, vector<int>> m3;
string s, t;
int binarySearch(int i, int l, int r, int x) {
  while (l <= r) {
    int m = l + (r - l) / 2;

    // Check if x is present at mid
    if (m == m3[t[i]].size() - 1)
      return m;
    if ((m3[t[i]][m] <= x) && (m3[t[i]][m + 1] > x))
      return m + 1;

    // If x greater, ignore left half
    if (m3[t[i]][m] < x)
      l = m + 1;

    // If x is smaller, ignore right half
    else
      r = m - 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  map<char, int> m1, m2;
  for (int i = 0; i < n; i++) {
    m1[s[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    m2[t[i]] = 1;
  }
  map<char, int>::iterator it = m2.begin();
  for (it; it != m2.end(); it++) {
    if (m1[it->first] != 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    m3[s[i]].push_back(i);
  }
  map<char, vector<int>>::iterator it1 = m3.begin();
  for (it1; it1 != m3.end(); it1++) {
    sort((it1->second).begin(), (it1->second).end());
  }
  ll res = 0;
  int cnt = 0;
  int last = 0;
  for (int i = 0; i < m; i++) {
    if (i == 0) {
      res += (1 + m3[t[0]][0]);
      last = m3[t[0]][0];
      continue;
    }
    int taille = m3[t[i]].size();
    int el = 0;
    if (last >= m3[t[i]][taille - 1])
      el = m3[t[i]][0];
    else {
      el = m3[t[i]][binarySearch(i, 0, taille - 1, last)];
    }
    if (el > last) {
      res += (el - last);
    } else {
      res += (el + 1);
      res += (n - last - 1);
    }
    last = el;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

map<char, vector<int>> m3;
string s, t;
int binarySearch(int i, int l, int r, int x) {
  while (l <= r) {
    int m = l + (r - l) / 2;

    // Check if x is present at mid
    if (m == m3[t[i]].size() - 1)
      return m;
    if ((m3[t[i]][m] <= x) && (m3[t[i]][m + 1] > x))
      return m + 1;

    // If x greater, ignore left half
    if (m3[t[i]][m] < x)
      l = m + 1;

    // If x is smaller, ignore right half
    else
      r = m - 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  map<char, int> m1, m2;
  for (int i = 0; i < n; i++) {
    m1[s[i]] = 1;
  }
  for (int i = 0; i < m; i++) {
    m2[t[i]] = 1;
  }
  map<char, int>::iterator it = m2.begin();
  for (it; it != m2.end(); it++) {
    if (m1[it->first] != 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    m3[s[i]].push_back(i);
  }
  map<char, vector<int>>::iterator it1 = m3.begin();
  for (it1; it1 != m3.end(); it1++) {
    sort((it1->second).begin(), (it1->second).end());
  }
  ll res = 0;
  int cnt = 0;
  int last = 0;
  for (int i = 0; i < m; i++) {
    if (i == 0) {
      res += (1 + m3[t[0]][0]);
      last = m3[t[0]][0];
      continue;
    }
    int taille = m3[t[i]].size();
    int el = 0;
    if (last >= m3[t[i]][taille - 1])
      el = m3[t[i]][0];
    else {
      el = m3[t[i]][binarySearch(i, 0, taille - 1, last)];
    }
    if (el > last) {
      res += (el - last);
    } else {
      res += (el + 1);
      res += (n - last - 1);
    }
    last = el;
  }
  cout << res << endl;
  return 0;
}