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

#include <iostream>

using namespace std;
int N, M;
int h[1501], w[1501];
int e;
int ans = 0;

int x[1500000];

int main() {
  while (1) {
    cin >> N;
    cin >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < 1500000; i++) {
      x[i] = 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];
      x[w[i]] = 1;
    }
    h[0] = 0;
    w[0] = 0;
    ans = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = i + 1; k <= N; k++) {
          int edgh = h[k] - h[i];
          if (x[w[j] + edgh]) {
            // cout<<"h["<<j<<"]:"<<h[j]<<" edgh:"<<edgh<<endl;
            ans++;
          }
          // for(int l=j+1; l<=M; l++){
          // 	if(edgh==w[l]-w[j]){
          // 		cout<<"h["<<j<<"]:"<<h[j]<<" edgh:"<<edgh<<endl;
          // 		ans++;
          // 	}
          // 	if(edgh<=w[l]-w[j])break;
          // }
        }
      }
    }
    cout << ans << endl;

    // for(int i=0; i<1500000; i++){
    // 	if(y[i])cout<<"i "<<i<<endl;
    // }
    // cout<<endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int N, M;
int h[1501], w[1501];
int e;
int ans = 0;

int main() {
  while (1) {
    cin >> N;
    cin >> M;
    if (N == 0 && M == 0)
      break;
    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];
    }
    h[0] = 0;
    w[0] = 0;
    ans = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = i + 1; k <= N; k++) {
          int edgh = h[k] - h[i];
          for (int l = j + 1; l <= M; l++) {
            if (edgh == w[l] - w[j])
              ans++;
            if (edgh <= w[l] - w[j])
              break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int N, M;
  vector<int> HH, WW;
  while (cin >> N >> M, N | M) {
    int H[N + 1];
    H[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    HH.clear();
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        HH.push_back(H[j] - H[i]);
      }
    }
    int W[M + 1];
    W[0] = 0;
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    WW.clear();
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        WW.push_back(W[j] - W[i]);
      }
    }
    sort(HH.begin(), HH.end());
    sort(WW.begin(), WW.end());
    int c = 0;
    for (auto &h : HH) {
      auto lo = lower_bound(WW.begin(), WW.end(), h);
      auto up = upper_bound(WW.begin(), WW.end(), h);
      for (auto i = lo; i != up; ++i) {
        if (h == *i)
          c++;
      }
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N, M;
  vector<int> HH, WW;
  while (cin >> N >> M, N | M) {
    int H[N + 1];
    H[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> H[i];
      H[i] += H[i - 1];
    }
    HH.clear();
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        HH.push_back(H[j] - H[i]);
      }
    }
    int W[M + 1];
    W[0] = 0;
    for (int i = 1; i <= M; i++) {
      cin >> W[i];
      W[i] += W[i - 1];
    }
    WW.clear();
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        WW.push_back(W[j] - W[i]);
      }
    }
    int c = 0;
    for (auto &h : HH) {
      for (auto &w : WW) {
        if (h == w)
          c++;
      }
    }
    cout << c << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int MAX = 1500000;
int check(vector<int> h, vector<int> w, int x, int y, int b) {
  if (h[y] && w[x] && h[y + b] && w[x + b])
    return 1;
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h(n + 1, 0);
    vector<int> w(m + 1, 0);
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      h[i] = h[i - 1] + tmp;
    }
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      w[i] = w[i - 1] + tmp;
    }
    map<int, int> cnt;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        cnt[h[i] - w[j]]++;
      }
    }
    int ans = 0;
    map<int, int>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      ans += (*it).second * ((*it).second - 1) / 2;
      it++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int MAX = 1500000;
