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

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c, ans;
string s, str;
char ch;
vector<int> v;

void solve() {
  cin >> n;
  unordered_map<int, int> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (s.find(i - arr[i]) != s.end())
      ans += s[i - arr[i]];
    s[i + arr[i]] += 1;
  }
  cout << ans << "\\n";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c, ans;
string s, str;
char ch;
vector<int> v;

void solve() {
  cin >> n;
  multiset<int> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    ans += s.count(i - arr[i]);
    s.insert(i + arr[i]);
  }
  cout << ans << "\\n";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}