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

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

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define lop(i, s, e) for (int i = s; i < (int)e; i++)
#define rlop(i, e, s) for (int i = e; i >= s; i--)
#define prvec(v)                                                               \\
  lop(R15, 0, v.size()) cout << v[R15] << " ";                                 \\
  cout << endl;
#define prarr(arr, s1, e1)                                                     \\
  lop(R15, s1, e1) cout << arr[R15] << " ";                                    \\
  cout << endl;

#define endl '\\n'
#define ff first
#define ss second
#define pii pair<int, int>

#define pb push_back
#define all(v) v.begin(), v.end()

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

int n, Q;
int arr[N + 2];

void solve() {
  string str;
  cin >> str;
  int n = str.length();

  int ans = 0, rem = 0;
  int p = 1;
  unordered_map<int, int> mp2;
  mp2[0]++;
  rlop(i, n - 1, 0) {
    int d = str[i] - '0';
    rem = (rem + p * (d)) % 2019;
    if (mp2.count(rem))
      ans += mp2[rem], mp2[rem]++;
    else
      mp2[rem] = 1;
    p = (10 * p) % 2019;
  }

  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  // cin >> test;
  while (test--)
    solve();
}


Error: TLE

Fixed submission:

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

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define lop(i, s, e) for (int i = s; i < (int)e; i++)
#define rlop(i, e, s) for (int i = e; i >= s; i--)
#define prvec(v)                                                               \\
  lop(R15, 0, v.size()) cout << v[R15] << " ";                                 \\
  cout << endl;
#define prarr(arr, s1, e1)                                                     \\
  lop(R15, s1, e1) cout << arr[R15] << " ";                                    \\
  cout << endl;

#define endl '\\n'
#define ff first
#define ss second
#define pii pair<int, int>

#define pb push_back
#define all(v) v.begin(), v.end()

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

int n, Q;
int arr[N + 2];

void solve() {
  string str;
  cin >> str;
  int n = str.length();

  map<int, int> mp;
  int rem = 0;
  mp[rem]++;
  vector<map<int, int>> vec(n);
  lop(i, 0, n) {
    int d = str[i] - '0';
    rem = (rem * 10 + (d)) % 2019;
    mp[rem]++;
    vec[i] = mp;
    // bug(i, rem);
  }

  int ans = 0;
  rem = 0;
  // cout << endl;
  int p = 1;
  map<int, int> mp2;
  mp2[0]++;
  rlop(i, n - 1, 0) {
    int d = str[i] - '0';
    rem = (rem + p * (d)) % 2019;

    // if (i) ans += vec[i - 1][rem];
    // else if ( rem == 0) ans++;
    // bug(i, rem);
    ans += mp2[rem];
    mp2[rem]++;

    p = (10 * p) % 2019;
  }

  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int test = 1;
  // cin >> test;
  while (test--)
    solve();
}