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

/*
h1, h2, h3, ....., hn-1, hn

1, 2, 3, ......, n-1, n

hi + hj = j - i; j>i

hi + i = j - hj

arr1 = {h1+1, h2+2, h3+3, ....., hn+n}
arr2 = {1-h1, 2-h2, 3-h3, ....., n-hn}

arrf[][0] = {{i, hi+i}, {j, hj+j}, {}}
arrf[][1] = {{i, i-hi}, {j, j-hj}, {}}

*/

#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> arrf[2][500005];
ll revmp[500005];

int main() {
  int n;
  vector<ll> a;
  map<ll, int> mp;
  cin >> n;
  a.assign(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    ll sum = a[i] + i + 1;
    ll diff = i + 1 - a[i];
    if (mp.count(sum) == 0) {
      revmp[cnt] = sum;
      mp[sum] = cnt++;
    }
    arrf[0][mp[sum]].pb(i + 1);
    if (diff >= 0) {
      if (mp.count(diff) == 0) {
        revmp[cnt] = diff;
        mp[diff] = cnt++;
      }
      arrf[1][mp[diff]].pb(i + 1);
    }
  }
  for (int i = 0; i < cnt; i++) {
    sort(arrf[0][i].begin(), arrf[0][i].end());
    sort(arrf[1][i].begin(), arrf[1][i].end());
  }
  /*for(int i=0;i<cnt;i++){
          cout<<i<<" "<<revmp[i]<<":\\n"<<"==sum==\\n";
          for(int j=0;j<arrf[0][i].size();j++){
                  cout<<arrf[0][i][j]<<" ";
          }
          cout<<"\\n==diff==\\n";
          for(int j=0;j<arrf[1][i].size();j++){
                  cout<<arrf[1][i][j]<<" ";
          }
          cout<<"\\n";
  }*/
  ull ans = 0;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < arrf[0][i].size(); j++) {
      int idx =
          upper_bound(arrf[1][i].begin(), arrf[1][i].end(), arrf[0][i][j]) -
          arrf[1][i].begin();
      ull tmp = arrf[1][i].size() - idx;
      ans += tmp;
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

/*
h1, h2, h3, ....., hn-1, hn

1, 2, 3, ......, n-1, n

hi + hj = j - i; j>i

hi + i = j - hj

arr1 = {h1+1, h2+2, h3+3, ....., hn+n}
arr2 = {1-h1, 2-h2, 3-h3, ....., n-hn}

arrf[][0] = {{i, hi+i}, {j, hj+j}, {}}
arrf[][1] = {{i, i-hi}, {j, j-hj}, {}}

*/

#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;

vector<int> arrf[2][200005];
ll revmp[200005];

int main() {
  int n;
  vector<ll> a;
  map<ll, int> mp;
  cin >> n;
  a.assign(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    ll sum = a[i] + i + 1;
    ll diff = i + 1 - a[i];
    if (mp.count(sum) == 0) {
      revmp[cnt] = sum;
      mp[sum] = cnt++;
    }
    arrf[0][mp[sum]].pb(i + 1);
    if (diff >= 0) {
      if (mp.count(diff) == 0) {
        revmp[cnt] = diff;
        mp[diff] = cnt++;
      }
      arrf[1][mp[diff]].pb(i + 1);
    }
  }
  for (int i = 0; i < cnt; i++) {
    sort(arrf[0][i].begin(), arrf[0][i].end());
    sort(arrf[1][i].begin(), arrf[1][i].end());
  }
  /*for(int i=0;i<cnt;i++){
          cout<<i<<" "<<revmp[i]<<":\\n"<<"==sum==\\n";
          for(int j=0;j<arrf[0][i].size();j++){
                  cout<<arrf[0][i][j]<<" ";
          }
          cout<<"\\n==diff==\\n";
          for(int j=0;j<arrf[1][i].size();j++){
                  cout<<arrf[1][i][j]<<" ";
          }
          cout<<"\\n";
  }*/
  int ans = 0;
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < arrf[0][i].size(); j++) {
      int idx =
          upper_bound(arrf[1][i].begin(), arrf[1][i].end(), arrf[0][i][j]) -
          arrf[1][i].begin();
      int tmp = arrf[1][i].size() - idx;
      ans += tmp;
    }
  }
  cout << ans << "\\n";
  return 0;
}