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

#include <bits/stdc++.h>

#define _USE_MATH_DEFINES
#define reb(i, b, n) for (int i = (int)(b); i < (int)(n); i++)
#define rep(i, n) reb(i, 0, n)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define reach(it, v)                                                           \\
  for (__typeof((v).rbegin()) it = (v).rbegin(); it != (v).rend(); it++)
#define vsort(v)           sort((x).begin(), (x).end()
#define rvsort(v)          sort((x).rbegin(), (x).rend()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr << __LINE__ << ": " << #x << " = [" << (x) << "]" << endl;
#define dbgArr(table, n)                                                       \\
  rep(i, n) { cout << table[i] << ' '; }                                       \\
  cout << '\\n';
#define dbg2Arr(table, W, H)                                                   \\
  rep(y, H) {                                                                  \\
    rep(x, W) cout << table[y][x] << ' ';                                      \\
    cout << '\\n';                                                              \\
  }

using namespace std;
using std::stringstream;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<string> vs;

static const int INF = 0x3f3f3f3f;
static const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
static const double EPS = 1e-9;
static const ll MOD = 1000000007LL;

static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  ll N;
  cin >> N;

  map<int, int> m;
  ll ans = 0;
  rep(i, N) {
    ll A;
    cin >> A;
    ans += m[i - A];
    m[i + A]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define _USE_MATH_DEFINES
#define reb(i, b, n) for (int i = (int)(b); i < (int)(n); i++)
#define rep(i, n) reb(i, 0, n)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define reach(it, v)                                                           \\
  for (__typeof((v).rbegin()) it = (v).rbegin(); it != (v).rend(); it++)
#define vsort(v)           sort((x).begin(), (x).end()
#define rvsort(v)          sort((x).rbegin(), (x).rend()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr << __LINE__ << ": " << #x << " = [" << (x) << "]" << endl;
#define dbgArr(table, n)                                                       \\
  rep(i, n) { cout << table[i] << ' '; }                                       \\
  cout << '\\n';
#define dbg2Arr(table, W, H)                                                   \\
  rep(y, H) {                                                                  \\
    rep(x, W) cout << table[y][x] << ' ';                                      \\
    cout << '\\n';                                                              \\
  }

using namespace std;
using std::stringstream;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<string> vs;

static const int INF = 0x3f3f3f3f;
static const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
static const double EPS = 1e-9;
static const ll MOD = 1000000007LL;

static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main(int argc, char *argv[]) {
  ll N;
  cin >> N;
  vl A(N);
  rep(i, N) { cin >> A[i]; }

  int ans = 0;
  rep(i, N) {
    reb(j, i + 1, N) {
      if (j - i == A[i] + A[j]) {
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}