Problem p03363 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read() freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
#define sf(n) scanf("%d", &n)
#define sfd(n) scanf("%lf", &n)
#define sl(x) scanf("%I64d", &x)
#define sfl(n) scanf("%lld", &n)
#define sfc(n) scanf(" %c", &n)
#define sful(n) scanf("%ull", &n)
#define ull unsigned long long int
#define endll pf("\n")
#define pf printf
#define pi acos(-1.0)
#define eps 1e-10
#define mem(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define xx first
#define yy second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair
#define ll long long
#define uniq(a) a.erase(unique(a.begin(), a.end()), a.end())
#define cnd tree[idx]
#define lnd tree[idx << 1]
#define rnd tree[(idx << 1) + 1]
#define lndp (idx << 1), (b), ((b + e) >> 1)
#define rndp ((idx << 1) + 1), (((b + e) >> 1) + 1), (e)
#define inf 1999999999
#define MX 100005
#define mod 100000000007
#define var int i, j, k, n, m, z = 0, c = 0, t, x, y, l, r, mid
// int dx[8]={0,0,1,1,-1,-1,1,-1};  //8 direction
// int dy[8]={1,-1,1,-1,1,-1,0,0};
// int dx[4]={1,-1,0,0};  //4 direction
// int dy[4]={0,0,1,-1};

ll int ans;
map<ll int, vector<ll int>> mp;
void findSubArrays(ll int arr[], ll int n) {

  ll int sum = 0;

  for (ll int i = 0; i < n; i++) {
    sum += arr[i];
    if (sum == 0)
      ans++;

    if (mp.find(sum) != mp.end()) {
      // map[sum] stores starting index of all subarrays
      vector<ll int> vc = mp[sum];
      ans += vc.size();
    }
    mp[sum].push_back(i);
  }
}

ll int a[200005];
int main() {
  ll var;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  findSubArrays(a, n);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read() freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
#define sf(n) scanf("%d", &n)
#define sfd(n) scanf("%lf", &n)
#define sl(x) scanf("%I64d", &x)
#define sfl(n) scanf("%lld", &n)
#define sfc(n) scanf(" %c", &n)
#define sful(n) scanf("%ull", &n)
#define ull unsigned long long int
#define endll pf("\n")
#define pf printf
#define pi acos(-1.0)
#define eps 1e-10
#define mem(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define xx first
#define yy second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair
#define ll long long
#define uniq(a) a.erase(unique(a.begin(), a.end()), a.end())
#define cnd tree[idx]
#define lnd tree[idx << 1]
#define rnd tree[(idx << 1) + 1]
#define lndp (idx << 1), (b), ((b + e) >> 1)
#define rndp ((idx << 1) + 1), (((b + e) >> 1) + 1), (e)
#define inf 1999999999
#define MX 100005
#define mod 100000000007
#define var int i, j, k, n, m, z = 0, c = 0, t, x, y, l, r, mid
// int dx[8]={0,0,1,1,-1,-1,1,-1};  //8 direction
// int dy[8]={1,-1,1,-1,1,-1,0,0};
// int dx[4]={1,-1,0,0};  //4 direction
// int dy[4]={0,0,1,-1};

ll int ans;
map<ll int, vector<ll int>> mp;
void findSubArrays(ll int arr[], ll int n) {

  ll int sum = 0;

  for (ll int i = 0; i < n; i++) {
    sum += arr[i];
    if (sum == 0)
      ans++;

    if (mp.find(sum) != mp.end()) {
      ans += mp[sum].size();
    }
    mp[sum].push_back(i);
  }
}

ll int a[200005];
int main() {
  ll var;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  findSubArrays(a, n);

  cout << ans << endl;

  return 0;
}