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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  string s;
  cin >> s;
  int n = sz(s);
  vi m(n);
  m[n - 1] = (int)(s[n - 1] - '0');
  int d = 10;
  REPR(i, n - 1) {
    m[i] = (m[i + 1] + (int)(s[i] - '0') * d) % 2019;
    d = (d * 10) % 2019;
  }
  // printv(m);
  map<int, int> mp;
  mp[0]++;
  for (auto x : m)
    mp[x]++;

  ll ans = 0;
  for (auto p : mp) {
    // cout << p.first << ": " << p.second << endl;
    ll c = p.second;
    if (c > 1)
      ans += c * (c - 1) / 2;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

template <typename T> T calc_mod(string s, T d) {
  T r = 0;
  int n = sz(s);
  REP(i, n) r = (r * 10 + (s[i] - '0')) % d;
  return r;
}

void solve() {
  string s;
  cin >> s;
  int n = sz(s);
  vi re(n);
  vi sum(n + 1);
  REP(i, n) {
    re[i] = (int)(s[i] - '0') % 3;
    sum[i + 1] = sum[i] + re[i];
  }
  // printv(re);
  // printv(sum);

  int l = 0;
  int r = 4;
  ll ans = 0;
  while (l < n and r < n) {
    if (calc_mod(s.substr(l, r - l + 1), 2019) == 0)
      ans++;
    r++;
    while (r < n and (sum[r + 1] - sum[l]) % 3 != 0)
      r++;
    if (r >= n) {
      l++;
      r = l + 4;
    }
  }
  cout << ans << endl;
}