Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define scnaf scanf
#define sacnf scanf
#define sancf scanf

#define ln '\n'
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define P(p) cout << (p) << endl;
#define S(s) cin >> (s);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define tmax(x, y, z) max(x, max(y, z))
#define tmin(x, y, z) min(x, min(y, z))
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

#define reps(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rep(i, n) reps(i, 0, (n))
#define rreps(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define rrep(i, n) reprt(i, n, 0)

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int>> mat;

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;
const double EPS = 1e-9;
const double PI = acos(-1.0);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

void Yes(bool f) {
  if (f) {
    P("Yes");
  } else {
    P("No")
  }
}
void yES(bool f) {
  if (f) {
    P("yES");
  } else {
    P("nO")
  }
}
void Yay(bool f) {
  if (f) {
    P("Yay!");
  } else {
    P(":(")
  }
}

/*********TEMPLATE**********/

ll N;
ll A[22], A_S[22];

void init() {

  S(N);
  rep(i, N) scnaf("%lld", A + i);
  rep(i, N) A_S[i + 1] = A_S[i] + A[i];
}

signed main() {

  init();

  map<ll, ll> mp;
  rep(i, N + 1) {
    ll v = A_S[i];
    if (mp.count(v) == 0) {
      mp[v] = 1;
    } else {
      ++mp[v];
    }
  }

  ll ans = 0;
  for (auto i = mp.begin(); i != mp.end(); ++i) {
    ll q = i->second;
    // P(i->first);P(q);P(-1);
    if (q > 1) {
      ans += q * (q - 1) / 2;
    }
  }
  P(ans);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define scnaf scanf
#define sacnf scanf
#define sancf scanf

#define ln '\n'
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define P(p) cout << (p) << endl;
#define S(s) cin >> (s);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define tmax(x, y, z) max(x, max(y, z))
#define tmin(x, y, z) min(x, min(y, z))
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

#define reps(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rep(i, n) reps(i, 0, (n))
#define rreps(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define rrep(i, n) reprt(i, n, 0)

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int>> mat;

const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;
const double EPS = 1e-9;
const double PI = acos(-1.0);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

void Yes(bool f) {
  if (f) {
    P("Yes");
  } else {
    P("No")
  }
}
void yES(bool f) {
  if (f) {
    P("yES");
  } else {
    P("nO")
  }
}
void Yay(bool f) {
  if (f) {
    P("Yay!");
  } else {
    P(":(")
  }
}

/*********TEMPLATE**********/

ll N;
ll A[234567], A_S[234567];

void init() {

  S(N);
  rep(i, N) scnaf("%lld", A + i);
  rep(i, N) A_S[i + 1] = A_S[i] + A[i];
}

signed main() {

  init();

  map<ll, ll> mp;
  rep(i, N + 1) {
    ll v = A_S[i];
    if (mp.count(v) == 0) {
      mp[v] = 1;
    } else {
      ++mp[v];
    }
  }

  ll ans = 0;
  for (auto i = mp.begin(); i != mp.end(); ++i) {
    ll q = i->second;
    // P(i->first);P(q);P(-1);
    if (q > 1) {
      ans += q * (q - 1) / 2;
    }
  }
  P(ans);
}