Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int fc[100001], in[100001];

int pwmd(int a, int n = mod - 2) {
  if (!n)
    return 1;
  int pt = pwmd(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}
int nCr(int n, int r) {
  fc[0] = in[0] = 1;

  for (int i = 1; i <= 100000; ++i) {
    fc[i] = (i * fc[i - 1]) % mod;
    in[i] = pwmd(fc[i]);
  }

  int ans = fc[n];
  ans = (ans * in[r]) % mod;
  ans = (ans * in[n - r]) % mod;

  return ans;
}
int pow(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1)
      ans *= a;
    a *= a;
    b = b >> 1;
  }
  return ans;
}
// vector<pair<int,int>> vec;
bool compare(pair<int, int> v1, pair<int, int> v2) { return v1.ss < v2.ss; }

vector<int> adj[200005];
int dis[200005];
void dfs1(int par, int u) {
  for (int x : adj[u]) {
    if (x == par)
      continue;
    dis[x] = dis[u] + 1;
    dfs1(u, x);
  }
}

void dfs(int par, int u, bool *vis) {
  vis[u] = true;
  for (int x : adj[u]) {
    if (x == par)
      continue;
    dfs(u, x, vis);
    vis[x] = true;
  }
}

vector<int> vec[100001];
int32_t main() {
  FIO;
  int n;
  cin >> n;
  int a[n];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  int c[1000005];
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++) {
    if (a[i] > n)
      continue;
    int t = (i + 1) + a[i];
    c[t]++;
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = (i + 1) - a[i];
    if (x >= 0)
      ans += c[x];
    if (x == ((i + 1) + a[i]))
      ans--;
  }

  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int fc[100001], in[100001];

int pwmd(int a, int n = mod - 2) {
  if (!n)
    return 1;
  int pt = pwmd(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}
int nCr(int n, int r) {
  fc[0] = in[0] = 1;

  for (int i = 1; i <= 100000; ++i) {
    fc[i] = (i * fc[i - 1]) % mod;
    in[i] = pwmd(fc[i]);
  }

  int ans = fc[n];
  ans = (ans * in[r]) % mod;
  ans = (ans * in[n - r]) % mod;

  return ans;
}
int pow(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1)
      ans *= a;
    a *= a;
    b = b >> 1;
  }
  return ans;
}
// vector<pair<int,int>> vec;
bool compare(pair<int, int> v1, pair<int, int> v2) { return v1.ss < v2.ss; }

vector<int> adj[200005];
int dis[200005];
void dfs1(int par, int u) {
  for (int x : adj[u]) {
    if (x == par)
      continue;
    dis[x] = dis[u] + 1;
    dfs1(u, x);
  }
}

void dfs(int par, int u, bool *vis) {
  vis[u] = true;
  for (int x : adj[u]) {
    if (x == par)
      continue;
    dfs(u, x, vis);
    vis[x] = true;
  }
}

vector<int> vec[100001];
int32_t main() {
  FIO;
  int n;
  cin >> n;
  int a[n];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  int c[1000005];
  for (int i = 0; i < n; i++) {
    int t = (i + 1) + a[i];
    c[t]++;
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = (i + 1) - a[i];
    if (x >= 0)
      ans += c[x];
    if (x == ((i + 1) + a[i]))
      ans--;
  }

  cout << ans;
}