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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 999999999
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x % (mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool debug = false;
/*---------------------------------------------------*/

int n, m;
ll ans;
int wide[1500001], hight[1500001];
int w[1505], h[1505];

void enumerate_hight() {
  int sum;
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = i; j < n; j++) {
      sum += h[j];
      hight[sum]++;
    }
  }
}

void enumerate_wide() {
  int sum;
  for (int i = 0; i < m; i++) {
    sum = 0;
    for (int j = i; j < m; j++) {
      sum += w[j];
      wide[sum]++;
    }
  }
}

void solve() {
  ll max_w = 0, max_h = 0, maxi;
  ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
    max_h += h[i];
  }
  enumerate_hight();
  for (int i = 0; i < m; i++) {
    scanf("%d", &w[i]);
    max_w += w[i];
  }
  enumerate_wide();
  maxi = max(max_w, max_h);
  rep(i, maxi + 1) {
    ans += wide[i] * hight[i];
    wide[i] = hight[i] = 0;
  }
  cout << ans << endl;
}

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

Error: -11

Fixed submission:

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

int main() {
  int N, M;
  int height[1505];
  int width[1505];
  int H[1500001];
  int W[1500001];
  int max_h, max_w;
  int sum;
  int cnt;
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    max_h = max_w = 0;
    for (int i = 0; i < N; i++) {
      scanf("%d", &height[i]);
      max_h += height[i];
    }
    for (int i = 0; i < M; i++) {
      scanf("%d", &width[i]);
      max_w += width[i];
    }
    for (int i = 0; i < N; i++) {
      sum = 0;
      for (int j = i; j < N; j++) {
        sum += height[j];
        H[sum]++;
      }
    }
    for (int i = 0; i < M; i++) {
      sum = 0;
      for (int j = i; j < M; j++) {
        sum += width[j];
        W[sum]++;
      }
    }
    cnt = 0;
    for (int i = 0; i <= (max_h > max_w) ? max_h : max_w; i++) {
      cnt += H[i] * W[i];
      H[i] = W[i] = 0;
    }
    cout << cnt << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    int h[1024 * 1500] = {0}, w[1024 * 1500] = {0};
    int tmp_h[n + 1], tmp_w[m + 1];
    int sum1 = 0, sum2 = 0;
    for (int i = 0; i < n; i++) {
      cin >> tmp_h[i];
      sum1 += tmp_h[i];
      // cout<<tmp_h[i]<<endl;
    }
    for (int i = 0; i < m; i++) {
      cin >> tmp_w[i];
      sum2 += tmp_w[i];
      // cout<<tmp_w[i]<<endl;
    }
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = i; j < n; j++) {
        tmp += tmp_h[j];
        h[tmp]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int tmp = 0;
      for (int j = i; j < m; j++) {
        tmp += tmp_w[j];
        w[tmp]++;
      }
    }
    long long res = 0;
    for (int i = 1; i <= min(sum1, sum2); i++) {
      res += h[i] * w[i];
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    std::vector<int> h, w;
    for (int i = 0; i < n; i++) {
      int he;
      cin >> he;
      h.push_back(he);
    }
    for (int i = 0; i < m; i++) {
      int wi;
      cin >> wi;
      w.push_back(wi);
    }
    multiset<int> hsum, wsum;
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = i; j < n; j++) {
        tmp += h[j];
        hsum.insert(tmp);
      }
    }
    for (int i = 0; i < m; i++) {
      int tmp = 0;
      for (int j = i; j < m; j++) {
        tmp += w[j];
        wsum.insert(tmp);
      }
    }
    int res = 0;
    for (int i = 0; i < hsum.size(); i++) {
      auto itr = hsum.begin();
      for (int j = 0; j < i; j++)
        itr++;
      res += wsum.count(*itr);
    }
    cout << res << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, x, y) for (int(i) = (x); (i) < (y); (i)++)

