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

// https://atcoder.jp/contests/abc166/tasks/abc166_e
// 2020-05-03
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int num;
  cin >> num;
  int tmp_num;
  vector<int> plus;
  vector<int> minus;
  for (int i = 0; i < num; i++) {
    cin >> tmp_num;
    plus.push_back(tmp_num + i);
    minus.push_back(-tmp_num + i);
  }
  long long ans = 0;
  sort(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());
  int index_minus = 0;
  int tmp = -1;
  int ren = 0, index_ren = -1;
  bool end_flag = false;
  for (int index_plus = 0; index_plus < plus.size(); index_plus++) {
    if ((index_ren >= 0) && (plus[index_plus] == minus[index_ren])) {
      ans += ren;
      continue;
    } else {
      ren = 0;
    }
    if (plus[index_plus] < minus[index_minus]) {
      continue;
    } else if (plus[index_plus] > minus[index_minus]) {
      while (plus[index_plus] > minus[index_minus]) {
        index_minus++;
        if (index_minus == num) {
          index_minus = num - 1;
          end_flag = true;
          break;
        }
      }
    }
    while (plus[index_plus] == minus[index_minus]) {
      index_ren = index_minus;
      index_minus++;
      ren++;
      if (index_minus == num) {
        index_minus = num - 1;
        if (end_flag) {
          ren--;
        } else {
          end_flag = true;
        }
        break;
      }
    }
    if ((index_ren >= 0) && (plus[index_plus] == minus[index_ren])) {
      ans += ren;
    } else {
      ren = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// https://atcoder.jp/contests/abc166/tasks/abc166_e
// 2020-05-03
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int num;
  cin >> num;
  array<int, 20010> v;
  vector<int> plus;
  vector<int> minus;
  for (int i = 0; i < num; i++) {
    cin >> v[i];
    plus.push_back(v[i] + i);
    minus.push_back(-v[i] + i);
  }
  long long ans = 0;
  sort(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());
  int index_minus = 0;
  int tmp = -1;
  int ren = 0, index_ren = -1;
  bool end_flag = false;
  for (int index_plus = 0; index_plus < plus.size(); index_plus++) {
    if ((index_ren >= 0) && (plus[index_plus] == minus[index_ren])) {
      ans += ren;
      continue;
    } else {
      ren = 0;
    }
    if (plus[index_plus] < minus[index_minus]) {
      continue;
    } else if (plus[index_plus] > minus[index_minus]) {
      while (plus[index_plus] > minus[index_minus]) {
        index_minus++;
        if (index_minus == num) {
          index_minus = num - 1;
          end_flag = true;
          break;
        }
      }
    }
    while (plus[index_plus] == minus[index_minus]) {
      index_ren = index_minus;
      index_minus++;
      ren++;
      if (index_minus == num) {
        index_minus = num - 1;
        if (end_flag) {
          ren--;
        } else {
          end_flag = true;
        }
        break;
      }
    }
    if ((index_ren >= 0) && (plus[index_plus] == minus[index_ren])) {
      ans += ren;
    } else {
      ren = 0;
    }
  }
  cout << ans << endl;
  return 0;
}