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


#include <algorithm>
#include <array>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

using ll = long long int;
using lln = long long unsigned;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI = acos(-1.0);
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define Else(a, b, c) a != b &&b != c &&c != a
#define all(v) v.begin(), v.end()
#define Abs(x) (x ^ (x >> 31) - (x >> 31))
const long MOD = pow(10, 9) + 7;
const double pai = 3.14159265358979;

char alfa[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
               'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

inline void finish() {
  cout << -1 << endl;
  exit(0);
}

unsigned GetDigit(double num) { return log10(num) + 1; }

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

class Mod {
public:
  static inline ll Add(ll a, ll b) { return (a + b) % MOD; }

  static inline ll Sub(ll a, ll b) {
    ll ans = (a - b) % MOD;
    return (ans < 0) ? (ans + MOD) : ans;
  }

  static inline ll Mul(ll a, ll b) { return (a * b) % MOD; }

  static inline ll Div(ll a, ll b) { return (a % MOD) * modinv(b, MOD) % MOD; }
};

template <typename T> inline void debug(vector<T> v) {
  cout << "[";
  for (ll i = 0; i < v.size() - 1; ++i) {
    cout << v[i] << ", ";
  }
  cout << v[v.size() - 1] << "]" << endl;
}

void Main();
inline void DebugMain() {
  while (true) {
    Main();
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  bool debug = false;
  if (debug) {
    cout << "\\x1b[41m"
         << "Debug Mode " << endl;
  }
  (debug) ? DebugMain() : Main();
  return 0;
}

int parmitation(ll N) {
  ll ans = 1;
  for (ll i = 1; i <= N; i++) {
    ans *= i;
  }

  return ans;
}

int SemiCommonMultiple(ll A, ll ANum, ll B, ll BNum, ll diff, ll max) {
  ll calc;
  calc = diff + B * BNum - A * ANum;

  if (diff + B * BNum > max) {
    return 0;
  } else {
    if (calc < 0) {
      SemiCommonMultiple(A, ANum, B, ++BNum, diff, max);
    } else if (calc > 0) {
      SemiCommonMultiple(A, ++ANum, B, BNum, diff, max);
    } else {
      return A * (ANum + 0.5);
    }
  }
}

ll CaracalVsMonstor(ll H, ll sum, ll count) {
  if (H == 0) {
    return sum;
  }

  H = H / 2;
  sum = sum + count;

  count = count * 2;

  return CaracalVsMonstor(H, sum, count);
}

class ibis {
public:
  ll A;
  ll B;
  ld spec;

  bool operator<(const ibis &another) const { return spec < another.spec; };
};

ll gcd(ll a, ll b) {
  if ((a = a % b) == 0) {
    return b;
  }

  return gcd(b, a);
}

inline void Main() {
  ll X, Y, N, M, K, H;
  vector<ll> A;
  vector<ll> B;
  vector<ll> C;
  vector<ll> D;
  ll ans, temp = 0;
  ll sum = 0, count = 0;
  double avg = 0.0;
  string S;
  string T;
  string Ans = "";

  cin >> N;

  A.resize(N);
  B.resize(N);

  for (ll i = 0; i < N; i++) {
    cin >> Y;
    A[i] = i - Y;
    B[i] = i + Y;
  }
  sort(all(A));
  sort(all(B));

  C.resize(A[A.size() - 1] + 1);
  D.resize(A[A.size() - 1] + 1);

  for (ll i = 0; i < N; i++) {
    if (A[i] > -1) {
      C[A[i]]++;
    }
    if (B[i] <= A[A.size() - 1]) {
      D[B[i]]++;
    }
  }

  for (ll i = 0; i < C.size(); i++) {
    count += C[i] * D[i];
  }

  cout << count;
}

Error: WA

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

using ll = long long int;
using lln = long long unsigned;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI = acos(-1.0);
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define Else(a, b, c) a != b &&b != c &&c != a
#define all(v) v.begin(), v.end()
#define Abs(x) (x ^ (x >> 31) - (x >> 31))
const long MOD = pow(10, 9) + 7;
const double pai = 3.14159265358979;

char alfa[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
               'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

inline void finish() {
  cout << -1 << endl;
  exit(0);
}

unsigned GetDigit(double num) { return log10(num) + 1; }

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

class Mod {
public:
  static inline ll Add(ll a, ll b) { return (a + b) % MOD; }

  static inline ll Sub(ll a, ll b) {
    ll ans = (a - b) % MOD;
    return (ans < 0) ? (ans + MOD) : ans;
  }

  static inline ll Mul(ll a, ll b) { return (a * b) % MOD; }

  static inline ll Div(ll a, ll b) { return (a % MOD) * modinv(b, MOD) % MOD; }
};

template <typename T> inline void debug(vector<T> v) {
  cout << "[";
  for (ll i = 0; i < v.size() - 1; ++i) {
    cout << v[i] << ", ";
  }
  cout << v[v.size() - 1] << "]" << endl;
}

void Main();
inline void DebugMain() {
  while (true) {
    Main();
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  bool debug = false;
  if (debug) {
    cout << "\\x1b[41m"
         << "Debug Mode " << endl;
  }
  (debug) ? DebugMain() : Main();
  return 0;
}

int parmitation(ll N) {
  ll ans = 1;
  for (ll i = 1; i <= N; i++) {
    ans *= i;
  }

  return ans;
}

int SemiCommonMultiple(ll A, ll ANum, ll B, ll BNum, ll diff, ll max) {
  ll calc;
  calc = diff + B * BNum - A * ANum;

  if (diff + B * BNum > max) {
    return 0;
  } else {
    if (calc < 0) {
      SemiCommonMultiple(A, ANum, B, ++BNum, diff, max);
    } else if (calc > 0) {
      SemiCommonMultiple(A, ++ANum, B, BNum, diff, max);
    } else {
      return A * (ANum + 0.5);
    }
  }
}

ll CaracalVsMonstor(ll H, ll sum, ll count) {
  if (H == 0) {
    return sum;
  }

  H = H / 2;
  sum = sum + count;

  count = count * 2;

  return CaracalVsMonstor(H, sum, count);
}

class ibis {
public:
  ll A;
  ll B;
  ld spec;

  bool operator<(const ibis &another) const { return spec < another.spec; };
};

ll gcd(ll a, ll b) {
  if ((a = a % b) == 0) {
    return b;
  }

  return gcd(b, a);
}

inline void Main() {
  ll X, Y, N, M, K, H;
  vector<ll> A;
  vector<ll> B;
  vector<ll> C;
  vector<ll> D;
  ll ans, temp = 0;
  ll sum = 0, count = 0;
  double avg = 0.0;
  string S;
  string T;
  string Ans = "";

  cin >> N;

  A.resize(N);
  B.resize(N);

  for (ll i = 0; i < N; i++) {
    cin >> Y;
    A[i] = i - Y;
    B[i] = i + Y;
  }
  sort(all(A));
  sort(all(B));

  C.resize(B[B.size() - 1] + 1);
  D.resize(B[B.size() - 1] + 1);

  for (ll i = 0; i < N; i++) {
    if (A[i] > -1) {
      C[A[i]]++;
    }

    D[B[i]]++;
  }

  for (ll i = 0; i < C.size(); i++) {
    count += C[i] * D[i];
  }

  cout << count;
}