int main() {

  while (true) {
    vector<int> X, Y;
    vector<int> lengthX, lengthY;
    int x, y;
    int n, m;
    int sumx, sumy;
    int count = 0;

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

    rep(i, 0, n) {
      cin >> y;
      Y.push_back(y);
    }
    rep(i, 0, m) {
      cin >> x;
      X.push_back(x);
    }

    rep(i, 0, n) {
      sumy = 0;
      rep(j, i, n) {
        sumy += Y[j];
        lengthY.push_back(sumy);
      }
    }
    rep(i, 0, m) {
      sumx = 0;
      rep(j, i, m) {
        sumx += X[j];
        lengthX.push_back(sumx);
      }
    }
    sort(lengthX.begin(), lengthX.end());
    sort(lengthY.begin(), lengthY.end());

    int i = 0;
    int j = 0;
    while (i < lengthY.size() && j < lengthX.size()) {
      if (lengthY[i] < lengthX[j]) {
        i++;
      } else if (lengthY[i] > lengthX[j]) {
        j++;
      } else {
        int k = j + 1;
        while (k < lengthX.size()) {
          if (lengthX[k] != lengthX[j]) {
            break;
          }
          k++;
        }
        count += k - j;
        i++;
      }
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (int(i) = (x); (i) < (y); (i)++)

int main() {

  while (true) {
    vector<int> X, Y;
    vector<int> lengthX, lengthY;
    int x, y;
    int n, m;
    int sumx, sumy;
    int count = 0;

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

    rep(i, 0, n) {
      cin >> y;
      Y.push_back(y);
    }
    rep(i, 0, m) {
      cin >> x;
      X.push_back(x);
    }

    rep(i, 0, n) {
      sumy = 0;
      rep(j, i, n) {
        sumy += Y[j];
        lengthY.push_back(sumy);
      }
    }
    rep(i, 0, m) {
      sumx = 0;
      rep(j, i, m) {
        sumx += X[j];
        lengthX.push_back(sumx);
      }
    }
    // sort(lengthX.begin(), lengthX.end());
    // sort(lengthY.begin(), lengthY.end());

    rep(i, 0, lengthX.size()) {
      rep(j, 0, lengthY.size()) {
        if (lengthX[i] == lengthY[j]) {
          count++;
        }
      }
    }

    cout << count << endl;
  }

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

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

int main() {
  int N, M, p, count;
  vector<int> N_n(1500000, 0), M_n(1500000, 0);
  vector<int> N_s, M_s;
  while (cin >> N >> M, N) {
    count = 0;
    for (int i = 0; i < N; i++) {
      cin >> p;
      N_s.push_back(p);
    }
    for (int i = 0; i < M; i++) {
      cin >> p;
      M_s.push_back(p);
    }
    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += N_s[j];
        N_n[p - 1]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += M_s[j];
        M_n[p - 1]++;
      }
    }
    for (int i = 0; i < 1500000; i++) {
      count += M_n[i] * N_n[i];
      if (M_n[i] != 0 || N_n[i] != 0)
        M_n[i] = N_n[i] = 0;
    }
    cout << count << endl;
    N_s.clear();
    M_s.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M, l, count, N_s, M_s;
  vector<int> N_n, M_n;
  while (cin >> N >> M, N) {
    for (int i = 0; i < N; i++) {
      count = 0;
      cin >> l;
      N_n.push_back(l);
    }
    for (int i = 0; i < M; i++) {
      cin >> l;
      M_n.push_back(l);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        for (int k = i, N_s = 0; k < N; k++) {
          N_s += N_n[k];
          for (l = j, M_s = 0; l < M; l++) {
            M_s += M_n[l];
            if (N_s == M_s) {
              count++;
              break;
            }
          }
        }
      }
    }
    cout << count << endl;
    N_n.clear();
    M_n.clear();
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; i++)
#define MAX 1501 * 1001 + 1

int ch[MAX], cw[MAX];
int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int count = 0;
    int h[1501], w[1501];
    REP(i, 0, N) cin >> h[i];
    REP(i, 0, M) cin >> w[i];

    REP(i, 0, MAX) {
      ch[i] = 0;
      cw[i] = 0;
    }
    REP(i, 0, N) {
      int sh = 0;
      REP(j, i, N) {
        sh += h[j];
        ch[sh]++;
      }
    }
    REP(i, 0, M) {
      int sw = 0;
      REP(j, i, M) {
        sw += w[j];
        cw[sw]++;
      }
    }
    REP(i, 0, MAX) count += ch[i] * cw[i];

    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; i++)

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int count = 0;
    int h[1501], w[1501];
    REP(i, 0, N) cin >> h[i];
    REP(i, 0, M) cin >> w[i];

    REP(i, 0, N) {
      REP(j, 0, M) {
        int sh = 0;
        REP(k, i, N) {
          sh += h[k];

          int sw = 0;
          REP(l, j, M) {
            sw += w[l];

            if (sh == sw) {
              count++;
              break;
            } else if (sh < sw)
              break;
          }
        }
      }
    }

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

///
// File:  2015.cpp
// Author: ymiyamoto
//
// Created on Sat Dec  2 23:41:55 2017
//

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

using namespace std;

int32_t main() {
  while (true) {
    uint32_t N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    vector<uint64_t> heights;
    heights.push_back(0);
    for (uint32_t i = 1; i <= N; i++) {
      uint32_t h;
      cin >> h;
      h += heights[i - 1];
      heights.push_back(h);
    }

    vector<uint64_t> widths;
    widths.push_back(0);
    for (uint32_t i = 1; i <= M; i++) {
      uint32_t w;
      cin >> w;
      w += widths[i - 1];
      widths.push_back(w);
    }

    vector<uint64_t> hs;
    for (uint32_t i = 0; i < heights.size(); i++) {
      for (uint32_t j = i + 1; j < heights.size(); j++) {
        hs.push_back(heights[j] - heights[i]);
      }
    }

    vector<uint64_t> ws;
    for (uint32_t i = 0; i < widths.size(); i++) {
      for (uint32_t j = i + 1; j < widths.size(); j++) {
        ws.push_back(widths[j] - widths[i]);
      }
    }

    sort(ws.begin(), ws.end());
    uint64_t count = 0;
    for (uint32_t i = 0; i < hs.size(); i++) {
      count += upper_bound(ws.begin(), ws.end(), hs[i]) -
               lower_bound(ws.begin(), ws.end(), hs[i]);
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

///
// File:  2015.cpp
// Author: ymiyamoto
//
// Created on Sat Dec  2 23:41:55 2017
//

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

using namespace std;

int32_t main() {
  while (true) {
    uint32_t N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    vector<uint64_t> heights;
    heights.push_back(0);
    for (uint32_t i = 1; i <= N; i++) {
      uint32_t h;
      cin >> h;
      h += heights[i - 1];
      heights.push_back(h);
    }

    vector<uint64_t> widths;
    widths.push_back(0);
    for (uint32_t i = 1; i <= M; i++) {
      uint32_t w;
      cin >> w;
      w += widths[i - 1];
      widths.push_back(w);
    }

    vector<uint64_t> hs;
    for (uint32_t i = 0; i < heights.size(); i++) {
      for (uint32_t j = i + 1; j < heights.size(); j++) {
        hs.push_back(heights[j] - heights[i]);
      }
    }

    vector<uint64_t> ws;
    for (uint32_t i = 0; i < widths.size(); i++) {
      for (uint32_t j = i + 1; j < widths.size(); j++) {
        ws.push_back(widths[j] - widths[i]);
      }
    }

    uint32_t count = 0;
    for (uint32_t i = 0; i < hs.size(); i++) {
      for (uint32_t j = 0; j < ws.size(); j++) {
        if (hs[i] == ws[j]) {
          count++;
        }
      }
    }

    cout << count << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define M 1500 * 1000

int h_pos[M + 1];
int w_pos[M + 1];

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> h(n), w(m);
    memset(h_pos, 0, sizeof(h_pos));
    memset(w_pos, 0, sizeof(w_pos));
    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 sum = 0;
      for (int j = i; j < n; ++j) {
        sum += h[j];
        h_pos[sum]++;
      }
    }
    for (int i = 0; i < m; ++i) {
      int sum = 0;
      for (int j = i; j < m; ++j) {
        sum += w[j];
        w_pos[sum]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i <= M; ++i) {
      cnt += h_pos[i] * w_pos[i];
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M 1500 * 1000

int h_pos[M + 1];
int w_pos[M + 1];

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h(n, 0), w(n, 0);
    memset(h_pos, 0, sizeof(h_pos));
    memset(w_pos, 0, sizeof(h_pos));
    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 sum = 0;
      for (int j = i; j < n; ++j) {
        sum += h[j];
        h_pos[sum]++;
      }
    }
    for (int i = 0; i < m; ++i) {
      int sum = 0;
      for (int j = i; j < m; ++j) {
        sum += w[j];
        w_pos[sum]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i <= M; ++i) {
      cnt += h_pos[i] * w_pos[i];
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, m;
vector<int> y;
vector<int> x;

void solve() {
  map<int, int> line;
  int add = 0;
  y.push_back(0);
  x.push_back(0);
  rep(i, n) {
    int a;
    cin >> a;
    add += a;
    y.push_back(add);
  }
  add = 0;
  rep(i, m) {
    int a;
    cin >> a;
    add += a;
    x.push_back(add);
  }
  rep(i, y.size()) rep(j, x.size()) line[y[i] - x[j]]++;

  int ans = 0;
  map<int, int>::iterator it = line.begin();
  while (it != line.end()) {
    ans += it->second * (it->second - 1) / 2;

    it++;
  }

  cout << ans << endl;
  y.clear();
  x.clear();
}

int main() {
  while (cin >> n >> m) {
    if (n + m == 0)
      return 0;
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, m;
vector<int> h;
vector<int> w;
bool grid[1503][1503];

void solve() {
  rep(i, 1503) rep(j, 1503) grid[i][j] = false;
  int add = 0;
  h.push_back(0);
  w.push_back(0);
  rep(i, n) {
    int a;
    cin >> a;
    add += a;
    h.push_back(add);
  }
  add = 0;
  rep(i, m) {
    int a;
    cin >> a;
    add += a;
    w.push_back(add);
  }
  sort(h.begin(), h.end());
  sort(w.begin(), w.end());
  rep(i, h.size()) rep(j, w.size()) grid[h[i]][w[j]] = true;
  int ans = 0;

  rep(i, h.size()) rep(j, w.size()) {
    if (grid[h[i]][w[j]]) {
      rep(k, w.size()) {
        if (w[j] >= w[k])
          continue;
        int t = w[k] - w[j];
        if (grid[h[i] + t][w[k]])
          ans++;
      }
    }
  }
  cout << ans << endl;
  w.clear();
  h.clear();
}

int main() {
  while (cin >> n >> m) {
    if (n + m == 0)
      return 0;
    solve();
  }

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

#include <iostream>
#include <vector>
using namespace std;
#define MAX_len 1500001

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    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> h_count(MAX_len), w_count(MAX_len);

    for (int i1 = 0; i1 < N; i1++) {
      int len = 0;
      for (int i2 = i1; i2 < N; i2++) {
        len += h[i2];
        h_count[len]++;
      }
    }
    for (int i1 = 0; i1 < M; i1++) {
      int len = 0;
      for (int i2 = i1; i2 < M; i2++) {
        len += w[i2];
        w_count[len]++;
      }
    }
    int ans = 0;
    for (int i = 1; i < MAX_len; i++) {
      ans += h_count[i] * w_count[i];
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int getdist(int data[], int n1, int n2) {
  int sum = 0;
  for (int i = n1; i < n2 + 1; i++) {
    sum += data[i];
  }
  return sum;
}
int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    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];
    multiset<int> hset;
    multiset<int> wset;
    set<int> edge;
    for (int i1 = 0; i1 < N; i1++) {
      for (int i2 = i1; i2 < N; i2++) {
        int t = getdist(h, i1, i2);
        edge.insert(t);
        hset.insert(t);
      }
    }
    for (int j1 = 0; j1 < M; j1++) {
      for (int j2 = j1; j2 < M; j2++) {
        int t = getdist(w, j1, j2);
        edge.insert(t);
        wset.insert(t);
      }
    }
    set<int>::iterator itr = edge.begin();
    int ans = 0;
    while (itr != edge.end()) {
      ans += hset.count(*itr) * wset.count(*itr);
      ++itr;
    }
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;
int c[3000010];
int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1503], w[1503];
    int ans = 0;
    REP(i, 0, 3000010) c[i] = 0;
    h[0] = w[0] = 0;
    REP(i, 1, N + 1) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    REP(i, 1, M + 1) {
      cin >> w[i];
      w[i] += w[i - 1];
    }
    REP(i, 0, N + 1) REP(j, 0, M + 1) c[h[i] - w[j] + 1500000]++;
    REP(i, 0, 3000010) ans += c[i] * (c[i] - 1) / 2;
    p(ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1503], w[1503];
    int ans = 0;
    int c[3010] = {};
    h[0] = w[0] = 0;
    REP(i, 1, N + 1) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    REP(i, 1, M + 1) {
      cin >> w[i];
      w[i] += w[i - 1];
    }
    REP(i, 0, N + 1) REP(j, 0, M + 1) c[h[i] - w[j] + 1500]++;
    REP(i, 0, 3010) ans += c[i] * (c[i] - 1) / 2;
    p(ans);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1501], w[1501];
    map<int, int> allh, allw;
    int ans = 0, mx = 0, k;
    REP(i, 0, N) {
      cin >> h[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    mx = max(mx, k);
    REP(i, 0, M) {
      cin >> w[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    mx = max(mx, k);
    REP(i, 1, mx + 1) { ans += allh[i] * allw[i]; }
    p(ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << S << endl
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1501] = {0}, w[1501] = {0}, allh[1501] = {0}, allw[1501] = {0};
    int ans = 0;
    REP(i, 0, N) {
      cin >> h[i];
      int k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    REP(i, 0, M) {
      cin >> w[i];
      int k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    REP(i, 1, 1501) { ans += allh[i] * allw[i]; }
    p(ans);
  }

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

#include <algorithm>
#include <array>
#include <iostream>

using namespace std;

int main() {
  array<int, 1501> x, y;
  array<int, 1500001> t;
  int n, m;
  while (cin >> n >> m && n + m != 0) {
    y[0] = x[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> y[i];
      y[i] += y[i - 1];
    }
    for (int i = 1; i <= m; i++) {
      cin >> x[i];
      x[i] += x[i - 1];
    }
    fill(t.begin(), t.begin() + max(n, m) * 1000, 0);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++)
        t[y[j] - y[i]]++;
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j <= m; j++)
        cnt += t[x[j] - x[i]];
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>

using namespace std;

int main() {
  array<int, 1501> y, x;
  int n, m;
  while (cin >> n >> m && n + m != 0) {
    y[0] = x[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> y[i];
      y[i] += y[i - 1];
    }
    for (int i = 1; i <= m; i++) {
      cin >> x[i];
      x[i] += x[i - 1];
    }
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int k = i + 1, l = j + 1;
        while (k <= n && l <= m) {
          int h = y[k] - y[i];
          int w = x[l] - x[j];
          if (h < w)
            k++;
          else if (h > w)
            l++;
          else
            cnt++, k++, l++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}