Problem p02691 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 1e5 + 10;
ll a[maxn];
int main() {
  map<ll, ll> cnt;
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    cnt[a[i] + i]++;
  }
  ll res = 0;
  for (int i = n; i >= 1; --i) {
    --cnt[a[i] + i];
    res += cnt[i - a[i]];
  }
  printf("%lld\n", res);
}

Fixed submission:
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 2e5 + 10;
ll a[maxn];
int main() {
  map<ll, ll> cnt;
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    cnt[a[i] + i]++;
  }
  ll res = 0;
  for (int i = n; i >= 1; --i) {
    --cnt[a[i] + i];
    res += cnt[i - a[i]];
  }
  printf("%lld\n", res);
}