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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <sstream>
#include <vector>

using namespace std;
typedef long long ll;

template <typename T, int COUNT> class BinaryIndexedTree {
private:
  T m_data[COUNT + 1];

public:
  BinaryIndexedTree() { fill(m_data, m_data + COUNT + 1, T()); }
  T sum(int i) {
    T s = T();
    while (i > 0) {
      s += m_data[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, const T &x) {
    while (i <= COUNT) {
      m_data[i] += x;
      i += i & -i;
    }
  }
};

struct LexicalCompare {
  bool operator()(int a, int b) const {
    char abuf[16] = {0}, bbuf[16] = {0};
    int alen = 0, blen = 0;
    while (a > 0) {
      abuf[alen++] = '0' + (a % 10);
      a /= 10;
    }
    while (b > 0) {
      bbuf[blen++] = '0' + (b % 10);
      b /= 10;
    }
    reverse(abuf, abuf + alen);
    reverse(bbuf, bbuf + blen);
    for (int i = 0; i < 16; ++i) {
      if (abuf[i] < bbuf[i]) {
        return true;
      }
      if (abuf[i] > bbuf[i]) {
        return false;
      }
    }
    return false;
  }
};

int main() {
  while (true) {
    ll A, B, P;
    cin >> A >> B >> P;
    if (A == 0 && B == 0 && P == 0) {
      break;
    }
    vector<int> lexical_order(B - A + 1);
    for (int i = A; i <= B; ++i) {
      lexical_order[i - A] = i;
    }
    sort(lexical_order.begin(), lexical_order.end(), LexicalCompare());
    int N = lexical_order.size();
    BinaryIndexedTree<ll, 100001> bit;
    vector<ll> dp(N);
    for (int i = 0; i < N; ++i) {
      dp[i] = (bit.sum(lexical_order[i] - A) + 1) % P;
      bit.add(lexical_order[i] - A + 1, dp[i]);
    }
    ll answer = accumulate(dp.begin(), dp.end(), 0ll) % P;
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <sstream>
#include <vector>

using namespace std;
typedef long long ll;

template <typename T, int COUNT> class BinaryIndexedTree {
private:
  T m_data[COUNT + 1];

public:
  BinaryIndexedTree() { fill(m_data, m_data + COUNT + 1, T()); }
  T sum(int i) {
    T s = T();
    while (i > 0) {
      s += m_data[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, const T &x) {
    while (i <= COUNT) {
      m_data[i] += x;
      i += i & -i;
    }
  }
};

struct LexicalCompare {
  bool operator()(ll a, ll b) const {
    char abuf[16] = {0}, bbuf[16] = {0};
    int alen = 0, blen = 0;
    while (a > 0) {
      abuf[alen++] = '0' + (a % 10);
      a /= 10;
    }
    while (b > 0) {
      bbuf[blen++] = '0' + (b % 10);
      b /= 10;
    }
    reverse(abuf, abuf + alen);
    reverse(bbuf, bbuf + blen);
    for (int i = 0; i < 16; ++i) {
      if (abuf[i] < bbuf[i]) {
        return true;
      }
      if (abuf[i] > bbuf[i]) {
        return false;
      }
    }
    return false;
  }
};

int main() {
  while (true) {
    ll A, B, P;
    cin >> A >> B >> P;
    if (A == 0 && B == 0 && P == 0) {
      break;
    }
    vector<ll> lexical_order(B - A + 1);
    for (ll i = A; i <= B; ++i) {
      lexical_order[i - A] = i;
    }
    sort(lexical_order.begin(), lexical_order.end(), LexicalCompare());
    int N = lexical_order.size();
    BinaryIndexedTree<ll, 100001> bit;
    vector<ll> dp(N);
    for (int i = 0; i < N; ++i) {
      dp[i] = (bit.sum(lexical_order[i] - A) + 1) % P;
      bit.add(lexical_order[i] - A + 1, dp[i]);
    }
    ll answer = accumulate(dp.begin(), dp.end(), 0ll) % P;
    cout << answer << endl;
  }
  return 0;
}