int check(vector<int> h, vector<int> w, int x, int y, int b) {
  if (h[y] && w[x] && h[y + b] && w[x + b])
    return 1;
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> h(n + 1, 0);
    vector<int> w(n + 1, 0);
    h[0] = 0;
    w[0] = 0;
    for (int i = 1; i <= n; i++) {
      int tmp;
      cin >> tmp;
      h[i] = h[i - 1] + tmp;
    }
    for (int i = 1; i <= m; i++) {
      int tmp;
      cin >> tmp;
      w[i] = w[i - 1] + tmp;
    }
    map<int, int> cnt;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        cnt[h[i] - w[j]]++;
      }
    }
    int ans = 0;
    map<int, int>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      ans += (*it).second * ((*it).second - 1) / 2;
      it++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define MAX 1000000
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  // int x[MAX]={0};
  while ((cin >> N >> M) && N) {
    int tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }
    map<int, int> x;
    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  int x[MAX] = {0};
  while ((cin >> N >> M) && N) {
    long long tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }
    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M, h[1500], w[1500];

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

    map<int, int> foo, piyo;
    for (int i = 0; i < N; i++) {
      int sum = 0;
      for (int j = i; j < N; j++) {
        sum += h[j];
        foo[sum]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int sum = 0;
      for (int j = i; j < M; j++) {
        sum += w[j];
        piyo[sum]++;
      }
    }

    int cnt = 0;
    for (map<int, int>::iterator it = foo.begin(); it != foo.end(); it++) {
      cnt += it->second * piyo[it->first];
    }
    cout << cnt << endl;
  }
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  int N, M;
  vector<int> w, h;
  int p = 0;
  int x[MAX] = {MAX};
  while ((cin >> N) && (cin >> M)) {
    int tt = 0;
    w.resize(M);
    h.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }

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

    for (int i = 0; i < N; i++) {
      p = 0;
      for (int j = i; j < N; j++) {
        p += h[j];
        x[p]++;
      }
    }
    for (int i = 0; i < M; i++) {
      p = 0;
      for (int j = i; j < M; j++) {
        p += w[j];
        tt += x[p];
      }
    }
    cout << tt << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[1500001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {
  int sw[150001], sum, n, m, i, j, k, count, w[1500], h[1500];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 0; i < m; i++)
      scanf("%d", &w[i]);
    memset(sw, 0, sizeof(sw));
    for (i = 0; i < m; i++) {
      for (j = i; j < m; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += w[k];
        sw[sum]++;
      }
    }
    count = 0;
    for (i = 0; i < n; i++) {
      for (j = i; j < n; j++) {
        sum = 0;
        for (k = i; k <= j; k++)
          sum += h[k];
        if (sw[sum])
          count += sw[sum];
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {

  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M))
      break;

    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 p[1000 * 1500 + 1] = {};
    for (int i = 0; i < N; i++) {
      int s = 0;
      for (int j = i; j < N; j++) {
        s += h[j];
        p[s]++;
      }
    }
    int q[1000 * 1500 + 1] = {};
    for (int i = 0; i < M; i++) {
      int s = 0;
      for (int j = i; j < M; j++) {
        s += w[j];
        q[s]++;
      }
    }

    int s = 0;
    for (int i = 0; i < 1000 * 1500 + 1; i++) {
      s += p[i] * q[i];
    }

    cout << s << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M))
      break;

    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 x[M + 1], y[N + 1];
    x[0] = y[0] = 0;
    for (int i = 0; i < M; i++) {
      x[i + 1] = x[i] + w[i];
    }
    for (int i = 0; i < N; i++) {
      y[i + 1] = y[i] + h[i];
    }
    int s = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        int d = i + 1;
        int r = j + 1;
        while (d <= N && r <= M) {
          if (y[d] - y[i] == x[r] - x[j]) {
            s++;
            d++;
            r++;
          } else if (y[d] - y[i] > x[r] - x[j]) {
            r++;
          } else if (y[d] - y[i] < x[r] - x[j]) {
            d++;
          }
        }
      }
    }

    cout << s << endl;
  }

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

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

