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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<vector<long long int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(vector<long long int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(set<int> a) {
  for (auto x : a)
    cout << x << " ";
  cout << endl;
}

// nCrを返す関数.
long long int comb(long long int n, long long int r) {
  if (n < r || n < 0)
    return 0;
  long long int x = 1, y = 1;
  if (n - r < r)
    r = n - r;
  for (long long int i = 0; i < r; i++) {
    x = x * (n - i);
    y = y * (i + 1);
  }
  return x / y;
}

int main() {
  int n;
  cin >> n;
  map<ll, ll> l;
  map<ll, ll> r;
  rep(i, n) {
    ll a;
    cin >> a;
    l[a + i + 1]++;
    r[(i + 1) - a]++;
  }
  ll ans = 0;
  for (auto x : l) {
    ans += x.second * r[x.first];
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<vector<long long int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

void print(vector<int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(vector<long long int> a) {
  int n = a.size();
  for (int j = 0; j < n; j++) {
    if (j != n - 1)
      cout << a[j] << " ";
    else
      cout << a[j] << endl;
  }
}

void print(set<int> a) {
  for (auto x : a)
    cout << x << " ";
  cout << endl;
}

// 繰り返し2乗法を用いたべき乗.x^nを返す.
long long int pow_(long long int x, long long int n) {
  long long int res = 1;
  while (n) {
    if (n & 1) {
      res *= x;
    }
    n >>= 1;
    x = x * x;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  set<int> a;
  vector<int> temp(n);
  rep(i, n) {
    int b;
    cin >> b;
    temp[i] = b;
    a.insert(b);
  }
  int ans = 0;
  rep(i, n) {
    rep2(j, max(i, temp[i] + 1), n) {
      if (j - i == temp[j] + temp[i])
        ans++;
    }
  }
  cout << ans << endl;

  return 0;
}