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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int main() {
  int N, M;
  while (scanf("%d %d", &N, &M) && N) {
    vector<int> _H(N + 1, 0), _W(M + 1, 0);
    REP(i, N) {
      scanf("%d", &_H[i + 1]);
      _H[i + 1] += _H[i];
    }
    REP(i, M) {
      scanf("%d", &_W[i + 1]);
      _W[i + 1] += _W[i];
    }
    ++N;
    ++M;
    vector<int> H, W;
    REP(i, N) FOR(j, i + 1, N) H.push_back(_H[j] - _H[i]);
    REP(i, M) FOR(j, i + 1, M) W.push_back(_W[j] - _W[i]);
    SORT(H);
    SORT(W);
    int ans = 0;
    REP(i, H.size())
    ans += upper_bound(W.begin(), W.end(), H[i]) -
           lower_bound(W.begin(), W.end(), H[i]);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

bool check(int &y1, int w, vector<int> &H) {
  int l = y1 + 1, r = (int)(H.size());
  while (r - l > 1) {
    int y2 = (l + r) / 2, res = H[y2] - H[y1];
    if (res == w)
      return true;
    else if (res > w)
      r = y2;
    else
      l = y2;
  }
  if (l >= 0 && l < (int)H.size() && w == H[l] - H[y1])
    return true;
  if (r >= 0 && r < (int)H.size() && w == H[r] - H[y1])
    return true;
  return false;
}

int main() {
  int N, M;
  while (scanf("%d %d", &N, &M) && N) {
    vector<int> H(N + 1, 0), W(M + 1, 0);
    REP(i, N) {
      scanf("%d", &H[i + 1]);
      H[i + 1] += H[i];
    }
    REP(i, M) {
      scanf("%d", &W[i + 1]);
      W[i + 1] += W[i];
    }
    ++N;
    ++M;
    int ans = 0;
    REP(y, N)
    REP(x1, M) FOR(x2, x1 + 1, M) if (check(y, W[x2] - W[x1], H))++ ans;
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int N, M;
vector<int> hh, ww;

#define MAX 1500001

int main(void) {
  while (cin >> N >> M, N | M) {
    hh = vector<int>(N + 1, 0);
    ww = vector<int>(M + 1, 0);

    REPI(i, 1, N) {
      cin >> hh[i];
      hh[i] += hh[i - 1];
    }
    REPI(i, 1, M) {
      cin >> ww[i];
      ww[i] += ww[i - 1];
    }

    int cnt = 0;
    int wd[MAX];

    memset(wd, 0, sizeof(wd));
    rep(i, ww.size()) REP(j, i + 1, ww.size()) wd[ww[j] - ww[i]]++;

    rep(i, hh.size()) REP(j, i + 1, hh.size()) cnt += wd[hh[j] - hh[i]];

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int N, M;
vector<int> hh, ww;

int main(void) {
  while (cin >> N >> M, N | M) {
    hh = vector<int>(N + 1, 0);
    ww = vector<int>(M + 1, 0);

    REPI(i, 1, N) {
      cin >> hh[i];
      hh[i] += hh[i - 1];
    }
    REPI(i, 1, M) {
      cin >> ww[i];
      ww[i] += ww[i - 1];
    }

    int cnt = 0;
    rep(i, hh.size()) REP(j, i + 1, hh.size()) rep(k, ww.size()) {
      int r = ww[k] + hh[j] - hh[i];
      if (binary_search(ww.begin() + k, ww.end(), r))
        cnt++;
    }
    cout << cnt << endl;
  }

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

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

int main() {
  int N, M;
  vector<int> answer;
  while (cin >> N >> M && N != 0) {
    vector<int> h(N + 1, 0), w(M + 1, 0);
    int square = 0;
    map<int, int> length;
    for (int i = 0; i < N; i++) {
      cin >> h.at(i + 1);
      h.at(i + 1) += h.at(i);
    }
    for (int i = 0; i < M; i++) {
      cin >> w.at(i + 1);
      w.at(i + 1) += w.at(i);
    }
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < M + 1; j++) {
        length[h.at(i) - w.at(j)]++;
      }
    }
    for (auto x = length.begin(); x != length.end(); x++) {
      // cout << x->first << " " << x->second << endl;
      square += (x->second * (x->second - 1)) / 2;
    }
    answer.push_back(square);
  }
  for (int i = 0; i < answer.size(); i++) {
    cout << answer.at(i) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  vector<int> answer;
  while (cin >> N >> M && N != 0) {
    vector<int> h(N), w(M);
    vector<int> h_all, w_all;
    int square = 0;
    for (int i = 0; i < N; i++) {
      cin >> h.at(i);
      if (i != 0) {
        h.at(i) += h.at(i - 1);
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w.at(i);
      if (i != 0) {
        w.at(i) += w.at(i - 1);
      }
    }
    h_all.push_back(h.at(0));
    for (int i = 0; i < N - 1; i++) {
      for (int j = i; j < N; j++) {
        h_all.push_back(h.at(j) - h.at(i));
      }
    }
    w_all.push_back(w.at(0));
    for (int i = 0; i < M - 1; i++) {
      for (int j = i; j < M; j++) {
        w_all.push_back(w.at(j) - w.at(i));
      }
    }
    sort(h_all.begin(), h_all.end());
    sort(w_all.begin(), w_all.end());
    for (int i = 0; i < h_all.size(); i++) {
      for (int j = 0; j < w_all.size(); j++) {
        if (h_all.at(i) < w_all.at(j)) {
          break;
        }
        if (h_all.at(i) == w_all.at(j)) {
          square++;
        }
      }
    }
    answer.push_back(square);
  }
  for (int i = 0; i < answer.size(); i++) {
    cout << answer.at(i) << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

int main(void) {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    vec<int> h(n + 1), w(m + 1);
    h[0] = 0;
    w[0] = 0;
    rep(i, n) {
      int l;
      cin >> l;
      h[i + 1] = h[i] + l;
    }
    rep(i, m) {
      int l;
      cin >> l;
      w[i + 1] = w[i] + l;
    }

    int K = 1500 * 1000;
    vec<int> x(K + 1, 0);
    vec<int> y(K + 1, 0);
    rep(i, n) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]]++;
      }
    }
    rep(i, m) {
      for (int j = i + 1; j <= m; j++) {
        y[w[j] - w[i]]++;
      }
    }
    int ans = 0;
    rep(i, K) { ans += x[i + 1] * y[i + 1]; }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

int main(void) {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    vec<int> h(n + 1), w(m + 1);
    h[0] = 0;
    w[0] = 0;
    rep(i, n) {
      int l;
      cin >> l;
      h[i + 1] = h[i] + l;
    }
    rep(i, m) {
      int l;
      cin >> l;
      w[i + 1] = w[i] + l;
    }

    vec<int> x(1001, 0);
    vec<int> y(1001, 0);
    rep(i, n) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]]++;
      }
    }
    rep(i, m) {
      for (int j = i + 1; j <= m; j++) {
        y[w[j] - w[i]]++;
      }
    }
    int ans = 0;
    rep(i, 1000) { ans += x[i + 1] * y[i + 1]; }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cout << #x << ":" << x << endl
#define debug2(x, y) cout << #x << "," << #y ":" << x << "," << y << endl
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

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

void solve(int N, int M) {
  vector<int> h(N), w(M);
  rep(i, N) cin >> h[i];
  rep(i, M) cin >> w[i];

  map<int, int> hh, ww;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i + 1; j <= N; j++) {
      sum += h[j - 1];
      hh[sum]++;
    }
  }

  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i + 1; j <= M; j++) {
      sum += w[j - 1];
      ww[sum]++;
    }
  }

  ll ans = 0;
  for (auto x : hh) {
    ans += (ll)x.second * ww[x.first];
  }
  cout << ans << endl;
}

