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

#include <bits/stdc++.h>

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;

#define MAX_NUM (1000000000 + 7)

using namespace std;

template <typename T> static inline void ArrayInput(vector<T> &A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    cin >> *itr;
}

template <typename T> static inline void ArrayPut(const vector<T> &A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    cout << *itr << " ";
  cout << endl;
}

template <typename T> static inline T ArraySum(vector<T> &A) {
  T res = 0;
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    res += *itr;
  return res;
}

bool Sec_compare(pair<uint64_t, uint64_t> a, pair<uint64_t, uint64_t> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

u64 dec_dig(u64 num) {
  u64 res = 0;
  while (num > 0) {
    num /= 10;
    ++res;
  }
  return res;
}

i64 modinv(i64 a, i64 m) {
  i64 b = m, u = 1, v = 0;
  while (b) {
    i64 t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

u64 gcd(u64 a, u64 b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}

u64 My_Comb(u64 n, u64 k, i64 m) {
  if (n < k)
    return 0;
  if (k == 0 || n == k)
    return 1;
  u64 res = 1;
  k = (n / 2 < k) ? n - k : k;
  for (u64 i = 1; i <= k; ++i)
    res = (((res * (n + 1 - i)) % m) * modinv(i, m)) % m;
  return res;
}

u64 My_Pow(u64 a, u64 n, i64 m) {
  u64 tmp = n, calc = 1;
  while (tmp > 0) {
    if (tmp % 2) {
      tmp--;
      calc *= a;
      calc %= m;
    } else {
      a *= a;
      a %= m;
      tmp /= 2;
    }
  }
  return calc;
}

int main() {
  cout << setprecision(18);
  u32 N;
  cin >> N;
  vector<i64> A(N);
  ArrayInput(A);
  map<i64, u64> CondI, CondJ;
  for (i64 i = 0; i < N; ++i) {
    CondI[A[i] + (i + 1)]++;
    CondJ[(i + 1) - A[i]]++;
  }
  u64 res = 0;
  for (i64 i = 0; i < N; ++i) {
    res += CondJ[A[i] + (i + 1)];
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define MAX_NUM (1000000000 + 7)

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;

using namespace std;

template <typename T> static inline void ArrayInput(vector<T> &A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    cin >> *itr;
}

template <typename T> static inline void ArrayPut(const vector<T> &A) {
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    cout << *itr << " ";
  cout << endl;
}

template <typename T> static inline T ArraySum(vector<T> &A) {
  T res = 0;
  for (auto itr = A.begin(); itr < A.end(); ++itr)
    res += *itr;
  return res;
}

bool Sec_compare(pair<uint64_t, uint64_t> a, pair<uint64_t, uint64_t> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

u64 dec_dig(u64 num) {
  u64 res = 0;
  while (num > 0) {
    num /= 10;
    ++res;
  }
  return res;
}

u64 gcd(u64 a, u64 b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}

int main() {
  cout << setprecision(18);
  i32 N;
  cin >> N;
  if (N == 2)
    cout << 0 << endl;
  else {
    vector<i32> A(N);
    ArrayInput(A);
    u64 res = 0;
    for (i32 i = 0; i < N - 2; ++i) {
      for (i32 j = i + 1 + A[i]; j < N; ++j) {
        if (abs(i - j) == A[i] + A[j])
          res++;
      }
    }
    cout << res << endl;
  }
  return 0;
}