alexjercan/bugnet huggingface dataset:
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <unordered_map>
#define S 1500
using namespace std;
int W[S] = {0};
int H[S] = {0};

int main() {
  int N, M, h, w, i, j, k, l, c;
  while (1) {
    scanf("%d %d", &N, &M);
    if (!N && !M) {
      break;
    }
    unordered_map<int, int> ww, hh;
    for (i = 0; i < S; i++) {
      W[i] = 0;
      H[i] = 0;
    }
    for (i = 0; i < N; i++) {
      scanf("%d", &h);
      if (!i) {
        H[i] = h;
      } else {
        H[i] = H[i - 1] + h;
      }
    }
    for (i = 0; i < M; i++) {
      scanf("%d", &w);
      if (!i) {
        W[i] = w;
      } else {
        W[i] = W[i - 1] + w;
      }
    }
    c = 0;
    for (i = 0; i < M; i++) {
      for (j = 0; j <= i; j++) {
        if (i != j) {
          auto itr = ww.find(W[i] - W[j]);
          if (itr != ww.end()) {
            ww[W[i] - W[j]]++;
          } else {
            ww[W[i] - W[j]] = 1;
          }
        } else {
          auto itr = ww.find(W[i]);
          if (itr != ww.end()) {
            ww[W[i]]++;
          } else {
            ww[W[i]] = 1;
          }
        }
      }
    }

    for (k = 0; k < N; k++) {
      for (l = 0; l <= k; l++) {
        if (k != l) {
          auto itr = hh.find(H[k] - H[l]);
          if (itr != hh.end()) {
            hh[H[k] - H[l]]++;
          } else {
            hh[H[k] - H[l]] = 1;
          }
        } else {
          auto itr = hh.find(H[k]);
          if (itr != hh.end()) {
            hh[H[k]]++;
          } else {
            hh[H[k]] = 1;
          }
        }
      }
    }
    for (auto itr = ww.begin(); itr != ww.end(); itr++) {
      auto r = hh.find(itr->first);
      if (r != hh.end()) {
        c += (itr->second * r->second);
      }
    }
    printf("%d\\n", c);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#define S 1500
using namespace std;
int W[S] = {0};
int H[S] = {0};

int main() {
  int N, M, h, w, i, j, k, l, c;
  while (1) {
    scanf("%d %d", &N, &M);
    if (!N && !M) {
      break;
    }
    for (i = 0; i < S; i++) {
      W[i] = 0;
      H[i] = 0;
    }
    for (i = 0; i < N; i++) {
      scanf("%d", &h);
      if (!i) {
        H[i] = h;
      } else {
        H[i] = H[i - 1] + h;
      }
    }
    for (i = 0; i < M; i++) {
      scanf("%d", &w);
      if (!i) {
        W[i] = w;
      } else {
        W[i] = W[i - 1] + w;
      }
    }
    c = 0;
    for (i = 0; i < M; i++) {
      for (j = 0; j <= i; j++) {
        for (k = 0; k < N; k++) {
          for (l = 0; l <= k; l++) {
            if (i != j && k != l && W[i] - W[j] == H[k] - H[l]) {
              c++;
            } else if (i == j && k != l && W[i] == H[k] - H[l]) {
              c++;
            } else if (i != j && k == l && W[i] - W[j] == H[k]) {
              c++;
            } else if (i == j && k == l && W[i] == H[k]) {
              c++;
            }
          }
        }
      }
    }
    printf("%d\\n", c);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
using namespace std;
static int table[1500 * 1000];
int main() {
  int n, m;
  while (cin >> n >> m && n != 0 || m != 0) {
    int x[1502];
    int y[1502];
    memset(table, 0, sizeof table);
    y[0] = 0;
    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      y[i + 1] = y[i] + h;
    }
    x[0] = 0;
    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      x[i + 1] = x[i] + w;
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j <= m; j++) {
        table[x[j] - x[i]]++;
      }
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (table[y[j] - y[i]] > 0) {
          count += table[y[j] - y[i]];
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m && n != 0 || m != 0) {
    vector<int> x(m + 1);
    vector<int> y(n + 1);
    y[0] = 0;
    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      y[i + 1] = y[i] + h;
    }
    x[0] = 0;
    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      x[i + 1] = x[i] + w;
    }
    vector<pair<int, int>> xp;
    vector<pair<int, int>> yp;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j <= m; j++) {
        xp.push_back(make_pair(i, j));
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        yp.push_back(make_pair(i, j));
      }
    }
    int count = 0;
    for (int i = 0; i < yp.size(); i++) {
      for (int j = 0; j < xp.size(); j++) {
        int dx = x[xp[j].second] - x[xp[j].first];
        int dy = y[yp[i].second] - y[yp[i].first];
        if (dx == dy) {
          count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (int i = a; i < x; i++)

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && M && N) {
    int h[10000] = {};
    int w[10000] = {};
    unsigned long long sum_h[10000] = {};
    unsigned long long sum_w[10000] = {};
    long long h_c[510000] = {};
    long long w_c[500000] = {};

    rep(i, N) {
      cin >> h[i];
      sum_h[i + 1] = sum_h[i] + h[i];
    }
    rep(i, M) {
      cin >> w[i];
      sum_w[i + 1] = sum_w[i] + w[i];
    }

    rep(i, N) rep(j, N - i) h_c[sum_h[j + i + 1] - sum_h[j]]++;
    rep(i, M) rep(j, M - i) w_c[sum_w[j + i + 1] - sum_w[j]]++;

    long long count = 0;
    rep(i, min(sum_h[N], sum_w[M]) + 1) count += h_c[i] * w_c[i];
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, x) for (int i = 0; i < x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rep_to(a, i, x) for (int i = a; i < x; i++)

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && M && N) {
    int h[10000] = {};
    int w[10000] = {};
    unsigned long long sum_h[10000] = {};
    unsigned long long sum_w[10000] = {};
    long long h_c[225000] = {};
    long long w_c[225000] = {};

    rep(i, N) {
      cin >> h[i];
      sum_h[i + 1] = sum_h[i] + h[i];
    }
    rep(i, M) {
      cin >> w[i];
      sum_w[i + 1] = sum_w[i] + w[i];
    }

    rep(i, N) rep(j, N - i) h_c[sum_h[j + i + 1] - sum_h[j]]++;
    rep(i, M) rep(j, M - i) w_c[sum_w[j + i + 1] - sum_w[j]]++;

    long long count = 0;
    rep(i, min(sum_h[N], sum_w[M]) + 1) count += h_c[i] * w_c[i];
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int h[1024 * 1500];
int w[1024 * 1500];

int getInt() {
  int c = getchar();
  int ret = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    int hh[n];
    int ww[m];
    int mm = 0;

    for (int i = 0; i < n; i++)
      hh[i] = getInt();
    for (int i = 0; i < m; i++)
      ww[i] = getInt();

    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += hh[j];
        h[sum]++;
      }
      mm = max(mm, sum);
    }
    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += ww[j];
        if (sum > mm)
          break;
        w[sum]++;
      }
    }
    long long ans = 0;
    for (int i = 0; i <= mm; i++) {
      if (w[i] | h[i]) {
        ans += (long long)w[i] * h[i];
        w[i] = h[i] = 0;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int h[1024 * 1500];
int w[1024 * 1500];

int getInt() {
  int c = getchar();
  int ret = 0;
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    int hh[n];
    int ww[m];
    int mm = 0;

    for (int i = 0; i < n; i++)
      hh[i] = getInt();
    for (int i = 0; i < m; i++)
      ww[i] = getInt();

    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += hh[j];
        h[sum]++;
      }
      mm = max(mm, sum);
    }
    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += ww[j];
        if (sum > mm)
          break;
        w[sum]++;
      }
    }
    long long ans = 0;
    for (int i = 0; i <= mm; i++) {
      if (w[i] | h[i]) {
        ans += (long long)w[i] * h[i];
        w[i] = h[i] = 0;
      }
    }

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


