Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// type alias
typedef long long LL;
typedef vector<int> VI;
typedef unordered_map<int, int> MAPII;
typedef unordered_set<int> SETI;
typedef pair<int, int> II;
typedef tuple<int, int, int> III;
// repetition
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) for (int i = 0; i <= (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FORR(x, arr) for (auto &x : arr)
#define SZ(a) int((a).size())
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// minmax
#define SMAX(a, b) a = max(a, b)
#define SMIN(a, b) a = min(a, b)
// debug cerr
#define TRACE true
#define dump(x)                                                                \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << endl;                                        \
  }
#define dump2(x, y)                                                            \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;          \
  }
#define dump3(x, y, z)                                                         \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z     \
         << " = " << (z) << endl;                                              \
  }
#define dump4(x, y, z, a)                                                      \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z     \
         << " = " << (z) << ", " << #a << " = " << (a) << endl;                \
  }
#define dumpAR(ar)                                                             \
  if (TRACE) {                                                                 \
    FORR(x, (ar)) { cerr << x << ','; }                                        \
    cerr << endl;                                                              \
  }

// $ g++ -std=c++11 -Wall -O2 -D_GLIBCXX_DEBUG x.cpp && ./a.out
// 21:20-
const int MAX_N = 1e5 + 1;
int N;
LL A[MAX_N];
LL solve() {
  unordered_map<LL, LL> M;
  LL res = 0, sum = 0;
  M[0] = 1;
  REP(i, N) {
    sum += A[i];
    if (M.count(sum))
      res += M[sum];
    M[sum]++;
    //    dump2(sum,M[sum]);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  REP(i, N) cin >> A[i];
  cout << solve() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// type alias
typedef long long LL;
typedef vector<int> VI;
typedef unordered_map<int, int> MAPII;
typedef unordered_set<int> SETI;
typedef pair<int, int> II;
typedef tuple<int, int, int> III;
// repetition
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) for (int i = 0; i <= (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FORR(x, arr) for (auto &x : arr)
#define SZ(a) int((a).size())
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// minmax
#define SMAX(a, b) a = max(a, b)
#define SMIN(a, b) a = min(a, b)
// debug cerr
#define TRACE true
#define dump(x)                                                                \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << endl;                                        \
  }
#define dump2(x, y)                                                            \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;          \
  }
#define dump3(x, y, z)                                                         \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z     \
         << " = " << (z) << endl;                                              \
  }
#define dump4(x, y, z, a)                                                      \
  if (TRACE) {                                                                 \
    cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z     \
         << " = " << (z) << ", " << #a << " = " << (a) << endl;                \
  }
#define dumpAR(ar)                                                             \
  if (TRACE) {                                                                 \
    FORR(x, (ar)) { cerr << x << ','; }                                        \
    cerr << endl;                                                              \
  }

// $ g++ -std=c++11 -Wall -O2 -D_GLIBCXX_DEBUG x.cpp && ./a.out
// 21:20-
const int MAX_N = 2e5 + 1;
int N;
LL A[MAX_N];
LL solve() {
  unordered_map<LL, LL> M;
  LL res = 0, sum = 0;
  M[0] = 1;
  REP(i, N) {
    sum += A[i];
    if (M.count(sum))
      res += M[sum];
    M[sum]++;
    //    dump2(sum,M[sum]);
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  REP(i, N) cin >> A[i];
  cout << solve() << endl;
  return 0;
}