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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  LL n;
  cin >> n;
  VLL a(n);
  rep(i, n) { cin >> a[i]; }
  VLL wa(n), sa(n);
  map<int, int> m;
  LL ans = 0;
  rep(i, n) {
    wa[i] = i + a[i];
    sa[i] = i - a[i];
    ans += m[sa[i]];
    m[wa[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<long long>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<vector<double>> VVD;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) (a).begin(), (a).end()

const int MOD = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void debug(VI &arr) {
  int n = arr.size();
  rep(i, n) { cout << arr.at(i) << endl; }
}

void debug(VVI &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VVLL &board) {
  int h = board.size();
  int w = board.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << board.at(i).at(j) << " "; }
    cout << endl;
  }
}

void debug(VS &board) {
  int h = board.size();
  rep(i, h) { cout << board.at(i) << endl; }
  return;
}

LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }

LL lcm(LL a, LL b) { return a * b / gcd(a, b); }

int main() {
  // cout<<fixed;
  // cout<<setprecision(9)<<...
  LL n;
  cin >> n;
  VLL a(n, 0);
  rep(i, n) { cin >> a[i]; }
  LL ans = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] + a[j] == j - i) {
        ans++;
        // cout<<i<<" "<<j<<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}