int main() {
  int n, m;
  int w[1500], h[1500];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    scanf("%d", &w[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &h[i]);
  while (n) {
    vector<int> x(1501 * 1001);
    vector<int> y(1501 * 1001);
    for (int i = 0; i < n; i++) {
      int a = 0;
      for (int j = i; j < n; j++) {
        a += w[j];
        x[a]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int b = 0;
      for (int j = i; j < m; j++) {
        b += h[j];
        y[b]++;
      }
    }

    int ave = 0;
    for (int i = 0; i < 1500 * 1000; i++) {
      // if(y[i]!=0)cout << i << " " << y[i] << " / ";
      ave += x[i] * y[i];
    }
    cout << ave << endl;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      scanf("%d", &h[i]);
  }
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  int n, m;
  int w[1500], h[1500];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    scanf("%d", &w[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &h[i]);
  while (n) {
    vector<int> x;
    vector<int> y;
    for (int i = 0; i < n; i++) {
      int a = 0;
      for (int j = i; j < n; j++) {
        a += w[j];
        x[a]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int b = 0;
      for (int j = i; j < m; j++) {
        b += h[j];
        y[b]++;
      }
    }

    int ave = 0;
    for (int i = 0; i < 1500 * 1000; i++) {
      // if(y[i]!=0)cout << i << " " << y[i] << " / ";
      ave += x[i] * y[i];
    }
    cout << ave << endl;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
      scanf("%d", &w[i]);
    for (int i = 0; i < m; i++)
      scanf("%d", &h[i]);
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

#define N 1500

static int my[N * N];

using namespace std;

/** Application main entry point. */
int main(int argc, char *argv[]) {
  int i;

  for (;;) {
    int px[N + 1], py[N + 1];
    /** Allocated in Heap Memory Area:
     * int my[ N * N ]; */
    int x1, x2, y1, y2;
    int res = 0;
    int n, m;

    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;

    px[0] = 0;
    py[0] = 0;
    for (i = 1; i <= n; ++i) {
      scanf("%d", px + i);
      px[i] += px[i - 1];
    }
    for (i = 1; i <= m; ++i) {
      scanf("%d", py + i);
      py[i] += py[i - 1];
    }

    i = 0;
    for (y1 = 0; y1 <= m; ++y1)
      for (y2 = y1 + 1; y2 <= m; ++y2) {
        my[i++] = py[y2] - py[y1];
      }
    sort(my, my + i);

    for (x1 = 0; x1 <= n; ++x1)
      for (x2 = x1 + 1; x2 <= n; ++x2) {
        const int dx = px[x2] - px[x1];

        res += upper_bound(my, my + i, dx) - lower_bound(my, my + i, dx);
      }

    printf("%d\\n", res);
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define N 1500

static int my[N + 1][N + 1];

using namespace std;

/** Application main entry point. */
int main(int argc, char *argv[]) {
  int i;

  for (;;) {
    int px[N + 1], py[N + 1];
    /** Allocated in Heap Memory Area:
     * int my[ N + 1 ][ N + 1 ]; */
    int x1, x2, y1, y2;
    int res = 0;
    int n, m;

    scanf("%d%d", &n, &m);
    if (!(n | m))
      break;

    px[0] = 0;
    py[0] = 0;
    for (i = 1; i <= n; ++i) {
      scanf("%d", px + i);
      px[i] += px[i - 1];
    }
    for (i = 1; i <= m; ++i) {
      scanf("%d", py + i);
      py[i] += py[i - 1];
    }

    for (y1 = 0; y1 <= m; ++y1)
      for (y2 = y1 + 1; y2 <= m; ++y2) {
        my[y1][y2] = py[y2] - py[y1];
      }
    for (x1 = 0; x1 <= n; ++x1)
      for (x2 = x1 + 1; x2 <= n; ++x2) {
        const int dx = px[x2] - px[x1];

        for (y1 = 0; y1 <= m; ++y1) {
          res += !!(binary_search(&my[y1][y1 + 1], &my[y1][m + 1], dx));
        }
      }

    printf("%d\\n", res);
  }

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

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

#define LIM 2000000

using namespace std;

int main() {

  int n, m, h[2000], w[2000];

  while (cin >> n >> m, (n || m)) {

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

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

    int res = 0;
    vector<int> hlen, wlen;

    for (int p = 0; p < n; ++p) {

      int hsum = 0;

      for (int q = p; q < n; ++q) {

        hsum += h[q];
        hlen.push_back(hsum);
      }
    }

    for (int r = 0; r < m; ++r) {

      int wsum = 0;

      for (int s = r; s < m; ++s) {

        wsum += w[s];
        wlen.push_back(wsum);
      }
    }

    sort(hlen.begin(), hlen.end());
    sort(wlen.begin(), wlen.end());

    for (int i = 0; i < hlen.size(); ++i) {

      vector<int>::iterator it1 =
                                lower_bound(wlen.begin(), wlen.end(), hlen[i]),
                            it2 =
                                upper_bound(wlen.begin(), wlen.end(), hlen[i]);

      res += it2 - it1;
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define LIM 2000000

using namespace std;

int main() {

  int n, m, h[2000], w[2000];

  while (cin >> n >> m, (n || 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 p = 0; p < n; ++p) {

      int hsum = 0;

      for (int q = p; q < n; ++q) {

        hsum += h[q];

        for (int r = 0; r < m; ++r) {

          int wsum = 0;

          for (int s = r; s < m; ++s) {

            wsum += w[s];

            if (hsum == wsum)
              res++;
          }
        }
      }
    }

    cout << res << endl;
  }

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

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[10600] = {};
    int num2[10600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int num[2500100] = {};
    int takasa[1600] = {};
    short num2[1600] = {};
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
      int a;
      cin >> a;
      takasa[i] = takasa[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      int a;
      cin >> a;
      num2[i] = num2[i - 1] + a;
      for (int j = 0; j < i; j++) {
        num[num2[i] - num2[j]]++;
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        ans += num[takasa[i] - takasa[j]];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1, 0);
    vector<int> w(m + 1, 0);
    for (int i = 1; i <= n; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
      //            cout << h[i] << " ";
    }
    //        cout << endl;
    for (int i = 1; i <= m; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
      //            cout << w[i] << " ";
    }
    //        cout << endl;
    int ans = 0;
    map<int, int> hlen, wlen;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        int t = h[j] - h[i];
        hlen[t]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j <= m; j++) {
        int t = w[j] - w[i];
        wlen[t]++;
      }
    }
    map<int, int>::iterator hitr, witr;
    for (hitr = hlen.begin(); hitr != hlen.end(); ++hitr) {
      int len = hitr->first, hval = hitr->second;
      witr = wlen.find(len);
      if (witr != wlen.end()) {
        int wval = witr->second;
        ans += hval * wval;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> h(n + 1, 0);
    vector<int> w(m + 1, 0);
    for (int i = 1; i <= n; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
      //            cout << h[i] << " ";
    }
    //        cout << endl;
    for (int i = 1; i <= m; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
      //            cout << w[i] << " ";
    }
    //        cout << endl;
    int ans = 0;
    for (int i = 1; i <= h[n]; i++) {
      int hnum = 0, wnum = 0;
      for (int j = n; j > 0; j--) {
        /*
                        cout << "*" << i << " ";
                        cout << *lower_bound(h.begin(),h.end(),h[j]-i);
        */
        if (*lower_bound(h.begin(), h.end(), h[j] - i) == h[j] - i) {
          hnum++;
          //                   cout << "+";
        }
        //                else cout << "-";
      }
      //            cout << endl;
      for (int j = m; j > 0; j--) {
        /*
                        cout << "*" << i << " ";
                        cout << *lower_bound(w.begin(),w.end(),w[j]-i);
        */
        if (*lower_bound(w.begin(), w.end(), w[j] - i) == w[j] - i) {
          wnum++;
          //                    cout << "+";
        }
        //                else cout << "-";
      }
      //            cout << endl;
      ans += hnum * wnum;
    }
    cout << ans << endl;
  }
  return 0;
}