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

#include <iostream>

using namespace std;

int max(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}

int main() {
  int n, m;

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

    int *h, *w;

    int *hNum, *wNum;

    h = new int[n];
    w = new int[m];

    int maxHLength = 0;
    int maxWLength = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      maxHLength += h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
      maxWLength += w[i];
    }

    hNum = new int[maxHLength + 1];
    wNum = new int[maxWLength + 1];

    for (int i = 0; i <= maxHLength; i++) {
      hNum[i] = 0;
    }

    for (int i = 0; i <= maxWLength; i++) {
      wNum[i] = 0;
    }

    int maxLength = 0;

    for (int i = 0; i < n; i++) {
      int length = 0;
      for (int j = i; j < n; j++) {
        length += h[j];
        hNum[length]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int length = 0;
      for (int j = i; j < m; j++) {
        length += w[j];
        wNum[length]++;
      }
    }

    int sum = 0;

    maxLength = max(maxHLength, maxWLength);

    for (int i = 0; i <= maxLength; i++) {
      sum += hNum[i] * wNum[i];
    }

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int main() {
  int n, m;

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

    int *h, *w;

    int *hNum, *wNum;

    h = new int[n];
    w = new int[m];

    int maxHLength = 0;
    int maxWLength = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      maxHLength += h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
      maxWLength += w[i];
    }

    hNum = new int[maxHLength + 1];
    wNum = new int[maxWLength + 1];

    for (int i = 0; i <= maxHLength; i++) {
      hNum[i] = 0;
    }

    for (int i = 0; i <= maxWLength; i++) {
      wNum[i] = 0;
    }

    int maxLength = 0;

    for (int i = 0; i < n; i++) {
      int length = 0;
      for (int j = i; j < n; j++) {
        length += h[j];
        hNum[length]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int length = 0;
      for (int j = i; j < m; j++) {
        length += w[j];
        wNum[length]++;
      }
    }

    int sum = 0;

    maxLength = max(maxHLength, maxWLength);

    for (int i = 0; i <= maxLength; i++) {
      sum += hNum[i] * wNum[i];
    }

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;
  vector<int> h(n + 1), w(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    h[i] += h[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> w[i];
    w[i] += w[i - 1];
  }

  vector<int> hei(1500001);
  for (int i = 0; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      hei[h[j] - h[i]]++;
    }
  }

  int cnt = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      cnt += hei[w[j] - w[i]];
    }
  }
  cout << cnt << endl;
  return true;
}

int main(void) {
  while (solve()) {
  }
  return 0;
}

//????????????

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool solve() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0)
    return false;
  vector<int> h(n + 1), w(m + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    h[i] += h[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    cin >> w[i];
    w[i] += w[i - 1];
  }

  vector<int> hei(2001);
  for (int i = 0; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      hei[h[j] - h[i]]++;
    }
  }

  int cnt = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      cnt += hei[w[j] - w[i]];
    }
  }
  cout << cnt << endl;
  return true;
}

int main(void) {
  while (solve()) {
  }
  return 0;
}

//????????????
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int total(int k);

using namespace std;
int main() {
  int n, m, i, j, sum, squares;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int h[n], w[m];
    for (i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (i = 0; i < m; i++) {
      cin >> w[i];
    }
    int array[1500001];
    for (i = 0; i < 1500001; ++i) {
      array[i] = 0;
    }
    for (i = 0; i < n; ++i) {
      sum = 0;
      for (j = 0; j < n - i; ++j) {
        sum += h[i + j];
        array[sum]++;
      }
    }

    squares = 0;
    for (i = 0; i < m; ++i) {
      sum = 0;
      for (j = 0; j < m - i; ++j) {
        sum += w[i + j];
        squares += array[sum];
      }
    }
    cout << squares << endl;
  }
}

int total(int k) {
  int i, sum;
  sum = 0;
  for (i = 0; i < k; ++i) {
    sum += (k - i);
  }
  return sum;
}

Error: TLE

Fixed submission:

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

int total(int k);
int count(std::vector<int> arr, int k);

using namespace std;
int main() {
  int n, m, i, j, sum, squares;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int h[n], w[m];
    for (i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (i = 0; i < m; i++) {
      cin >> w[i];
    }
    vector<int> sums_h;

    for (i = 0; i < n; ++i) {
      sum = 0;
      for (j = 0; j < n - i; ++j) {
        sum += h[i + j];
        sums_h.push_back(sum);
      }
    }
    squares = 0;
    for (i = 0; i < m; ++i) {
      sum = 0;
      for (j = 0; j < m - i; ++j) {
        sum += w[i + j];
        squares += count(sums_h, sum);
      }
    }
    cout << squares << endl;
  }
}

