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

////////////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

namespace my {
/*汎用*/
template <typename T> void input_vec(vector<T> &a, ll n) {
  assert(a.empty());
  for (ll i = 0; i < n; i++) {
    T tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
}
template <typename T> void input_set(set<T> &a, ll n) {
  assert(a.empty());
  for (ll i = 0; i < n; i++) {
    T tmp;
    cin >> tmp;
    a.insert(tmp);
  }
}

char itoc(int i) { return i + '0'; }
int ctoi(char c) { return c - '0'; }

/*整数*/
ll gcd(ll a, ll b) { // O( log(max(a,b)) )
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
ll lcm(ll a, ll b) { return a * (b / my::gcd(a, b)); } // O( log(max(a,b)) )
ll modinv(ll n, ll mod) {                              // O( log(max(n,mod)) )
  assert(mod > 0 && my::gcd(n, mod) == 1);
  n %= mod;
  if (n < 0)
    n += mod;
  ll a = mod, b = n, x = 1, y = 0;
  while (b != 0) {
    ll q = a / b, r = a % b;
    a = b;
    b = r;
    y -= q * x;
    swap(x, y);
  }
  y %= mod;
  if (y < 0)
    y += mod;
  return y;
}

bool is_prime(ll n) { // O( sqrt(n) )
  bool flag = true;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
vector<pair<ll, ll>> prime_factorize(ll n) { // O( sqrt(n) )
  vector<pair<ll, ll>> ret;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0)
      continue;
    ll exp = 0;
    while (n % i == 0) {
      n /= i;
      exp++;
    }
    ret.push_back(make_pair(i, exp));
  }
  if (n != 1)
    ret.push_back(make_pair(n, 1));
  return ret;
}

/*union find*/
class unionfind {
private:
  /*根のparの値は「-(その木の要素数)」とする*/
  ll n_;
  vector<ll> par;

  ll root(ll a) { // O( log(n) )
    if (par.at(a) < 0)
      return a;
    else {
      par.at(a) = root(par.at(a)); // 経路圧縮
      return par.at(a);
    }
  }

public:
  unionfind(ll n) : n_(n), par(n, -1) {} // O(n)

  void unite(ll a, ll b) { // O( log(n) )
    assert(0 <= a && a < n_);
    assert(0 <= b && b < n_);
    if (root(a) == root(b))
      return;
    else {
      ll ra = root(a), rb = root(b);
      if (-par.at(ra) < -par.at(rb))
        swap(ra, rb); // サイズによる併合
      par.at(ra) += par.at(rb);
      par.at(rb) = ra;
    }
  }

  bool is_same(ll a, ll b) { // O( log(n) )
    assert(0 <= a && a < n_);
    assert(0 <= b && b < n_);
    return root(a) == root(b);
  }

  ll size(ll a) { // O( log(n) )
    assert(0 <= a && a < n_);
    return -par.at(root(a));
  }
};
} // namespace my
////////////////////////////////////////////////

int main() {
  int n;
  cin >> n;
  vector<ll> a;
  my::input_vec(a, n);
  map<ll, int> cntx;
  for (int i = 0; i < n; i++) {
    ll x = i + a.at(i);
    if (!cntx.count(x))
      cntx[x] = 1;
    else
      cntx.at(x)++;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll y = i - a.at(i);
    if (cntx.count(y))
      ans += cntx.at(y);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

/*                    */
#include <bits/stdc++.h>
using namespace std;
using lint = int64_t;
namespace my {
char itoc(int i) { return i + '0'; }
int ctoi(char c) { return c - '0'; }
lint gcd(lint a, lint b) {
  a = abs(a);
  b = abs(b);
  if (a < b)
    swap(a, b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
lint lcm(lint a, lint b) { return a * b / my::gcd(a, b); }
} // namespace my
/*                    */

int main() {
  int n;
  vector<lint> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    lint tmp;
    cin >> tmp;
    a.push_back(tmp);
  }

  lint ans = 0;
  for (int sa = 1; sa <= n - 1; sa++) {
    for (int i = 0; i + sa < n; i++) {
      if (sa == a.at(i) + a.at(i + sa))
        ans++;
    }
  }
  cout << ans << endl;
}