#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int min(int p, int q) {
  if (p > q)
    return q;
  return p;
}

int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);

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

    vector<int> x, y;
    int sum = 0;
    int xmax, ymax;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      y.push_back(sum);
    }
    ymax = sum;
    sum = 0;
    for (int i = 0; i < m; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      x.push_back(sum);
    }
    xmax = sum;

    vector<int> dx, dy;
    int width = x.size();
    int height = y.size();

    for (int i = 0; i <= xmax; i++)
      dx.push_back(0);
    for (int i = 0; i <= ymax; i++)
      dy.push_back(0);

    for (int i = 0; i < width; i++)
      for (int j = i + 1; j < width; j++)
        dx.at(x.at(j) - x.at(i))++;

    for (int i = 0; i < height; i++)
      for (int j = i + 1; j < height; j++)
        dy.at(y.at(j) - y.at(i))++;

    int ans = 0;
    int size = min(dx.size(), dy.size());

    for (int i = 0; i < size; i++)
      ans += dx.at(i) * dy.at(i);

    cout << ans << endl;
  }

  // while(1){}
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);

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

    vector<int> x, y;
    int sum = 0;
    x.push_back(0);
    y.push_back(0);

    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      y.push_back(sum);
    }
    sum = 0;
    for (int i = 0; i < m; i++) {
      int tmp;
      cin >> tmp;
      sum += tmp;
      x.push_back(sum);
    }

    int height = y.size();
    int width = x.size();

    int ans = 0;
    for (int i = 0; i < width; i++)
      for (int j = 0; j < height; j++)
        for (int k = i + 1; k < width; k++)
          for (int l = j + 1; l < height; l++)
            if (x.at(i) - x.at(k) == y.at(j) - y.at(l))
              ans++;

    cout << ans << endl;
  }

  // while(1){}
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main(int argc, char const *argv[]) {
  while (1) {
    int n, m;
    cin >> n >> m;
    vector<int> hmap, wmap;
    vector<int> map;
    if (!n || !m)
      return 0;

    int o = 0;
    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      hmap.push_back(o);
      o += h;
    }
    hmap.push_back(o);
    o = 0;
    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      wmap.push_back(o);
      o += w;
    }
    wmap.push_back(o);

    int cnt = 0;
    map.resize(max(hmap[n], wmap[m]) + 10);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        map[hmap[j] - hmap[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m + 1; j++) {
        int it = wmap[j] - wmap[i];
        if (map[it] > 0)
          cnt += map[it];
      }
    }

    cout << cnt << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;

