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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int main(void) {
  for (int n, m, res; cin >> n >> m, n; cout << res << endl) {
    res = 0;
    map<int, int> sh;

    vi w(m), h(n);
    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int sum = 0;
      range(j, i, n) {
        sum += h[j];
        sh[sum]++;
      }
    }

    rep(i, m) {
      int sum = 0;
      range(j, i, m) {
        sum += w[j];
        res += sh[sum];
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  for (int n, m, res; cin >> n >> m, n; cout << res << endl) {
    res = 0;

    vi w(m), h(n);
    while (n--)
      cin >> h[n];
    while (m--)
      cin >> w[m];

    int curh, curw;
    rep(i, h.size()) {
      curh = 0;
      for (int j = i; j < h.size(); j++) {
        curh += h[j];
        rep(k, w.size()) {
          curw = 0;
          for (int l = k; l < w.size(); l++) {
            curw += w[l];
            if (curw == curh)
              res++;
          }
        }
      }
    }
  }

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

#include <iostream>
using namespace std;

int main() {
  int i, j, index, n, m, ns[1501], ew[1501], data[1000001], data2[1000001];
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 1; i <= 1000000; i++) {
      data[i] = 0;
      data2[i] = 0;
    }
    for (i = 1; i <= n; i++) {
      cin >> ns[i];
      data[ns[i]]++;
    }
    for (i = 1; i <= m; i++) {
      cin >> ew[i];
      data2[ew[i]]++;
    }

    while (n >= 2) {
      for (i = 1; i < n; i++) {
        index = 0;
        for (j = i; j <= n; j++) {
          index += ns[j];
        }
        data[index]++;
      }
      n--;
    }

    while (m >= 2) {
      for (i = 1; i < m; i++) {
        index = 0;
        for (j = i; j <= m; j++) {
          index += ew[j];
        }
        data2[index]++;
      }
      m--;
    }

    int cnt = 0;
    for (i = 1; i <= 1000000; i++) {
      cnt += data[i] * data2[i];
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int i, j, index, n, m, ns[1501], ew[1501], data[150001], data2[150001];
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 1; i <= 150000; i++) {
      data[i] = 0;
      data2[i] = 0;
    }
    for (i = 1; i <= n; i++) {
      cin >> ns[i];
      data[ns[i]]++;
    }
    for (i = 1; i <= m; i++) {
      cin >> ew[i];
      data2[ew[i]]++;
    }

    while (n >= 2) {
      for (i = 1; i < n; i++) {
        index = 0;
        for (j = i; j <= n; j++) {
          index += ns[j];
        }
        data[index]++;
      }
      n--;
    }

    while (m >= 2) {
      for (i = 1; i < m; i++) {
        index = 0;
        for (j = i; j <= m; j++) {
          index += ew[j];
        }
        data2[index]++;
      }
      m--;
    }

    int cnt = 0;
    for (i = 1; i <= 150000; i++) {
      cnt += data[i] * data2[i];
    }

    cout << cnt << endl;
  }

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

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

#define fin cin

using namespace std;

int n = 0, m = 0;
vector<int> h;
vector<int> w;

template <typename Vec> void push_sum(Vec &v, int size) {
  for (unsigned i = 0; i < size; i++) {
    int num = v[i];

    unsigned sub = i + 1;
    while (sub < size) {
      num += v[sub];
      v.push_back(num);
      sub++;
    }
  }
}

int main() {
  //    ifstream fin("in.txt");

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

    h.clear();
    w.clear();

    h.shrink_to_fit();
    w.shrink_to_fit();

    int tmp = 0;
    for (int i = 0; i < n; i++) {
      fin >> tmp;
      h.push_back(tmp);
    }
    for (int i = 0; i < m; i++) {
      fin >> tmp;
      w.push_back(tmp);
    }

    push_sum(h, n);
    push_sum(w, m);

    int ans = 0;
    short h_val[1000 * 1500 + 1] = {};
    short w_val[1000 * 1500 + 1] = {};

    // sort(h.begin(), h.end());
    // sort(w.begin(), w.end());

    for (unsigned i = 0; i < h.size(); i++) {
      h_val[(h[i])]++;
    }
    for (unsigned i = 0; i < w.size(); i++) {
      w_val[(w[i])]++;
    }
    for (int i = 0; i < 1000 * 1500 + 1; i++) {
      ans += (h_val[i] * w_val[i]);
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define fin cin

using namespace std;

int n = 0, m = 0;
vector<int> h;
vector<int> w;

template <typename Vec> void push_sum(Vec &v, int size) {
  for (unsigned i = 0; i < size; i++) {
    int num = v[i];

    unsigned sub = i + 1;
    while (sub < size) {
      num += v[sub];
      v.push_back(num);
      sub++;
    }
  }
}

int main() {
  //    ifstream fin("in.txt");

  while (fin >> n >> m, n | m) {
    h.clear();
    w.clear();

    h.shrink_to_fit();
    w.shrink_to_fit();

    int tmp = 0;
    for (int i = 0; i < n; i++) {
      fin >> tmp;
      h.push_back(tmp);
    }
    for (int i = 0; i < m; i++) {
      fin >> tmp;
      w.push_back(tmp);
    }

    push_sum(h, n);
    push_sum(w, m);

    int ans = 0;
    int w_val[1024] = {};
    int h_val[1024] = {};

    // sort(h.begin(), h.end());
    // sort(w.begin(), w.end());

    for (auto itr = h.begin(); itr != h.end(); itr++) {
      h_val[*itr]++;
    }
    for (auto itr = w.begin(); itr != w.end(); itr++) {
      w_val[*itr]++;
    }

    for (int i = 0; i < 1001; i++)
      ans += h_val[i] * w_val[i];

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

#include <iostream>
using namespace std;

int h[1501], w[1501];
int n, m, sum, line_length, a;
int ish[1500001], isw[1500001];

int solve(int n, int m) {
  for (int i = 1; i <= 1500000; i++) {
    ish[i] = 0;
    isw[i] = 0;
  }
  sum = 0;

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

  for (int i = 1; i <= 1500000; i++) {
    sum += ish[i] * isw[i];
  }

  return sum;
}

int main() {
  h[0] = 0;
  w[0] = 0;
  while (cin >> n >> m && n > 0) {
    for (int i = 1; i <= n; i++) {
      cin >> a;
      h[i] = h[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      cin >> a;
      w[i] = w[i - 1] + a;
    }
    cout << solve(n, m) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int h[1500], w[1500];
int n, m, sum, line_length, a;

void count(int n, int m) {
  if (n > 0) {
    for (int i = n - 1; i >= 0; i--) {
      line_length = h[n] - h[i];
      for (int j = 0; j <= m; j++) {
        for (int k = j + 1; k <= m; k++) {
          if ((w[k] - w[j]) == line_length) {
            sum += 1;
          }
        }
      }
    }
    count(n - 1, m);
  }
}

int solve(int n, int m) {
  sum = 0;
  count(n, m);
  return sum;
}

int main() {
  h[0] = 0;
  w[0] = 0;
  while (cin >> n >> m && n > 0) {
    for (int i = 1; i <= n; i++) {
      cin >> a;
      h[i] = h[i - 1] + a;
    }
    for (int i = 1; i <= m; i++) {
      cin >> a;
      w[i] = w[i - 1] + a;
    }
    cout << solve(n, m) << endl;
  }

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int h[1501], w[1501];

  int N, M;
  while (cin >> N >> M, (N || M)) {
    for (int i = 0; i < N; i++) {
      int t;
      cin >> t;
      h[i] = t;
    }
    for (int i = 0; i < M; i++) {
      int t;
      cin >> t;
      w[i] = t;
    }

    vector<int> V(1500001), H(1500001);
    for (int i = 0; i < N; i++) {
      int len = 0;
      for (int j = i; j < N; j++) {
        len += h[j];
        V[len]++;
      }
    }
    for (int i = 0; i < M; i++) {
      int len = 0;
      for (int j = i; j < M; j++) {
        len += w[j];
        H[len]++;
      }
    }

    int count = 0;
    for (int i = 1; i <= 1500000; i++) {
      count += V[i] * H[i];
    }
    cout << count << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
using namespace std;
const int MAX_LEN = 10000000;
int N, M;
int h[MAX_LEN], w[MAX_LEN];
int x, y;
int solve() {
  int sum = 0;
  int k = N;
  int l = M;
  for (int i = 0; i < max(N, M); i++) {
    if (i < N)
      h[k] = h[i];
    if (i < M)
      w[l] = w[i];
    for (int j = i + 1; j < max(N, M); j++) {
      if (i < N) {
        h[k] += h[j];
        h[k + 1] = h[k];
        k++;
      }
      if (i < M) {
        w[l] += w[j];
        w[l + 1] = w[l];
        l++;
      }
    }
  }
  sort(h, h + k);
  sort(w, w + l);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < l && j <= i; j++)
      if (w[j] == h[i])
        sum++;
  return sum;
}

int max(int a, int b) { return a > b ? a : b; }
int main() {
  while (cin >> N >> M && N != 0) {
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
    }
    cout << solve() << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    vi h(n + 1);
    vi w(m + 1);
    int totalh = 0, totalw = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      totalh += h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
      totalw += w[i];
    }
    vi htype(totalh + 1);
    vi wtype(totalw + 1);
    for (int i = 0; i < n; i++) {
      int height = h[i];
      htype[height]++;
      for (int j = i + 1; j < n; j++) {
        height += h[j];
        htype[height]++;
      }
    }
    for (int i = 0; i < m; i++) {
      int width = w[i];
      wtype[width]++;
      for (int j = i + 1; j < m; j++) {
        width += w[j];
        wtype[width]++;
      }
    }
    for (int i = 1; i <= min(totalh, totalw); i++) {
      count += htype[i] * wtype[i];
    }
    cout << count << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
vi ans, ans2;
const int inf = 1e9;
int polnum[205];
int dp[1000005];
int odddp[1000005];
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    int tmph, tmpw;
    vi h(n + 1);
    vi w(m + 1);
    h[0] = 0;
    w[0] = 0;
    int count = 0;
    int lw, rw, uh, dh;
    for (int i = 1; i <= n; i++) {
      cin >> tmph;
      h[i] += h[i - 1] + tmph;
    }
    for (int j = 1; j <= m; j++) {
      cin >> tmpw;
      w[j] += w[j - 1] + tmpw;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        uh = h[i];
        dh = h[j];
        for (int k = 0; k <= m; k++) {
          for (int l = k + 1; l <= m; l++) {
            lw = w[k];
            rw = w[l];
            if (dh - uh == rw - lw)
              count++;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

int w[1501] = {0}, h[1501] = {0};
int W[2250000] = {0}, H[2250000] = {0};

int sum(int *a, int i, int n, int size) {
  int s = 0;
  for (int j = n; j--;) {
    if (i >= size) {
      return 0;
    }
    s += a[i++];
  }
  return s;
}

int main() {
  int n, m, w_, h_;
  while (cin >> n >> m, n || m) {
    // int cnt_h[1001] = {0}
    int cnt_w[1000000] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int i_ = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = 0; i + j <= n; i++) {
        H[i_++] = sum(h, i, j, n);
      }
    }
    /*for(int i=0 ; i<i_ ; i++ ){
            cnt_h[ H[i] ]++;
    }*/

    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int j_ = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 0; i + j <= m; i++) {
        W[j_++] = sum(w, i, j, m);
      }
    }
    for (int i = 0; i < j_; i++) {
      cnt_w[W[i]] += 1;
    }
    /*for(int i=0 ; i<j_ ; i++ ){
            cout << "W[" << i << "] : " << W[i] << endl;
    }*/

    int ans = 0;
    for (int i = 0; i < i_; i++) {
      // if( H[i] <= 1000 && H[i] >= 0 )
      ans += cnt_w[H[i]];
      // for(int j=0 ; j < j_ ; j++ ){
      //	if( W[j] == H[i] ) ans++;
      // }
    }
    // cout << "ans : " << ans << endl;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int w[1501] = {0}, h[1501] = {0};
int W[2250000] = {0}, H[2250000] = {0};

int sum(int *a, int i, int n, int size) {
  int s = 0;
  for (int j = n; j--;) {
    if (i >= size) {
      return 0;
    }
    s += a[i++];
  }
  return s;
}

int main() {
  int n, m, w_, h_;
  while (cin >> n >> m, n || m) {
    // int cnt_h[1001] = {0}
    int cnt_w[100000] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int i_ = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = 0; i + j <= n; i++) {
        H[i_++] = sum(h, i, j, n);
      }
    }
    /*for(int i=0 ; i<i_ ; i++ ){
            cnt_h[ H[i] ]++;
    }*/

    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }
    int j_ = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 0; i + j <= m; i++) {
        W[j_++] = sum(w, i, j, m);
      }
    }
    for (int i = 0; i < j_; i++) {
      cnt_w[W[i]] += 1;
    }
    /*for(int i=0 ; i<j_ ; i++ ){
            cout << "W[" << i << "] : " << W[i] << endl;
    }*/

    int ans = 0;
    for (int i = 0; i < i_; i++) {
      // if( H[i] <= 1000 && H[i] >= 0 )
      ans += cnt_w[H[i]];
      // for(int j=0 ; j < j_ ; j++ ){
      //	if( W[j] == H[i] ) ans++;
      // }
    }
    // cout << "ans : " << ans << endl;
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#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 allh[1500010], allw[1500010];
int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int h[1501], w[1501];
    REP(i, 0, 1500010) allh[i] = allw[i] = 0;
    int ans = 0, k;
    REP(i, 0, N) {
      cin >> h[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    REP(i, 0, M) {
      cin >> w[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    REP(i, 0, 1500010) { ans += allh[i] * allw[i]; }
    p(ans);
  }

  return 0;
}

Error: MLE

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], w[1501];
    map<int, int> allh, allw;
    int ans = 0, k;
    REP(i, 0, N) {
      cin >> h[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += h[j];
        allh[k]++;
      }
    }
    REP(i, 0, M) {
      cin >> w[i];
      k = 0;
      for (int j = i; j >= 0; j--) {
        k += w[j];
        allw[k]++;
      }
    }
    REP(i, 0, 1500000) { ans += allh[i] * allw[i]; }
    p(ans);
  }

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

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

using namespace std;

int main() {

  int N, M;
  int a;

  while (1) {
    vector<int> v1;
    vector<int> v2;
    vector<int> vh;
    vector<int> vw;
    int count = 0;
    int k = 0;
    int check = 0;
    int l = 0;

    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      cin >> a;
      v1.push_back(a);
    }

    for (int i = 0; i < M; ++i) {
      cin >> a;
      v2.push_back(a);
    }

    for (int i = 0; i < N; ++i) {
      int temp1 = 0;

      for (int j = i; j < N; ++j) {
        temp1 += v1[j];
        vh.push_back(temp1);
      }
    }

    for (int i = 0; i < M; ++i) {
      int temp2 = 0;

      for (int j = i; j < M; ++j) {
        temp2 += v2[j];
        vw.push_back(temp2);
        ++l;
      }
    }

    sort(vh.begin(), vh.end());
    sort(vw.begin(), vw.end());

    for (auto e : vh) {
      while (1) {
        if (k == l) {
          break;
        }
        if (e == vw[k]) {
          ++count;
        } else if (e < vw[k]) {
          break;
        } else {
          check = k + 1;
        }
        ++k;
      }
      k = check;
    }
    cout << count << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int main() {

  int N, M;
  int a;

  while (1) {
    vector<int> v1;
    vector<int> v2;
    vector<int> vh;
    vector<int> vw;
    int count = 0;
    int k = 0;
    int check = 0;
    int l = 0;

    cin >> N >> M;

    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      cin >> a;
      v1.push_back(a);
    }

    for (int i = 0; i < M; ++i) {
      cin >> a;
      v2.push_back(a);
    }

    for (int i = 0; i < N; ++i) {
      int temp1 = 0;

      for (int j = i; j < N; ++j) {
        temp1 += v1[j];
        vh.push_back(temp1);
      }
    }

    for (int i = 0; i < M; ++i) {
      int temp2 = 0;

      for (int j = i; j < M; ++j) {
        temp2 += v2[j];
        vw.push_back(temp2);
        ++l;
      }
    }

    sort(vh.begin(), vh.end());
    sort(vw.begin(), vw.end());

    for (auto e : vh) {
      while (1) {
        if (e == vw[k]) {
          ++count;
        } else if (e < vw[k]) {
          break;
        } else {
          check = k + 1;
        }

        ++k;

        if (k == l) {
          break;
        }
      }
      k = check;
    }
    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;
  while (cin >> n >> m && n > 0) {
    vector<int> h(n);
    vector<int> w(m);
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    int ans = 0;
    int x, y;
    // for(int i = 0; i < n; i++){
    // for(int j = i; j < n; j++){
    // 	// ?§??????¨??????????±???????
    // 	x = 0;
    // 	for(int k = i; k <= j; k++){
    // 		x += h[k];
    // 	}

    // 	for(int l = 0; l < m; l++){
    // 		y = 0;
    // 		for(int p = l; p < m; p++){
    // 			y += w[p];
    // 			if(y == x){
    // 				ans++;
    // 				break;
    // 			}
    // 		}
    // 	}
    // }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int p = i;
        int q = j;
        x = 0;
        y = 0;
        while (true) {
          if (x >= y) {
            y += w[q];
            q++;
            if (q > m)
              break;
          } else {
            x += h[p];
            p++;
            if (p > n)
              break;
          }
          // cout << i << " " << p << " " << j << " " << q << " " << endl;
          // cout << x << " " << y << endl;
          if (x == y) {
            // cout << "!!!" << endl;
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m && n > 0) {
    vector<int> h(n);
    vector<int> w(m);
    for (int i = 0; i < n; i++)
      cin >> h[i];
    for (int i = 0; i < m; i++)
      cin >> w[i];
    int ans = 0;
    int x, y;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        // ?§??????¨??????????±???????
        x = 0;
        for (int k = i; k <= j; k++) {
          x += h[k];
        }

        for (int l = 0; l < m; l++) {
          y = 0;
          for (int p = l; p < m; p++) {
            y += w[p];
            if (y == x) {
              ans++;
              break;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}