alexjercan/bugnet huggingface dataset:
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1500 * 1000;
int cnt[MAX + 1];

void input(vector<int> &vec, int size) {
  for (int i = 1; i <= size; ++i) {
    cin >> vec[i];
    vec[i] += vec[i - 1];
  }
}

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

  for (int n, m; cin >> n >> m, n;) {
    vector<int> vertical(n + 1, 0), horizontal(m + 1, 0);
    input(vertical, n);
    input(horizontal, m);

    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j <= n; ++j)
        ++cnt[vertical[j] - vertical[i]];

    int ans = 0;
    for (int i = 0; i < m; ++i)
      for (int j = i + 1; j <= m; ++j)
        ans += cnt[horizontal[j] - horizontal[i]];

    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1500 * 1000;
int cnt[MAX + 1];

void input(vector<int> &vec, int size) {
  for (int i = 1; i <= size; ++i) {
    cin >> vec[i];
    vec[i] += vec[i - 1];
  }
}

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

  for (int n, m; cin >> n >> m, n;) {
    vector<int> vertical(n), horizontal(m);
    input(vertical, n);
    input(horizontal, m);

    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j <= n; ++j)
        ++cnt[vertical[j] - vertical[i]];

    int ans = 0;
    for (int i = 0; i < m; ++i)
      for (int j = i + 1; j <= m; ++j)
        ans += cnt[(horizontal[j] - horizontal[i])];

    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  //  cin.tie(0);
  //  ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m, n) {
    int nsize = n * (n + 1) / 2;
    int msize = m * (m + 1) / 2;
    vector<int> rrr(nsize);
    vector<int> ccc(msize);
    int tmp;

    int k = n;
    REP(i, n) {
      cin >> tmp;
      rrr[i] = tmp;
      int wa = tmp;
      for (int j = i - 1; j >= 0; j--) {
        wa += rrr[j];
        rrr[k] = wa;
        k++;
      }
    }
    k = m;
    REP(i, m) {
      cin >> tmp;
      ccc[i] = tmp;
      int wa = tmp;
      for (int j = i - 1; j >= 0; j--) {
        wa += ccc[j];
        ccc[k] = wa;
        k++;
      }
    }
    sort(rrr.begin(), rrr.end());
    sort(ccc.begin(), ccc.end());
    rrr.push_back(-1);
    ccc.push_back(-1);
    int ri = 0, ci = 0;
    int ans = 0;
    int cct = 1, rct = 1;

    bool is_r = true, is_c = true;
    while (true) {
      while (is_r && rrr[ri] == rrr[ri + 1]) {
        rct++;
        ri++;
      }
      while (is_c && ccc[ci] == ccc[ci + 1]) {
        cct++;
        ci++;
      }
      if (rrr[ri] == ccc[ci]) {
        ans += rct * cct;
        //	cout << rrr[ri] << " " << ccc[ci] << endl;
        is_c = true;
        is_r = true;
        rct = 1;
        cct = 1;
        ri++;
        ci++;
      } else if (rrr[ri] > ccc[ci]) {
        ci++;
        is_c = true;
        is_r = false;
        cct = 1;
      } else {
        ri++;
        is_r = true;
        is_c = false;
        rct = 1;
      }
      if (ri > nsize - 1 || ci > msize - 1)
        break;
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> hori(1, 0);
    vector<int> vert(1, 0);
    int tmp;
    for (int i = 1; i < n + 1; i++) {
      cin >> tmp;
      REP(j, i) { hori.push_back(tmp - hori[j]); }
    }
    for (int i = 1; i < m + 1; i++) {
      cin >> tmp;
      REP(j, i) { vert.push_back(tmp - vert[j]); }
    }
    int hori_size = hori.size();
    int vert_size = vert.size();
    int ct = 0;
    for (int i = 1; i < hori_size; i++)
      for (int j = 1; j < vert_size; j++) {
        if (hori[i] == vert[j])
          ct++;
      }
    cout << ct << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

signed main() {
  Int(n);
  Int(m);

  while (n && m) {
    vi h(n), w(m);
    map<int, int> s;
    rep(i, n) { cin >> h[i]; }
    rep(i, n) {
      int tmp = h[i];
      if (s.find(tmp) != s.end()) {
        int sec = s.find(tmp)->second + 1;
        s.erase(tmp);
        s.insert(mp(tmp, sec));
      } else {
        s.insert(mp(tmp, 1));
      }
      for (int j = i + 1; j < n; j++) {
        tmp += h[j];
        if (s.find(tmp) != s.end()) {
          int sec = s.find(tmp)->second + 1;
          s.erase(tmp);
          s.insert(mp(tmp, sec));
        } else {
          s.insert(mp(tmp, 1));
        }
      }
    }
    rep(i, m) { cin >> w[i]; }
    int ret = 0;
    rep(i, m) {
      int tmp = w[i];
      if (s.find(tmp) != s.end()) {
        ret += s.find(tmp)->second;
      }
      for (int j = i + 1; j < m; j++) {
        tmp += w[j];
        if (s.find(tmp) != s.end()) {
          ret += s.find(tmp)->second;
        }
      }
    }
    cout << ret << endl;
    cin >> n >> m;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

signed main() {
  Int(n);
  Int(m);

  while (n && m) {
    vi h(n), w(m);
    multiset<int> s;
    rep(i, n) { cin >> h[i]; }
    rep(i, n) {
      int tmp = h[i];
      s.insert(tmp);
      for (int j = i + 1; j < n; j++) {
        tmp += h[j];
        s.insert(tmp);
      }
    }
    rep(i, m) { cin >> w[i]; }
    int ret = 0;
    rep(i, m) {
      int tmp = w[i];
      ret += s.count(tmp);
      for (int j = i + 1; j < n; j++) {
        tmp += w[j];
        ret += s.count(tmp);
      }
    }
    cout << ret << endl;
    cin >> n >> m;
  }

  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (j); i++)
int memo[15000000];
int main() {
  int n, m, w[1501], h[1501], t, s, cnt;
  for (;;) {
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    cnt = s = *w = *h = 0;
    memset(memo, 0, sizeof(memo));
    rep(i, n) {
      scanf("%d", &t);
      s += t;
      w[i + 1] = s;
    }
    s = 0;
    rep(i, m) {
      scanf("%d", &t);
      s += t;
      h[i + 1] = s;
    }

    rep(i, n + 1) rep(j, i) {
      int c = 0;
      int len = w[i] - w[j];
      if (memo[len] > 0) {
        cnt += memo[len];
        continue;
      }
      int l = 0, r = 1;
      while (r <= m) {
        if (h[r] - h[l] == len) {
          c++;
          r++;
          l++;
        } else if (h[r] - h[l] < len)
          r++;
        else
          l++;
      }
      memo[len] = c;
      cnt += c;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

using namespace std;
#define rep(i, j) for (int i = 0; i < (j); i++)
int main() {
  int n, m, w[1501], h[1501], t, s, cnt;
  for (;;) {
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    cnt = s = *w = *h = 0;
    rep(i, n) {
      scanf("%d", &t);
      s += t;
      w[i + 1] = s;
    }
    s = 0;
    rep(i, m) {
      scanf("%d", &t);
      s += t;
      h[i + 1] = s;
    }

    rep(i, n + 1) rep(j, i) {
      int len = w[i] - w[j];
      int l = 0, r = 1;
      while (r <= m) {
        if (h[r] - h[l] == len)
          cnt++;
        if (h[r] - h[l] <= len)
          r++;
        else
          l++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)

void slove(int n, int m) {
  vint tmph(1501);
  vint h(1500001); //?????±?????????????????????
  vint tmpw(1501);
  vint w(1500001);

  //??\\???
  rep(i, n) scanf("%d", &tmph[i]);
  rep(i, m) scanf("%d", &tmpw[i]);

  for (int i = 0; i < n; ++i) {
    int len = 0;
    for (int j = i; j < n; ++j) {
      len += tmph[j];
      h[len]++;
    }
  }

  for (int i = 0; i < m; ++i) {
    int len = 0;
    for (int j = i; j < m; ++j) {
      len += tmpw[j];
      w[len]++;
    }
  }

  ll sum = 0;
  rep(i, 1500001) { sum += h[i] * w[i]; }

  printf("%lld\\n", sum);
  return;
}

int main(void) {
  int n, m;
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0)
      break;
    slove(n, m);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)

void slove(int n, int m) {
  vint tmph(n);
  vint h(n * 1000); //?????±?????????????????????
  vint tmpw(m);
  vint w(m * 1000);

  //??\\???
  rep(i, n) scanf("%d", &tmph[i]);
  rep(i, m) scanf("%d", &tmpw[i]);

  for (int i = 0; i < n; ++i) {
    int len = 0;
    for (int j = i; j < n; ++j) {
      len += tmph[j];
      h[len]++;
    }
  }

  for (int i = 0; i < m; ++i) {
    int len = 0;
    for (int j = i; j < m; ++j) {
      len += tmpw[j];
      w[len]++;
    }
  }

  ll sum = 0;
  int limit = max(n, m);
  rep(i, limit * 1000) { sum += h[i] * w[i]; }

  printf("%lld\\n", sum);
  return;
}

int main(void) {
  int n, m;
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0)
      break;
    slove(n, m);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;
  int tmp;
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    vector<int> harr(N + 1), warr(M + 1);
    int ans = 0;
    harr[0] = 0;
    warr[0] = 0;
    for (int i = 1; i < N + 1; i++) {
      cin >> tmp;
      harr[i] = harr[i - 1] + tmp;
    }
    for (int i = 1; i < M + 1; i++) {
      cin >> tmp;
      warr[i] = warr[i - 1] + tmp;
    }
    vector<int> count(max(harr[N], warr[M]) + 1, 0);
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N + 1; j++) {
        count[harr[j] - harr[i]]++;
      }
    }
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j < M + 1; j++) {
        ans += count[warr[j] - warr[i]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, M;
  int tmp;
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    vector<int> harr(N + 1), warr(M + 1);
    int ans = 0;
    harr[0] = 0;
    warr[0] = 0;
    for (int i = 1; i < N + 1; i++) {
      cin >> tmp;
      harr[i] = harr[i - 1] + tmp;
    }
    for (int i = 1; i < M + 1; i++) {
      cin >> tmp;
      warr[i] = warr[i - 1] + tmp;
    }
    vector<int> count(harr[N] + 1, 0);
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N + 1; j++) {
        count[harr[j] - harr[i]]++;
      }
    }
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j < M + 1; j++) {
        ans += count[warr[j] - warr[i]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int n, m;
int h[2001], w[2001];
int hsum[2001], wsum[2001];
int hcnt[2000001], wcnt[2000001];

void init() {
  memset(hsum, 0, sizeof(hsum));
  memset(wsum, 0, sizeof(wsum));
  memset(h, 0, sizeof(h));
  memset(w, 0, sizeof(w));
  memset(hcnt, 0, sizeof(hcnt));
  memset(wcnt, 0, sizeof(wcnt));
  return;
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n + m == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (int i = 0; i < n; i++)
      hsum[i + 1] = hsum[i] + h[i];
    for (int i = 0; i < m; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      wsum[i + 1] = wsum[i] + w[i];
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        hcnt[hsum[j] - hsum[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m + 1; j++) {
        wcnt[wsum[j] - wsum[i]]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < 2000000; i++)
      cnt += hcnt[i] * wcnt[i];
    printf("%d\\n", cnt);
    init();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int n, m;
int h[2001], w[2001];
int hsum[2001], wsum[2001];

void init() {
  memset(hsum, 0, sizeof(hsum));
  memset(wsum, 0, sizeof(wsum));
  memset(h, 0, sizeof(h));
  memset(w, 0, sizeof(w));
  return;
}

bool check(int a, int s) {
  int l = s - 1, r = m + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (wsum[mid] - wsum[s] > a)
      r = mid;
    else
      l = mid;
  }
  if (a == wsum[l] - wsum[s])
    return true;
  return false;
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n + m == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (int i = 0; i < n; i++)
      hsum[i + 1] = hsum[i] + h[i];
    for (int i = 0; i < m; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      wsum[i + 1] = wsum[i] + w[i];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        int hl = hsum[j] - hsum[i];
        for (int k = 0; k < m; k++) {
          if (check(hl, k))
            cnt++;
        }
      }
    }
    printf("%d\\n", cnt);
    init();
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int hi, wi;
int N, M;
int h[3000];
int w[3000];
long long int hl[1500004] = {};
long long int wl[1500004] = {};
int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    long long int ans = 0;
    cin >> hi;
    h[0] = hi;
    for (int i = 1; i < N; i++) {
      cin >> hi;
      h[i] = h[i - 1] + hi;
    }
    cin >> wi;
    w[0] = wi;
    for (int i = 1; i < M; i++) {
      cin >> wi;
      w[i] = w[i - 1] + wi;
    }
    for (int i = 0; i < 1500000; i++) {
      hl[i] = 0;
      wl[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N - i; j++) {
        hl[h[i + j] - h[j - 1]]++;
      }
    }
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M - i; j++) {
        wl[w[i + j] - w[j - 1]]++;
      }
    }
    for (int i = 0; i <= 1500000; i++) {
      ans += hl[i] * wl[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int hi, wi;
int N, M;
int h[1520];
int w[1000];
int hl[150004] = {};
int wl[150004] = {};
int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    long long int ans = 0;
    cin >> hi;
    h[0] = hi;
    for (int i = 1; i < N; i++) {
      cin >> hi;
      h[i] = h[i - 1] + hi;
    }
    cin >> wi;
    w[0] = wi;
    for (int i = 1; i < M; i++) {
      cin >> wi;
      w[i] = w[i - 1] + wi;
    }
    hl[150004] = {};
    wl[150004] = {};
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N - i; j++) {
        hl[h[i + j] - h[j - 1]]++;
      }
    }
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M - i; j++) {
        wl[w[i + j] - w[j - 1]]++;
      }
    }
    for (int i = 0; i <= 150000; i++) {
      ans += hl[i] * wl[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 1000000007
#define allof(a) (a).begin(), (a).end()
using namespace std;

/*
const double EPS = 1e-9;
const int INF = 1L << 30;
vector<int> dx = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};

bool in_range(int x, int lb, int ub) {return lb <= x and x < ub;}
bool in_range(int x, int y, int W, int H) {return in_range(x,0,W) and
in_range(y,0,H);} void modAdd(int &a, int b, int mod) {a = (a + b) % MOD;}
template <typename T> void maxUpdate(T& a, T b){a = max(a,b);}
*/
int main() {

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }

    vector<int> h(n + 1);
    vector<int> w(m + 1);
    vector<int> h_cnt(1000000);
    vector<int> w_cnt(1000000);

    int sum = 0;
    h[0] = 0;
    for (int i = 1; i <= n; i++) {
      int num;
      cin >> num;
      sum += num;
      h[i] = sum;
    }

    sum = 0;
    w[0] = 0;
    for (int i = 1; i <= m; i++) {
      int num;
      cin >> num;
      sum += num;
      w[i] = sum;
    }

    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        h_cnt[h[j] - h[i]]++;
      }
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        w_cnt[w[j] - w[i]]++;
      }
    }

    int ans = 0;
    for (int i = 1; i < h_cnt.size(); i++) {
      ans += h_cnt[i] * w_cnt[i];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define allof(a) (a).begin(), (a).end()
using namespace std;

/*
const double EPS = 1e-9;
const int INF = 1L << 30;
vector<int> dx = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};

bool in_range(int x, int lb, int ub) {return lb <= x and x < ub;}
bool in_range(int x, int y, int W, int H) {return in_range(x,0,W) and
in_range(y,0,H);} void modAdd(int &a, int b, int mod) {a = (a + b) % MOD;}
template <typename T> void maxUpdate(T& a, T b){a = max(a,b);}
*/
int main() {

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }

    vector<int> h(n + 1);
    vector<int> w(m + 1);
    vector<int> h_cnt(1501);
    vector<int> w_cnt(1501);

    int sum = 0;
    h[0] = 0;
    for (int i = 1; i <= n; i++) {
      int num;
      cin >> num;
      sum += num;
      h[i] = sum;
    }

    sum = 0;
    w[0] = 0;
    for (int i = 1; i <= m; i++) {
      int num;
      cin >> num;
      sum += num;
      w[i] = sum;
    }

    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        h_cnt[h[j] - h[i]]++;
      }
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i; j <= m; j++) {
        w_cnt[w[j] - w[i]]++;
      }
    }

    int ans = 0;
    for (int i = 1; i < 1501; i++) {
      ans += h_cnt[i] * w_cnt[i];
    }
    cout << ans << endl;
  }

  return 0;
}