alexjercan/bugnet huggingface dataset:
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 out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int solve(int n, int m) {
  vi w(m + 1, 0), h(n + 1, 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];

  map<int, int> hm, wm;
  rep(i, n + 1) for (int j = i + 1; j <= n; ++j) hm[h[j] - h[i]]++;
  rep(i, m + 1) for (int j = i + 1; j <= m; ++j) wm[w[j] - w[i]]++;

  int ans = 0;
  rep(i, max(h[n], w[n]) + 1) ans += (hm[i] * wm[i]);
  return ans;
}

int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    out(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 out(S) cout << (S) << endl;
#define ShowAll(collection)                                                    \\
  for (auto i : collection) {                                                  \\
    out(i);                                                                    \\
  }
#define beginend(v) v.begin(), v.end()

using pii = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvc = vector<vector<char>>;
using ti3 = tuple<int, int, int>;
template <typename T> void removeAt(vector<T> &v, int index) {
  v.erase(v.begin() + index);
}

int solve(int n, int m) {
  vi w(m + 1, 0), h(n + 1, 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];

  map<int, int> hm, wm;
  rep(i, n + 1) for (int j = i + 1; j <= n; ++j) hm[h[j] - h[i]]++;
  rep(i, m + 1) for (int j = i + 1; j <= m; ++j) wm[w[j] - w[i]]++;

  int ans = 0;
  for (auto hh : hm) {
    int hk, hv;
    tie(hk, hv) = hh;
    for (auto ww : wm) {
      int wk, wv;
      tie(wk, wv) = ww;
      if (hk == wk)
        ans += wv * hv;
    }
  }
  return ans;
}

int main() {
  int N, M;
  while (cin >> N >> M, N | M) {
    out(solve(N, M));
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  int N, M, i, j, t, ans;
  while (cin >> N >> M, N | M) {
    vector<int> h(N), w(M);
    map<int, int> m;
    for (i = 0; i < N; i++)
      cin >> h[i];
    for (i = 0; i < M; i++)
      cin >> w[i];

    for (i = 0; i < N; i++) {
      t = 0;
      for (j = i; j < N; j++) {
        t += h[j];
        m[t]++;
      }
    }
    ans = 0;
    for (i = 0; i < M; i++) {
      t = 0;
      for (j = i; j < M; j++) {
        t += w[j];
        if (m.count(t))
          ans += m[t];
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:


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

int main() {
  int N, M, i, j, x, y, lx, ly, cnt;
  vector<int> h, w;
  while (cin >> N >> M, N || M) {
    h.resize(N);
    w.resize(M);
    for (i = 0; i < N; i++)
      cin >> h[i];
    for (i = 0; i < M; i++)
      cin >> w[i];
    cnt = 0;
    for (i = 0; i < N; i++) {
      for (j = 0; j < M; j++) {
        x = i;
        y = j;
        lx = h[x];
        ly = w[y];
        while (x < N && y < M) {
          if (lx == ly)
            cnt++;
          if (lx <= ly) {
            x++;
            lx += h[x];
          } else {
            y++;
            ly += w[y];
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_SIZE = 2000001;

int main() {

  // int mtate[MAX_SIZE];
  // int myoko[MAX_SIZE];
  vector<int> mtate(MAX_SIZE, 0);
  vector<int> myoko(MAX_SIZE, 0);

  int n, m;
  vector<int> tate;
  vector<int> yoko;
  while (cin >> n >> m && !(n == 0 && m == 0)) {
    tate.clear();
    yoko.clear();
    tate.push_back(0);
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      int p = tate.size() - 1;
      tate.push_back(t + tate[p]);
    }
    yoko.push_back(0);
    for (int i = 0; i < m; i++) {
      int t;
      cin >> t;
      int p = yoko.size() - 1;
      yoko.push_back(t + yoko[p]);
    }
    // ツ閉敖づ個青板づーcnt
    // fill(mtate,mtate+MAX_SIZE,0);
    // fill(myoko,myoko+MAX_SIZE,0);
    // memset(mtate,0,sizeof(mtate));
    // memset(myoko,0,sizeof(myoko));
    for (int i = 0; i < MAX_SIZE; i++) {
      mtate[i] = myoko[i] = 0;
    }

    for (int i = 0; i < tate.size(); i++) {
      for (int j = i + 1; j < tate.size(); j++) {
        mtate[tate[j] - tate[i]]++;
      }
    }
    for (int i = 0; i < yoko.size(); i++) {
      for (int j = i + 1; j < yoko.size(); j++) {
        myoko[yoko[j] - yoko[i]]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < MAX_SIZE; i++) {
      cnt += (mtate[i] * myoko[i]);
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_SIZE = 5000001;

int main() {

  int mtate[MAX_SIZE];
  int myoko[MAX_SIZE];
  int n, m;
  vector<int> tate;
  vector<int> yoko;
  while (cin >> n >> m && !(n == 0 && m == 0)) {
    tate.clear();
    yoko.clear();
    tate.push_back(0);
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      int p = tate.size() - 1;
      tate.push_back(t + tate[p]);
    }
    yoko.push_back(0);
    for (int i = 0; i < m; i++) {
      int t;
      cin >> t;
      int p = yoko.size() - 1;
      yoko.push_back(t + yoko[p]);
    }
    // ツ閉敖づ個青板づーcnt
    // fill(mtate,mtate+MAX_SIZE,0);
    // fill(myoko,myoko+MAX_SIZE,0);
    // memset(mtate,0,sizeof(mtate));
    // memset(myoko,0,sizeof(myoko));
    for (int i = 0; i < MAX_SIZE; i++) {
      mtate[i] = myoko[i] = 0;
    }

    for (int i = 0; i < tate.size(); i++) {
      for (int j = i + 1; j < tate.size(); j++) {
        mtate[tate[j] - tate[i]]++;
      }
    }
    for (int i = 0; i < yoko.size(); i++) {
      for (int j = i + 1; j < yoko.size(); j++) {
        myoko[yoko[j] - yoko[i]]++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < MAX_SIZE; i++) {
      cnt += (mtate[i] * myoko[i]);
    }
    cout << cnt << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int N = 1500 * 1000;
  while (1) {
    cin >> n;
    cin >> m;
    if (n * m == 0) {
      break;
    }
    int v_interval[n], h_interval[m];
    for (int i = 0; i < n; i++) {
      cin >> v_interval[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h_interval[i];
    }

    short v_length_num[N + 1];
    for (int i = 0; i < N + 1; i++) {
      v_length_num[i] = 0;
    }
    int v_length = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        if (i == j) {
          v_length = v_interval[i];
        } else {
          v_length += v_interval[j];
        }
        v_length_num[v_length]++;
      }
    }

    short h_length_num[N + 1];
    for (int i = 0; i < N + 1; i++) {
      h_length_num[i] = 0;
    }
    int h_length = 0;
    for (int i = 0; i < m; i++) {
      for (int j = i; j < m; j++) {
        if (i == j) {
          h_length = h_interval[i];
        } else {
          h_length += h_interval[j];
        }
        h_length_num[h_length]++;
      }
    }

    int count = 0;
    for (int i = 0; i < N + 1; i++) {
      count += v_length_num[i] * h_length_num[i];
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  int N = 1500 * 1000;
  while (1) {
    cin >> n;
    cin >> m;
    if (n * m == 0) {
      break;
    }
    int v_interval[n], h_interval[m];
    for (int i = 0; i < n; i++) {
      cin >> v_interval[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> h_interval[i];
    }

    int h_num = m * (m + 1) / 2;
    int h_lengthes[h_num];
    int temp = 0;
    short h_length_num[N + 1];
    for (int i = 0; i < N + 1; i++) {
      h_length_num[i] = 0;
    }
    for (int i = 0; i < m; i++) {
      for (int j = i; j < m; j++, temp++) {
        if (i == j) {
          h_lengthes[temp] = h_interval[i];
        } else {
          h_lengthes[temp] = h_lengthes[temp - 1] + h_interval[j];
        }
        h_length_num[h_lengthes[temp]]++;
      }
    }

    int v_num = n * (n + 1) / 2;
    int v_lengthes[v_num];
    temp = 0;
    short v_length_num[N + 1];
    for (int i = 0; i < N + 1; i++) {
      v_length_num[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++, temp++) {
        if (i == j) {
          v_lengthes[temp] = v_interval[i];
        } else {
          v_lengthes[temp] = v_lengthes[temp - 1] + v_interval[j];
        }
        v_length_num[v_lengthes[temp]]++;
      }
    }

    int count = 0;
    for (int i = 0; i < N + 1; i++) {
      count += h_length_num[i] * v_length_num[i];
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (i = 0; i < n; i++)
#define rep2(i, n) for (i = 1; i <= n; i++)

int main() {
  int n, m;
  int i, j, k;
  static int a[1500001];
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

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

    for (i = 0; i <= n; i++) {
      for (j = 0; j < i; j++) {
        a[h[i] - h[j]]++;
      }
    }

    int c = 0;
    for (i = 0; i <= m; i++) {
      for (j = 0; j < i; j++) {
        c += a[w[i] - w[j]];
      }
    }
    //		for(i=0;i<50;i++) cout << i << "   " << a[i] << endl;
    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (i = 0; i < n; i++)
#define rep2(i, n) for (i = 1; i <= n; i++)

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

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

    multiset<int> s;
    for (i = 0; i <= n; i++) {
      for (j = 0; j < i; j++) {
        s.insert(h[j] - h[i]);
      }
    }

    int c = 0;
    for (i = 0; i <= m; i++) {
      for (j = 0; j < i; j++) {
        c += s.count(w[j] - w[i]);
      }
    }

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

#include <iostream>
#include <vector>
#define M 1500000
using namespace std;
int main(void) {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    vector<int> h(n), w(m);
    vector<int> hl(M, 0), wl(M, 0);
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = 0;
      for (int j = i; j < n; j++) {
        sum += h[j];
        hl[sum - 1]++;
      }
    }
    for (int i = 0; i < m; i++) {
      sum = 0;
      for (int j = i; j < m; j++) {
        sum += w[j];
        wl[sum - 1]++;
      }
    }
    int t = 0;
    for (int i = 0; i < M; i++) {
      t += hl[i] * wl[i];
    }
    cout << t << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    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 t = 0;
    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += h[j];
        for (int k = 0; k < m; k++) {
          int sum2 = 0;
          for (int l = k; l < m; l++) {
            sum2 += w[l];
            if (sum < sum2)
              break;
            if (sum == sum2) {
              t++;
              break;
            }
          }
        }
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int INF = 10000000;

int n, m;

int w[1510];
int h[1510];

int wf[1510 * 1000];
int hf[1510 * 1000];

int main() {

  while (1) {
    memset(wf, 0, 1510 * 1000 * sizeof(int));
    memset(hf, 0, 1510 * 1000 * sizeof(int));

    cin >> n >> m;

    if (!n && !m)
      break;

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

    for (int i = 0; i < n; i++) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += w[j];
        wf[sum]++;
      }
    }

    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += h[j];
        hf[sum]++;
      }
    }

    int sum = 0;

    for (int i = 0; i < 1510 * 1000; i++) {
      sum += wf[i] * hf[i];
    }

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int INF = 10000000;

int n, m;

int w[1510];
int h[1510];

int f[1510 * 1000];

int main() {

  while (1) {
    memset(f, 0, 1510 * 1000);

    cin >> n >> m;

    if (!w && !h)
      break;

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

    int sum = 0;

    for (int i = 0; i < n; i++) {
      int tmpsum = 0;
      for (int j = i; j < n; j++) {
        tmpsum += w[j];
        f[tmpsum]++;
      }
    }

    for (int i = 0; i < m; i++) {
      int tmpsum = 0;
      for (int j = i; j < m; j++) {
        tmpsum += h[j];

        sum += f[tmpsum];
      }
    }

    cout << sum << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

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

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n + m == 0)
      break;
    int h[n], w[m], max_h = 0, max_w = 0;
    REP(i, n) {
      cin >> h[i];
      max_h += h[i];
    }
    REP(i, m) {
      cin >> w[i];
      max_w += w[i];
    }

    int wh[max_h + 1];
    int ww[max_w + 1];
    memset(wh, 0, sizeof(wh));
    memset(ww, 0, sizeof(ww));
    REP(i, n) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += h[j];
        wh[sum]++;
      }
    }
    REP(i, m) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += w[j];
        ww[sum]++;
      }
    }

    int cnt = 0;
    REP(i, min(max_h + 1, max_w + 1)) { cnt += wh[i] * ww[i]; }
    cout << cnt << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

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

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n + m == 0)
      break;
    int h[n], w[m];
    REP(i, n) cin >> h[i];
    REP(i, m) cin >> w[i];

    int wh[1500001];
    int ww[1500001];
    REP(i, n) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += h[i];
        wh[sum]++;
      }
    }
    REP(i, m) {
      int sum = 0;
      for (int j = i; j < n; j++) {
        sum += w[i];
        ww[sum]++;
      }
    }

    ll cnt = 0;
    REP(i, 1500001) cnt += wh[i] * ww[i];
    cout << cnt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 1501
#define MAX2 1001

int main() {
  static int h[MAX], w[MAX];
  static int H[MAX * MAX2], W[MAX * MAX2];
  for (int N, M; scanf("%d%d", &N, &M), N;) {
    memset(h, 0, sizeof(h));
    memset(w, 0, sizeof(w));
    memset(H, 0, sizeof(H));
    memset(W, 0, sizeof(W));

    for (int i = 0; i < N; i++) {
      scanf("%d", &h[i]);
    }
    for (int i = 0; i < M; i++) {
      scanf("%d", &w[i]);
    }

    for (int i = 0; i < N; i++) {
      int a = 0;
      for (int j = i; j < N; j++) {
        a += h[j];
        H[a]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int a = 0;
      for (int j = i; j < M; j++) {
        a += w[j];
        W[a]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < MAX * MAX2; i++) {
      ans += H[i] * W[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 1501
#define MAX2 1001

int main() {
  int h[MAX], w[MAX];
  int H[MAX2], W[MAX2];
  for (int N, M; scanf("%d%d", &N, &M), N;) {
    memset(h, 0, sizeof(h));
    memset(w, 0, sizeof(w));
    memset(H, 0, sizeof(H));
    memset(W, 0, sizeof(W));

    for (int i = 0; i < N; i++) {
      scanf("%d", &h[i]);
    }
    for (int i = 0; i < M; i++) {
      scanf("%d", &w[i]);
    }

    for (int i = 0; i < N; i++) {
      int a = 0;
      for (int j = i; j < N; j++) {
        a += h[j];
        H[a]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int a = 0;
      for (int j = i; j < M; j++) {
        a += w[j];
        W[a]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < MAX2; i++) {
      ans += H[i] * W[i];
    }
    cout << ans << 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;
  while (cin >> n >> m, n && m) {
    vector<int> vec;
    map<int, int> mapX, mapY;
    // nの入力
    for (int i = 0; i < n; i++) {
      int hi;
      cin >> hi;
      vec.push_back(hi);
    }
    for (int i = 0; i < n; i++) {
      int h = 0;
      for (int j = i; j < n; j++) {
        h += vec[j];
        mapY[h]++;
      }
    }
    vec.clear();
    // mの入力
    for (int i = 0; i < m; i++) {
      int hi;
      cin >> hi;
      vec.push_back(hi);
    }
    for (int i = 0; i < m; i++) {
      int h = 0;
      for (int j = i; j < m; j++) {
        h += vec[j];
        mapX[h]++;
      }
    }

    // 正方形を数える
    int count = 0;
    map<int, int>::iterator iteX, iteY;
    for (iteY = mapY.begin(); iteY != mapY.end(); iteY++) {
      iteX = mapX.end();
      if (mapX.find(iteY->first) != iteX) {
        count += mapX[iteY->first] * iteY->second;
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, h, w, ans;
  vector<int> hvec, wvec;
  map<int, int> hmap, wmap;
  map<int, int>::iterator hit, wit;

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

    hvec.clear();
    hmap.clear();
    for (int i = 0; i < n; i++) {
      cin >> h;
      hvec.push_back(h);
      int hsum = 0;
      for (int j = i; j > -1; j--) {
        hsum += hvec.at(j);
        hmap[hsum]++;
      }
    }
    wvec.clear();
    wmap.clear();
    for (int i = 0; i < m; i++) {
      cin >> w;
      wvec.push_back(w);
      int wsum = 0;
      for (int j = i; j > -1; j--) {
        wsum += wvec.at(j);
        wmap[wsum]++;
      }
    }

    ans = 0;
    for (hit = hmap.begin(); hit != hmap.end(); hit++) {
      for (wit = wmap.begin(); wit != wmap.end(); wit++) {
        if ((*hit).first == (*wit).first) {
          ans += (*hit).second * (*wit).second;
          break;
        }
      }
    }

    cout << ans << endl;
  }
}