Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--Range Set Counter
//-------------------------------------------------
template <typename T> class RangeSet {
private:
  ::std::vector<int> v;
  ::std::unordered_map<T, int> Map;
  ::std::vector<::std::vector<int>> data;
  int kind;

public:
  RangeSet(const ::std::vector<T> &v_) : v(v_.size()) {
    int sz = v_.size();
    kind = 0;
    for (int i = 0; i < sz; i++) {
      T t = v_[i];
      if (Map.count(t)) {
        data[Map[t]].push_back(i);
      } else {
        Map[t] = kind++;
        data.push_back(::std::vector<int>());
        data.back().push_back(i);
      }
      v[i] = Map[t];
    }
  }
  // query for all elements
  int query(T x) {
    if (!Map.count(x))
      return 0;
    return data[Map[x]].size();
  }
  // query for [a, b]
  int query(int a, int b, T x) {
    if (a > b)
      return 0;
    if (!Map.count(x))
      return 0;
    int xi = Map[x];
    auto right = ::std::upper_bound(data[xi].begin(), data[xi].end(), b);
    auto left = ::std::lower_bound(data[xi].begin(), data[xi].end(), a);
    return right - left;
  }
  // search x for [k, tail]
  int find(int k, T x) {
    if (!Map.count(x))
      return -1;
    int xi = Map[x];
    auto itr = ::std::lower_bound(data[xi].begin(), data[xi].end(), k);
    if (itr != data[xi].end()) {
      return *itr;
    } else {
      return -1;
    }
  }
};

const int MOD = 2019;

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  vi dp(n + 1), d10;
  d10.pb(1);
  rep(i, n) dp[i + 1] = (dp[i] * 10 + (s[i] - '0')) % MOD;
  do {
    d10.pb(d10.back() * 10 % MOD);
  } while (d10.back() != 1);
  int C = d10.size() - 1;
  rep(i, n + 1) dp[i] = dp[i] * d10[C - i % C] % MOD;

  RangeSet<int> rs(dp);
  ll ans = 0;
  rep(i, n) { ans += rs.query(i + 1, n, dp[i]); }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--Range Set Counter
//-------------------------------------------------
template <typename T> class RangeSet {
private:
  ::std::vector<int> v;
  ::std::unordered_map<T, int> Map;
  ::std::vector<::std::vector<int>> data;
  int kind;

public:
  RangeSet(const ::std::vector<T> &v_) : v(v_.size()) {
    int sz = v_.size();
    kind = 0;
    for (int i = 0; i < sz; i++) {
      T t = v_[i];
      if (Map.count(t)) {
        data[Map[t]].push_back(i);
      } else {
        Map[t] = kind++;
        data.push_back(::std::vector<int>());
        data.back().push_back(i);
      }
      v[i] = Map[t];
    }
  }
  // query for all elements
  int query(T x) {
    if (!Map.count(x))
      return 0;
    return data[Map[x]].size();
  }
  // query for [a, b]
  int query(int a, int b, T x) {
    if (a > b)
      return 0;
    if (!Map.count(x))
      return 0;
    int xi = Map[x];
    auto right = ::std::upper_bound(data[xi].begin(), data[xi].end(), b);
    auto left = ::std::lower_bound(data[xi].begin(), data[xi].end(), a);
    return right - left;
  }
  // search x for [k, tail]
  int find(int k, T x) {
    if (!Map.count(x))
      return -1;
    int xi = Map[x];
    auto itr = ::std::lower_bound(data[xi].begin(), data[xi].end(), k);
    if (itr != data[xi].end()) {
      return *itr;
    } else {
      return -1;
    }
  }
};

const int MOD = 2019;

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  vi dp(n + 1), d10;
  d10.pb(1);
  rep(i, n) dp[i + 1] = (dp[i] * 10 + (s[i] - '0')) % MOD;
  do {
    d10.pb(d10.back() * 10 % MOD);
  } while (d10.back() != 1);
  int C = d10.size() - 1;
  rep(i, n + 1) dp[i] = dp[i] * d10[C - i] % MOD;

  RangeSet<int> rs(dp);
  ll ans = 0;
  rep(i, n) { ans += rs.query(i + 1, n, dp[i]); }
  cout << ans << "\\n";
  return 0;
}