int total(int k) {
  int i, sum;
  sum = 0;
  for (i = 0; i < k; ++i) {
    sum += (k - i);
  }
  return sum;
}

int count(vector<int> arr, int k) {
  int sum = 0;
  for (vector<int>::iterator it = arr.begin(); it != arr.end(); ++it) {
    if (k == *it) {
      sum++;
    }
  }
  return sum;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[1500100], b[1500100], A[1500100], B[1500100], p[1500100], q[1500100];
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    memset(p, 0, sizeof(p));
    memset(q, 0, sizeof(q));
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    for (int i = 0; i < n; i++) {
      A[i + 1] = A[i] + a[i];
    }
    for (int i = 0; i < m; i++) {
      B[i + 1] = B[i] + b[i];
    }
    n++, m++;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        p[A[j] - A[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        q[B[j] - B[i]]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < 1500100; i++) {
      cnt += p[i] * q[i];
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int a[2000], b[2000];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    int A[2000] = {}, B[2000] = {};
    for (int i = 0; i < n; i++) {
      A[i + 1] = A[i] + a[i];
    }
    for (int i = 0; i < m; i++) {
      B[i + 1] = B[i] + b[i];
    }
    n++, m++;
    int p[2000] = {}, q[2000] = {};
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        p[A[j] - A[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        q[B[j] - B[i]]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < 2000; i++) {
      cnt += p[i] * q[i];
    }
    cout << cnt << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int h[1501];
int w[1501];

int widths[2000000];
int heights[2000000];

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    fill(widths, widths + 2000000, 0);
    fill(heights, heights + 2000000, 0);
    fill(w, w + 1501, 0);
    fill(w, w + 1501, 0);
    for (int i = 0, acc = 0; i < n; i++) {
      cin >> h[i + 1];
      h[i + 1] += h[i];
    }
    for (int j = 0, acc = 0; j < m; j++) {
      cin >> w[j + 1];
      w[j + 1] += w[j];
    }

    for (int i = 0; i < n + 1; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        // cout << h[j] << ',' << h[i] << endl;
        heights[h[j] - h[i]]++;
      }
    }
    for (int i = 0; i < m + 1; i++) {
      for (int j = i + 1; j < m + 1; j++) {
        // cout << w[j] << ',' << w[i] << endl;
        widths[w[j] - w[i]]++;
      }
    }
    int ans = 0;
    int maxsize = min(n, m) * 1000;
    for (int i = 0; i < maxsize; i++) {
      ans += widths[i] * heights[i];
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int h[1501];
int w[1501];

int widths[10000010];
int heights[10000010];

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    fill(widths, widths + 10000010, 0);
    fill(heights, heights + 10000010, 0);
    fill(w, w + 1501, 0);
    fill(w, w + 1501, 0);
    for (int i = 0, acc = 0; i < n; i++) {
      cin >> h[i + 1];
      h[i + 1] += h[i];
    }
    for (int j = 0, acc = 0; j < m; j++) {
      cin >> w[j + 1];
      w[j + 1] += w[j];
    }

    for (int i = 0; i < n + 1; i++) {
      for (int j = i + 1; j < n + 1; j++) {
        // cout << h[j] << ',' << h[i] << endl;
        heights[h[j] - h[i]]++;
      }
    }
    for (int i = 0; i < m + 1; i++) {
      for (int j = i + 1; j < m + 1; j++) {
        // cout << w[j] << ',' << w[i] << endl;
        widths[w[j] - w[i]]++;
      }
    }
    int ans = 0;
    for (int i = 0; i < 1000; i++) {
      ans += widths[i] * heights[i];
    }
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> w, h;
    int ws = 0, hs = 0;
    h.push_back(0);
    for (int i = 0; i < n; i++) {
      int hi;
      cin >> hi;
      h.push_back(hs += hi);
    }
    w.push_back(0);
    for (int i = 0; i < m; i++) {
      int wi;
      cin >> wi;
      w.push_back(ws += wi);
    }
    vector<int> diag(ws + hs + 1);
    for (int unsigned i = 0; i < h.size(); i++) {
      for (int unsigned j = 0; j < w.size(); j++) {
        diag[h[i] + w[j]]++;
      }
    }
    int ret = 0;
    for (int unsigned i = 0; i < diag.size(); i++) {
      ret += diag[i] * (diag[i] - 1) / 2;
    }
    cout << ret << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <array>
#include <iostream>

using namespace std;

int main() {
  int n, m;
  array<int, 1001> kaisa;
  kaisa[0] = 0;
  for (int i = 1; i < 1001; i++)
    kaisa[i] += kaisa[i - 1] + i;
  while (cin >> n >> m, n) {
    int hs = 0, ws = 0;
    array<bool, 1501> h, w;
    for (int i = 0; i < 1501; i++)
      h[i] = w[i] = false;
    for (int i = 0; i < n; i++) {
      int hi;
      cin >> hi;
      hs += hi;
      h[hs] = true;
    }
    for (int i = 0; i < m; i++) {
      int wi;
      cin >> wi;
      ws += wi;
      w[ws] = true;
    }
    int ret = 0;
    for (int i = 0; i < n; i++) {
      int count = 0;
      for (int j = 0; i + j < n && j < m; j++) {
        count += (int)(h[i + j] && w[j]);
      }
      ret += kaisa[count];
    }
    for (int i = 1; i < m; i++) {
      int count = 0;
      for (int j = 0; j < n && i + j < m; j++) {
        count += (int)(h[i] && w[i + j]);
      }
      ret += kaisa[count];
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && N > 0) {
    int h[1600];
    int w[1600];
    rep(i, N) { cin >> h[i]; }
    rep(i, M) { cin >> w[i]; }

    vector<int> h_edge(1500000, 0);
    vector<int> w_edge(1500000, 0);
    int len;

    rep(i, N) {
      len = 0;
      REP(j, i, N) {
        len += h[j];
        h_edge[len]++;
      }
    }

    rep(i, M) {
      len = 0;
      REP(j, i, M) {
        len += w[j];
        w_edge[len]++;
      }
    }

    int count = 0;
    REP(i, 1, 1500000) { count += h_edge[i] * w_edge[i]; }

    cout << count << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int N, M, ans, tmp;
int row[1500], cow[1500];

int main() {
  while (cin >> N >> M && N > 0) {
    row[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> tmp;
      row[i] = row[i - 1] + tmp;
    }
    cow[0] = 0;
    for (int i = 1; i <= M; i++) {
      cin >> tmp;
      cow[i] = cow[i - 1] + tmp;
    }

    ans = 0;

    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        for (int k = 0; k <= M; k++) {
          for (int l = k + 1; l <= M; l++) {
            if (row[j] - row[i] == cow[l] - cow[k])
              ans++;
          }
        }
      }
    }

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

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int H, W;

  while (cin >> H >> W, H + W) {
    int h[1501] = {};
    int w[1501] = {};
    map<int, int> setH;
    map<int, int> setW;

    for (int i = 1; i <= H; i++)
      cin >> h[i];
    for (int i = 1; i <= W; i++)
      cin >> w[i];

    for (int i = 1; i <= H; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= W; i++)
      w[i] += w[i - 1];

    for (int i = 0; i < H; i++) {
      for (int j = i + 1; j <= H; j++) {
        int t = h[j] - h[i];
        if (setH.count(t))
          setH[t]++;
        else
          setH[t] = 1;
      }
    }
    for (int i = 0; i < W; i++) {
      for (int j = i + 1; j <= W; j++) {
        int t = w[j] - w[i];
        if (setW.count(t))
          setW[t]++;
        else
          setW[t] = 1;
      }
    }

    int cnt = 0;
    for (auto i : setH) {
      if (setW.count(i.first))
        cnt += setW[i.first] * i.second;
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int main() {
  int H, W;

  while (cin >> H >> W, H + W) {
    int h[1001] = {};
    int w[1001] = {};
    map<int, int> setH;
    map<int, int> setW;

    for (int i = 1; i <= H; i++)
      cin >> h[i];
    for (int i = 1; i <= W; i++)
      cin >> w[i];

    for (int i = 1; i <= H; i++)
      h[i] += h[i - 1];
    for (int i = 1; i <= W; i++)
      w[i] += w[i - 1];

    for (int i = 0; i < H; i++) {
      for (int j = i + 1; j <= H; j++) {
        int t = h[j] - h[i];
        if (setH.count(t))
          setH[t]++;
        else
          setH[t] = 1;
      }
    }
    for (int i = 0; i < W; i++) {
      for (int j = i + 1; j <= W; j++) {
        int t = w[j] - w[i];
        if (setW.count(t))
          setW[t]++;
        else
          setW[t] = 1;
      }
    }

    int cnt = 0;
    for (auto i : setH) {
      if (setW.count(i.first))
        cnt += setW[i.first] * i.second;
    }

    cout << cnt << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }
    vector<int> h(n);
    vector<int> w(m);
    vector<int> t(1500001);
    vector<int> t2(1500001);

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

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

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

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

    int ans = 0;
    for (int i = 0; i < 1500001; i++) {
      ans += t[i] * t2[i];
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }
    vector<int> h(n);
    vector<int> w(m);

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

    int ans = 0;

    for (int i = 0; i < n; i++) {
      int s = h[i];
      for (int j = 0; j <= i; j++) {
        if (j > 0) {
          s = h[i] - h[j - 1];
        }
        for (int k = 0; k < m; k++) {
          int s2 = w[k];
          for (int l = 0; l <= k; l++) {
            if (l > 0) {
              s2 = w[k] - w[l - 1];
            }
            if (s == s2) {
              ans++;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int n[2000000][2], r[2000], c[2000], H, W, R, K, LEFT, RIGHT, MID, S;
int main() {
  while (true) {
    r[0] = 0;
    c[0] = 0;
    S = 0;
    memset(n, 0, sizeof(n));
    cin >> H >> W;
    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      cin >> R;
      r[i] = r[i - 1] + R;
    }
    for (int i = 1; i <= W; i++) {
      cin >> R;
      c[i] = c[i - 1] + R;
    }
    for (int i = 0; i <= H; i++) {
      for (int j = i + 1; j <= H; j++) {
        n[r[j] - r[i]][0]++;
      }
    }
    for (int i = 0; i <= W; i++) {
      for (int j = i + 1; j <= W; j++) {
        n[c[j] - c[i]][1]++;
      }
    }
    for (int i = 1; i < 2000000; i++) {
      S += n[i][0] * n[i][1];
    }
    cout << S << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n[2000000], m[2000000], r[2000], c[2000], H, W, R, K, LEFT, RIGHT, MID, S;
int main() {
  while (true) {
    r[0] = 0;
    c[0] = 0;
    S = 0;
    cin >> H >> W;
    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      cin >> R;
      r[i] = r[i - 1] + R;
    }
    for (int i = 1; i <= W; i++) {
      cin >> R;
      c[i] = c[i - 1] + R;
    }
    for (int i = 0; i <= H; i++) {
      for (int j = i + 1; j <= H; j++) {
        n[K] = r[j] - r[i];
        K++;
      }
    }
    K = 0;
    for (int i = 0; i <= W; i++) {
      for (int j = i + 1; j <= W; j++) {
        m[K] = c[j] - c[i];
        K++;
      }
    }
    sort(m, m + K);
    for (int i = 0; i <= K; i++) {
      LEFT = 0;
      RIGHT = K;
      while (LEFT < RIGHT) {
        MID = (LEFT + RIGHT) / 2;
        if (n[i] == MID) {
          S++;
          break;
        }
        if (n[i] < m[MID]) {
          RIGHT = MID;
        } else if (n[i] > MID) {
          LEFT = MID + 1;
        }
      }
    }
    cout << S << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
signed main() {
  int n, m;
  while (cin >> n >> m, n != 0) {
    vector<int> h(n), w(m);
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    vector<int> hs(1500 * 1000 + 10, 0);
    for (int i = 0; i < n; i++) {
      int s = 0;
      for (int j = i; j < n; j++) {
        s += h[j];
        hs[s]++;
      }
    }
    int res = 0;
    for (int i = 0; i < m; i++) {
      int s = 0;
      for (int j = i; j < m; j++) {
        s += w[j];
        res += hs[s];
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n != 0) {
    vector<int> h(n), w(m);
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int dy = 0, dx = 0;
        int ch = h[i + dy], cw = w[j + dx];
        while (i + dy < n && j + dx < m) {
          if (ch == cw) {
            res++;
          }
          if (ch < cw) {
            dy++;
            ch += h[i + dy];
          } else {
            dx++;
            cw += w[j + dx];
          }
        }
      }
    }
    cout << res << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for (int i = (a); i < (n); i++)

int h[1501], w[1501];
int H[2000000], W[2000000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M, N * M) {
    memset(H, 0, 2000000);
    memset(W, 0, 2000000);
    rep(i, N) cin >> h[i];
    rep(i, M) cin >> w[i];
    rep(i, N) {
      int l = 0;
      REP(j, i, N) {
        l += h[j];
        H[l]++;
      }
    }
    rep(i, M) {
      int l = 0;
      REP(j, i, M) {
        l += w[j];
        W[l]++;
      }
    }
    int ans = 0;
    rep(i, 2000000) ans += H[i] * W[i];
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for (int i = (a); i < (n); i++)

int dp[1510];
int h[1510], w[1510];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M, N * M) {
    rep(i, 1510) dp[i] = 0;
    h[0] = w[0] = 0;
    REP(i, 1, N + 1) cin >> h[i];
    REP(i, 1, M + 1) cin >> w[i];
    int ans = 0;
    REP(n, 1, N + 1) {
      REP(m, 1, M + 1) {
        int p = 0, q = 0;
        for (int i = n; i > 0; i--) {
          p += h[i];
          for (int j = m; j > 0; j--) {
            q += w[j];
            if (p == q)
              ans++;
            else if (q > p)
              break;
          }
          q = 0;
        }
      }
    }
    cout << ans << endl;
  }
}