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

#include <bits/stdc++.h>
using namespace std;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ll long long
#define gcd(a, b) __gcd(a, b)
#define pi 2 * acos(0.0)
#define endl "\\n"
// use emplace_back() instead of push_back()
const ll mod = 1e9 + 7;
template <typename T> void print(T t) {
  for (const auto &e : t)
    cout << e << " ";
  cout << endl;
}
ll power(ll x, ll y, ll p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<int> g[1000001];
int vis[1000001];
int in[100001];
int low[100001];
int timer = 0;
vector<pair<int, int>> v;
int f;
void dfs(int n, int p) {
  vis[n] = 1;
  low[n] = in[n] = timer++;
  if (timer != 1)
    v.push_back({p, n});
  for (auto ch : g[n]) {
    if (ch == p)
      continue;
    if (vis[ch]) {
      low[n] = min(low[n], in[ch]);
      if (in[n] > in[ch])
        v.push_back({n, ch});
    } else {
      dfs(ch, n);
      if (in[n] < low[ch]) {
        f = 1;
        return;
      }
      low[n] = min(low[n], low[ch]);
    }
  }
}
ll josephus(ll n) {
  ll p = 1;
  while (p <= n)
    p *= 2;
  return (2 * n) - p + 1;
}

bool isprime(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
vector<ll> aa;
void printallpf(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) {
        aa.push_back(i);
        if (i != n / i)
          if (isprime(n / i))
            aa.push_back(n / i), n = i;
        n = n / i;
      }
    }
  }
}
void primeFactors(int n) {
  while (n % 2 == 0) {
    cout << 2 << " ";
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      cout << i << " ";
      n = n / i;
    }
  }
  if (n > 2)
    cout << n << " ";
}
int dsu[100001];
void dsuunion(int u, int v) {
  dsu[v] += dsu[u];
  dsu[u] = v;
}
ll find(int a) {
  if (dsu[a] < 0)
    return a;
  else
    return dsu[a] = find(dsu[a]);
}
ll nod(ll x) {
  ll cnt = 0;
  while (x) {
    x = x >> 1;
    cnt++;
  }
  return cnt;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> m;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i] + i]++;
  }
  ll cnt = 0;
  for (ll i = 1; i < n; i++) {
    if (m[i - a[i]])
      cnt += m[i - a[i]];
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ll long long
#define gcd(a, b) __gcd(a, b)
#define pi 2 * acos(0.0)
#define endl "\\n"
// use emplace_back() instead of push_back()
const ll mod = 1e9 + 7;
template <typename T> void print(T t) {
  for (const auto &e : t)
    cout << e << " ";
  cout << endl;
}
ll power(ll x, ll y, ll p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<int> g[1000001];
int vis[1000001];
int in[100001];
int low[100001];
int timer = 0;
vector<pair<int, int>> v;
int f;
void dfs(int n, int p) {
  vis[n] = 1;
  low[n] = in[n] = timer++;
  if (timer != 1)
    v.push_back({p, n});
  for (auto ch : g[n]) {
    if (ch == p)
      continue;
    if (vis[ch]) {
      low[n] = min(low[n], in[ch]);
      if (in[n] > in[ch])
        v.push_back({n, ch});
    } else {
      dfs(ch, n);
      if (in[n] < low[ch]) {
        f = 1;
        return;
      }
      low[n] = min(low[n], low[ch]);
    }
  }
}
ll josephus(ll n) {
  ll p = 1;
  while (p <= n)
    p *= 2;
  return (2 * n) - p + 1;
}

bool isprime(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
vector<ll> aa;
void printallpf(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) {
        aa.push_back(i);
        if (i != n / i)
          if (isprime(n / i))
            aa.push_back(n / i), n = i;
        n = n / i;
      }
    }
  }
}
void primeFactors(int n) {
  while (n % 2 == 0) {
    cout << 2 << " ";
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      cout << i << " ";
      n = n / i;
    }
  }
  if (n > 2)
    cout << n << " ";
}
int dsu[100001];
void dsuunion(int u, int v) {
  dsu[v] += dsu[u];
  dsu[u] = v;
}
ll find(int a) {
  if (dsu[a] < 0)
    return a;
  else
    return dsu[a] = find(dsu[a]);
}
ll nod(ll x) {
  ll cnt = 0;
  while (x) {
    x = x >> 1;
    cnt++;
  }
  return cnt;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] >= n)
      continue;
    for (ll j = i + 1; j < n; j++) {
      if (a[i] + a[j] == abs(i - j))
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}