int main(int argc, char const *argv[]) {
  while (1) {
    int n, m;
    cin >> n >> m;
    vector<int> hmap, wmap;
    vector<int> map;
    if (!n || !m)
      return 0;

    int o = 0;
    for (int i = 0; i < n; i++) {
      int h;
      cin >> h;
      hmap.push_back(o);
      o += h;
    }
    hmap.push_back(o);
    o = 0;
    for (int i = 0; i < m; i++) {
      int w;
      cin >> w;
      wmap.push_back(o);
      o += w;
    }
    wmap.push_back(o);

    int cnt = 0;
    map.resize(max(hmap[n], wmap[m]));
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        map[hmap[j] - hmap[i]] += 1;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m + 1; j++) {
        int it = wmap[j] - wmap[i];
        if (map[it] > 0)
          cnt += map[it];
      }
    }

    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAX = 1500000;
int main() {

  for (int n, m; cin >> n >> m, n && m;) {
    int h[1500], w[1500], a0[MAX + 10] = {}, a1[MAX + 10] = {};
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < n - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += h[j + k];
        a0[r]++;
      }
    for (int i = 1; i <= m; i++)
      for (int j = 0; j < m - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += w[j + k];
        a1[r]++;
      }
    int r = 0;
    for (int i = 1; i <= MAX; i++)
      r += a0[i] * a1[i];
    cout << r << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
int main() {

  for (int n, m; cin >> n >> m, n && m;) {
    int h[1500], w[1500], a0[MAX + 10] = {}, a1[MAX + 10] = {};
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < n - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += h[j + k];
        a0[r]++;
      }
    for (int i = 1; i <= m; i++)
      for (int j = 0; j < m - i + 1; j++) {
        int r = 0;
        for (int k = 0; k < i; k++)
          r += w[j + k];
        a1[r]++;
      }
    int r = 0;
    for (int i = 1; i <= MAX; i++)
      r += a0[i] * a1[i];
    cout << r << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 1500 * 1000 + 5;
int size_w[MAX_N], size_h[MAX_N];

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

  int n, m;

  while (cin >> n >> m, n != 0 && m != 0) {
    vector<int> h(n + 1), w(m + 1);

    memset(size_h, 0, sizeof(size_h));
    memset(size_w, 0, sizeof(size_w));

    h[0] = w[0] = 0;
    for (int i = 0; i < n; ++i)
      cin >> h[i + 1];
    for (int i = 0; i < m; ++i)
      cin >> w[i + 1];

    ++n, ++m;
    for (int i = 1; i < n; ++i)
      h[i] += h[i - 1];
    for (int i = 1; i < m; ++i)
      w[i] += w[i - 1];

    for (int l = 0; l < n - 1; ++l)
      for (int r = l + 1; r < n; ++r)
        ++size_h[h[r] - h[l]];
    for (int l = 0; l < m - 1; ++l)
      for (int r = l + 1; r < m; ++r)
        ++size_w[w[r] - w[l]];

    int lim = max(h[n - 1], w[m - 1]);
    int ans = 0;
    for (int d = 1; d <= lim; ++d)
      ans += size_h[d] * size_w[d];

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int Count(int square, const vector<int> &data, int size) {
  int res = 0, l = 0, r = 1;

  while (r < size && l < r) {
    int area = data[r] - data[l];
    if (square == area) {
      ++res;
      ++r;
      ++l;
    } else if (square < area) {
      ++l;
      if (l == r)
        ++r;
    } else {
      ++r;
    }
  }

  return res;
}

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

  int n, m;

  while (cin >> n >> m, n != 0 && m != 0) {
    vector<int> h(n + 1), w(m + 1);

    h[0] = w[0] = 0;
    for (int i = 0; i < n; ++i)
      cin >> h[i + 1];
    for (int i = 0; i < m; ++i)
      cin >> w[i + 1];

    ++n, ++m;
    for (int i = 1; i < n; ++i)
      h[i] += h[i - 1];
    for (int i = 1; i < m; ++i)
      w[i] += w[i - 1];

    int lim = max(h[n - 1], w[m - 1]);
    int ans = 0;
    for (int d = 1; d <= lim; ++d) {
      int cnt_h = Count(d, h, n);
      int cnt_w = Count(d, w, m);

      ans += cnt_h * cnt_w;
    }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<int> h(N);
    vector<int> w(M);
    REP(i, N) cin >> h[i];
    REP(i, M) cin >> w[i];
    vector<int> sh(N + 1);
    vector<int> sw(M + 1);
    REP(i, N) sh[i + 1] = sh[i] + h[i];
    REP(i, M) sw[i + 1] = sw[i] + w[i];
    map<int, int> count;
    REP(i, N) FOR(j, i + 1, N + 1) { count[sh[j] - sh[i]]++; }
    int ans = 0;
    REP(i, M) FOR(j, i + 1, M + 1) { ans += count[sw[j] - sw[i]]; }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())

int main(void) {
  int N, M;
  while (cin >> N >> M, N) {
    vector<int> h(N);
    vector<int> w(M);
    vector<int> sumh(N + 1);
    vector<int> sumw(M + 1);
    REP(i, N) cin >> h[i];
    REP(i, M) cin >> w[i];
    REP(i, N) sumh[i + 1] = h[i] + sumh[i];
    REP(i, M) sumw[i + 1] = w[i] + sumw[i];
    int ans = 0;
    FOR(y1, 0, N + 1)
    FOR(y2, y1 + 1, N + 1) FOR(x1, 0, M + 1) FOR(x2, x1 + 1, M + 1) {
      if (sumh[y2] - sumh[y1] == sumw[x2] - sumw[x1]) {
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N, M, sum, n = 0;
  int w[1500], h[1500], W[1000000] = {}, H[1000000] = {};

  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> h[i];
    for (int i = 0; i < M; i++)
      cin >> w[i];
    sum = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        n += h[j];
        H[n]++;
      }
      n = 0;
    }
    for (int i = 0; i < M; i++) {
      for (int j = i; j < M; j++) {
        n += w[j];
        W[n]++;
      }
      n = 0;
    }
    for (int i = 0; i < 1000000; i++) {
      sum += H[i] * W[i];
      H[i] = 0;
      W[i] = 0;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

//?????????????????§??????????????????????????£????????????
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define PB push_back
#define in scanf
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#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 IREP(i, n) for (int i = 0; i <= (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
int N, M;
int h[1501], w[1501], tw[1502] = {}, th[1502] = {};
// int hhh[15000001]={};
// int www[15000001]={};
void solve() {
  while (cin >> N >> M, N != 0) {
    REP(i, N) cin >> h[i];
    REP(i, M) cin >> w[i];
    tw[0] = th[0] = 0;
    REP(i, N) { th[i + 1] = th[i] + h[i]; }
    REP(i, M) tw[i + 1] = tw[i] + w[i];
    int hhh[th[N] + 1];
    int www[tw[N] + 1];
    memset(www, 0, sizeof(www));
    memset(hhh, 0, sizeof(hhh)); // vector <int>hh,ww;
    int temp;
    for (int s = 0; s <= N; s++) {
      for (int g = s + 1; g <= N; g++) {
        temp = th[g] - th[s];
        // hh.PB(temp);
        hhh[temp]++;
      }
    }
    for (int s = 0; s <= M; s++) {
      for (int g = s + 1; g <= M; g++) {

        temp = tw[g] - tw[s];
        www[temp]++;
        // cout << temp <<endl;
        // ww.PB(temp);
      }
    }
    int m = min(th[N], tw[N]);

    int res = 0;
    REP(i, m + 1) res += www[i] * hhh[i];
    cout << res << endl;
  }
}

int main() { solve(); }