int main() {
  int N, M;
  while (cin >> N >> M) {
    if (N == 0 and M == 0)
      break;
    solve(N, M);
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cout << #x << ":" << x << endl
#define debug2(x, y) cout << #x << "," << #y ":" << x << "," << y << endl
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

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

void solve(int N, int M) {
  vector<int> h(N), w(M);
  rep(i, N) cin >> h[i];
  rep(i, M) cin >> w[i];

  map<int, int> hh, ww;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i + 1; j <= N; j++) {
      sum += h[j - 1];
      hh[sum]++;
    }
  }

  for (int i = 0; i < M; i++) {
    int sum = 0;
    for (int j = i + 1; j <= M; j++) {
      sum += w[j - 1];
      ww[sum]++;
    }
  }

  //    cout << "hh" << endl;
  //    for(auto x:hh){
  //        debug2(x.first,x.second);
  //    }
  //
  //    cout << "ww" << endl;
  //    for(auto y:ww){
  //        debug2(y.first,y.second);
  //    }

  ll ans = 0;
  for (auto x : hh)
    for (auto y : ww) {
      if (x.first == y.first) {
        ans += (ll)x.second * (ll)y.second;
      }
    }

  cout << ans << endl;
}

int main() {
  int N, M;
  while (cin >> N >> M) {
    if (N == 0 and M == 0)
      break;
    solve(N, M);
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int h[1500], w[1500];
    int sumh = 0, sumw = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      sumh += h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
      sumw += w[i];
    }
    int resulth[sumh + 1], resultw[sumw + 1];
    for (int i = 0; i < sumh + 1; i++) {
      resulth[i] = 0;
    }
    for (int i = 0; i < sumw + 1; i++) {
      resultw[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = i; j < n; j++) {
        tmp += h[j];
        resulth[tmp]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int tmp = 0;
      for (int j = i; j < m; j++) {
        tmp += w[j];
        resultw[tmp]++;
      }
    }
    int ans = 0;
    for (int i = 1; i < min(sumh, sumw) + 1; i++) {
      ans += resultw[i] * resulth[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

#define max 1500 * 1501 / 2

int solve(int n, int m, int h[], int w[]) {
  vector<long long int> wsize, hsize;
  long long int size = 0;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; i + j < n + 2; j++) {
      size = 0;
      for (int k = 0; k < i; k++) {
        size += w[j + k];
      }
      wsize.push_back(size);
    }
  }
  // i : 何マスか j: 最初のマス目
  sort(wsize.begin(), wsize.end());
  for (int i = 1; i < m + 1; i++) {
    for (int j = 1; i + j < m + 2; j++) {
      size = 0;
      for (int k = 0; k < i; k++) {
        size += w[j + k];
      }
      hsize.push_back(size);
    }
  }
  sort(hsize.begin(), hsize.end());
  int cnt = 0;
  for (int i = 0; i < hsize.size(); i++) {
    for (int j = 0; j < wsize.size(); j++) {
      if (hsize[i] == wsize[j]) {
        cnt++;
        break;
      } else if (wsize[j] > hsize[i])
        break;
    }
  }
  return cnt;
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int h[1501] = {}, w[1501] = {};
    for (int i = 1; i < n + 1; i++) {
      cin >> h[i];
    }
    for (int i = 1; i < m + 1; i++) {
      cin >> w[i];
    }
    int ans = solve(n, m, h, w);
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2017-02-04
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, m, h[1500 + 1], w[1500 + 1];
  static int hc[1500 * 1000 + 1], wc[1500 * 1000 + 1];
  h[0] = 0;
  w[0] = 0;
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      h[i + 1] = h[i] + a;
    }
    for (int i = 0; i < m; i++) {
      int a;
      cin >> a;
      w[i + 1] = w[i] + a;
    }
    fill(hc, hc + 1500 * 1000 + 1, 0);
    fill(wc, wc + 1500 * 1000 + 1, 0);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        hc[h[j] - h[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j <= m; j++) {
        wc[w[j] - w[i]]++;
      }
    }
    int c = 0;
    for (int i = 0; i < 1500 * 1000 + 1; i++) {
      c += hc[i] * wc[i];
    }
    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 2017-02-04
#include <iostream>
using namespace std;

int main() {
  int n, m, h[1500 + 1], w[1500 + 1];
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    h[n] = 0;
    w[m] = 0;
    int c = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int k = i;
        int l = j;
        int u = h[i];
        int v = w[j];
        while (k < n && l < m) {
          if (u < v) {
            k++;
            u += h[k];
          } else if (u > v) {
            l++;
            v += w[l];
          } else {
            c++;
            k++;
            u += h[k];
          }
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N + 1), w(M + 1);
    h[0] = 0, w[0] = 0;
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];
    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];
    vector<int> hs(h.back() + 1, 0), ws(w.back() + 1, 0);
    rep(i, N) REP(j, i + 1, N + 1) hs[h[j] - h[i]]++;
    rep(i, M) REP(j, i + 1, M + 1) ws[w[j] - w[i]]++;
    int ans = 0;
    rep(i, min(h.back(), w.back()) + 1) ans += hs[i] * ws[i];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N) {
    vector<int> h(N + 1), w(M + 1);
    h[0] = 0, w[0] = 0;
    rep(i, N) cin >> h[i + 1];
    rep(i, M) cin >> w[i + 1];
    rep(i, N) h[i + 1] += h[i];
    rep(i, M) w[i + 1] += w[i];
    vector<int> hs(h.back() + 1, 0), ws(w.back() + 1, 0);
    rep(i, N) REP(j, i + 1, N + 1) hs[h[j] - h[i]]++;
    rep(i, M) REP(j, i + 1, M + 1) ws[w[j] - w[i]]++;
    int ans = 0;
    rep(i, max(h.back(), w.back()) + 1) ans += hs[i] * ws[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
int m;

int hmp[1500 * 1000 + 1];
int wmp[1500 * 1000 + 1];

int main() {
  while (cin >> n >> m, n) {
    fill(hmp, hmp + 1 + 1500 * 1000, 0);
    fill(wmp, wmp + 1 + 1500 * 1000, 0);

    vector<int> h(n + 1, 0), w(m + 1, 0);

    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];
    }

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

    i64 result = 0;

    for (int i = 1; i <= 1500 * 1000; i++) {
      result += hmp[i] * wmp[i];
    }
    cout << result << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
int m;

int hmp[1500 * 1000 + 1];
int wmp[1500 * 1000 + 1];

int main() {
  while (cin >> n >> m, n) {
    fill(hmp, hmp + 1 + 1500 * 1000, 0);
    fill(wmp, wmp + 1 + 1500 * 1000, 0);

    vector<int> h(n + 1, 0), w(n + 1, 0);

    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];
    }

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

    i64 result = 0;

    for (int i = 1; i <= 1500 * 1000; i++) {
      result += hmp[i] * wmp[i];
    }
    cout << result << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, i, j, cnt, hsum, wsum, tmp, cnti, cntj;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int h[1502], w[1502];
    vector<int> sh, sw;
    hsum = wsum = 0;
    h[0] = 0;
    for (i = 1; i <= n; i++) {
      cin >> tmp;
      hsum += tmp;
      h[i] = hsum;
    }
    w[0] = 0;
    for (i = 1; i <= m; i++) {
      cin >> tmp;
      wsum += tmp;
      w[i] = wsum;
    }

    for (i = 0; i <= n - 1; i++) {
      for (j = i + 1; j <= n; j++) {
        sh.push_back(h[j] - h[i]);
      }
    }
    for (i = 0; i <= m - 1; i++) {
      for (j = i + 1; j <= m; j++) {
        sw.push_back(w[j] - w[i]);
      }
    }

    sort(sh.begin(), sh.end());
    sort(sw.begin(), sw.end());

    i = j = 0;
    cnt = 0;
    int maxi = sh.size(), maxj = sw.size();
    while (i < maxi && j < maxj) {
      if (sh[i] == sw[j]) {
        cnti = cntj = 1;
        while (i < maxi - 1 && sh[i] == sh[i + 1]) {
          cnti++;
          i++;
        }
        while (j < maxj - 1 && sw[j] == sw[j + 1]) {
          cntj++;
          j++;
        }
        cnt += cnti * cntj;
        i++;
        j++;
      } else if (sh[i] < sw[j]) {
        i++;
      } else {
        j++;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

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

    int cnt = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m;) {
        if (h[i] == w[j])
          cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}