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

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

int hh[1000 * 1500 + 1];
int ww[1000 * 1500 + 1];

int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vector<int> h(n);
    vector<int> w(m);
    memset(hh, 0, sizeof(hh));
    memset(ww, 0, sizeof(ww));
    int _max = 0;
    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];
        hh[sum]++;
        _max = max(_max, sum);
      }
    }

    for (int i = 0; i < m; i++) {
      int sum = 0;
      for (int j = i; j < m; j++) {
        sum += w[j];
        ww[sum]++;
        _max = max(_max, sum);
      }
    }

    int sum = 0;
    for (int i = 0; i <= _max; i++) {
      if (ww[i] != 0 && hh[i] != 0) {
        sum += (hh[i] * ww[i]);
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    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 count = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int hh = i;
        int ww = j;
        int h_sum = 0;
        int w_sum = 0;
        count--;
        while (hh < n && ww < m) {
          while (hh < n && ww < m && h_sum != w_sum) {
            while (h_sum == 0 || (hh < n && h_sum < w_sum)) {
              h_sum += h[hh++];
            }
            while (w_sum == 0 || (ww < m && h_sum > w_sum)) {
              w_sum += w[ww++];
            }
          }
          if (h_sum == w_sum) {
            count++;
            if (hh < n)
              h_sum += h[hh++];
          }
        }
        if (h_sum != w_sum) {
          while (hh >= n && ww < m && h_sum > w_sum)
            w_sum += w[ww++];
          while (ww >= m && hh < n && h_sum < w_sum)
            h_sum += h[hh++];
          if (h_sum == w_sum)
            count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
vector<int> h, w;
int mp[1500001] = {};
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {

    int ans = 0;
    h.push_back(0);
    w.push_back(0);
    for (int i = 0; i < n; i++)
      h.push_back(in());
    for (int i = 0; i < m; i++)
      w.push_back(in());
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int l = 1; l <= m; l++)
      w[l] += w[l - 1];
    for (int i = 0; i < n; i++) {
      for (int l = i + 1; l <= n; l++) {
        mp[h[l] - h[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int l = i + 1; l <= m; l++) {
        if (mp[w[l] - w[i]] != 0) {
          ans += mp[w[l] - w[i]];
          // cout<<i<<" "<<l<<endl;;
        }
      }
    }
    h.clear();
    w.clear();
    for (int i = 0; i < 1500001; i++)
      mp[i] = 0;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> h, w;
    int mp[2000] = {};
    int ans = 0;
    h.push_back(0);
    w.push_back(0);
    for (int i = 0; i < n; i++)
      h.push_back(in());
    for (int i = 0; i < m; i++)
      w.push_back(in());
    for (int i = 1; i <= n; i++)
      h[i] += h[i - 1];
    for (int l = 1; l <= m; l++)
      w[l] += w[l - 1];
    for (int i = 0; i < n; i++) {
      for (int l = i + 1; l <= n; l++) {
        mp[h[l] - h[i]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      for (int l = i + 1; l <= m; l++) {
        if (mp[w[l] - w[i]] != 0) {
          ans += mp[w[l] - w[i]];
          // cout<<i<<" "<<l<<endl;;
        }
      }
    }

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

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

int main(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vi v(n + 1, 0), h(m + 1, 0);
    rep(i, n) cin >> v[i + 1];
    rep(i, m) cin >> h[i + 1];
    partial_sum(all(v), v.begin());
    partial_sum(all(h), h.begin());
    map<int, int> v2, h2;
    rep(i, n + 1) range(j, i + 1, n + 1) {
      if (v2.find(v[j] - v[i]) == v2.end())
        v2[v[j] - v[i]] = 0;
      v2[v[j] - v[i]]++;
    }
    rep(i, m + 1) range(j, i + 1, m + 1) {
      if (h2.find(h[j] - h[i]) == h2.end())
        h2[h[j] - h[i]] = 0;
      h2[h[j] - h[i]]++;
    }
    ll ans = 0;
    for (auto i : v2) {
      if (h2.find(i.first) != h2.end()) {
        ans += i.second * h2[i.first];
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

int main(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vi v(n + 1, 0), h(m + 1, 0);
    rep(i, n) cin >> v[i + 1];
    rep(i, m) cin >> h[i + 1];
    partial_sum(all(v), v.begin());
    partial_sum(all(h), h.begin());
    multiset<int> h2;
    rep(i, m + 1) range(j, i + 1, m + 1) h2.insert(h[j] - h[i]);
    ll ans = 0;
    rep(i, n + 1) range(j, i + 1, n + 1) ans += h2.count(v[j] - v[i]);
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int nm[1501], mm[1501];
int nc[1500001] = {};
int c = 0;
int k = 0, l = 0;
void lsnn(int);
void lsnm(int);
int main() {
  int n, m;
  int np, mp;
  while (1) {
    // syoki
    for (int i = 0; i < 1501; i++) {
      nm[i] = mm[i] = 0;
    }
    for (int i = 0; i < 1500001; i++) {
      for (int j = 0; j < 2; j++) {
        nc[i] = 0;
      }
    }
    c = 0;
    k = 0;
    l = 0;
    //
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> nm[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> mm[i];
    }
    // n
    for (int i = 0; i < n; i++) {
      lsnn(nm[i]);
      np = nm[i];
      if (i != n - 1) {
        for (int j = i + 1; j < n; j++) {
          np += nm[j];
          lsnn(np);
        }
      }
    }
    // m
    for (int i = 0; i < m; i++) {
      mp = mm[i];
      if (nc[mp] != 0)
        c += nc[mp];
      if (i != m - 1) {
        for (int j = i + 1; j < m; j++) {
          mp += mm[j];
          if (nc[mp] != 0)
            c += nc[mp];
        }
      }
    }
    // out
    cout << c << endl;
  }
  return 0;
}
///////////////////////////////////////////
void lsnn(int n) { nc[n]++; }

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int nm[1501], mm[1501];
int nc[1125750][2] = {};
int mc[1125750][2] = {};
int c = 0;
int k = 0, l = 0;
void lsnn(int);
void lsnm(int);
int main() {
  int n, m;
  int np, mp;
  while (1) {
    // syoki
    for (int i = 0; i < 1501; i++) {
      nm[i] = mm[i] = 0;
    }
    for (int i = 0; i < 1125750; i++) {
      for (int j = 0; j < 2; j++) {
        nc[i][j] = mc[i][j] = 0;
      }
    }
    c = 0;
    k = 0;
    l = 0;
    //
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> nm[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> mm[i];
    }
    // n
    for (int i = 0; i < n; i++) {
      lsnn(nm[i]);
      np = nm[i];
      if (i != n - 1) {
        for (int j = i + 1; j < n; j++) {
          np += nm[j];
          lsnn(np);
        }
      }
    }
    // m
    for (int i = 0; i < m; i++) {
      lsnm(mm[i]);
      mp = mm[i];
      if (i != m - 1) {
        for (int j = i + 1; j < m; j++) {
          mp += mm[j];
          lsnm(mp);
        }
      }
    }
    // look ans
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < l; j++) {
        if (nc[i][0] == mc[j][0]) {
          // debug
          //	cout << nc[i][1] <<" "<< mc[j][1] <<" "<<nc[i][0]<< " " <<
          //mc[j][0]<<" "<<i<<" "<<j<< endl;
          //
          c += nc[i][1] * mc[j][1];
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}
///////////////////////////////////////////
void lsnn(int n) {
  for (int i = 0; i < k; i++) {
    if (nc[i][0] == n) {
      nc[i][1]++;
      return;
    }
  }
  nc[k][0] = n;
  nc[k][1]++;
  k++;
}
//////////////////////////////////////////
void lsnm(int m) {
  for (int i = 0; i < l; i++) {
    if (mc[i][0] == m) {
      mc[i][1]++;
      return;
    }
  }
  mc[l][0] = m;
  mc[l][1]++;
  l++;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define MAX_RANGE 1500
#define MAX_VALUE 1000
using namespace std;

int h[MAX_RANGE], w[MAX_RANGE];

int htype[MAX_VALUE * MAX_RANGE + 1], wtype[MAX_VALUE * MAX_RANGE + 1];

void calcAns(int hsum, int wsum) {
  int ans = 0;
  for (int i = 1; i <= min(hsum, wsum); i++)
    ans += htype[i] * wtype[i];
  cout << ans << endl;
}

void calcTypeNumber(int n, int m) {
  for (int i = 0; i < n; i++) {
    int temp = h[i];
    htype[temp]++;
    for (int j = i + 1; j < n; j++) {
      temp += h[j];
      htype[temp]++;
    }
  }

  for (int i = 0; i < m; i++) {
    int temp = w[i];
    wtype[temp]++;
    for (int j = i + 1; j < m; j++) {
      temp += w[j];
      wtype[temp]++;
    }
  }
}

void initArray() {
  for (int i = 0; i < MAX_VALUE * MAX_RANGE + 1; i++) {
    htype[i] = wtype[i] = 0;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;

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

    int hsum = 0, wsum = 0;
    for (int i = 0; i < n; i++)
      hsum += h[i];
    for (int i = 0; i < m; i++)
      wsum += w[i];

    initArray();

    calcTypeNumber(n, m);

    calcAns(hsum, wsum);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX_RANGE 1500
#define MAX_VALUE 1000
using namespace std;

int h[MAX_RANGE], w[MAX_RANGE];

int htype[MAX_VALUE + 1], wtype[MAX_VALUE + 1];

void calcAns(int hsum, int wsum) {
  int ans = 0;
  for (int i = 1; i <= min(hsum, wsum); i++)
    ans += htype[i] * wtype[i];
  cout << ans << endl;
}

void calcTypeNumber(int n, int m) {
  for (int i = 0; i < n; i++) {
    int temp = h[i];
    htype[temp]++;
    for (int j = i + 1; j < n; j++) {
      temp += h[j];
      htype[temp]++;
    }
  }

  for (int i = 0; i < m; i++) {
    int temp = w[i];
    wtype[temp]++;
    for (int j = i + 1; j < m; j++) {
      temp += w[j];
      wtype[temp]++;
    }
  }
}

void initArray() {
  for (int i = 0; i < MAX_VALUE + 1; i++) {
    htype[i] = wtype[i] = 0;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;

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

    int hsum = 0, wsum = 0;
    for (int i = 0; i < n; i++)
      hsum += h[i];
    for (int i = 0; i < m; i++)
      wsum += w[i];

    initArray();

    calcTypeNumber(n, m);

    calcAns(hsum, wsum);
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int long long

const int MAX = 1500001;

int hcount[MAX], wcount[MAX];

signed main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    fill(hcount, hcount + MAX, 0);
    fill(wcount, wcount + MAX, 0);
    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 hsum[n], wsum[m];
    hsum[0] = h[0];
    for (int i = 1; i < n; i++) {
      hsum[i] = hsum[i - 1] + h[i];
    }
    wsum[0] = w[0];
    for (int i = 1; i < m; i++) {
      wsum[i] = wsum[i - 1] + w[i];
    }
    for (int i = 0; i < n; i++) {
      hcount[hsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        hcount[hsum[i] - hsum[j]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      wcount[wsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        wcount[wsum[i] - wsum[j]]++;
      }
    }
    int count = 0;
    for (int i = 1; i < MAX; i++) {
      count += hcount[i] * wcount[i];
    }
    cout << count << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int long long

const int MAX = 1501;

signed main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    int hcount[MAX], wcount[MAX];
    fill(hcount, hcount + MAX, 0);
    fill(wcount, wcount + MAX, 0);
    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 hsum[n], wsum[m];
    hsum[0] = h[0];
    for (int i = 1; i < n; i++) {
      hsum[i] = hsum[i - 1] + h[i];
    }
    wsum[0] = w[0];
    for (int i = 1; i < m; i++) {
      wsum[i] = wsum[i - 1] + w[i];
    }
    for (int i = 0; i < n; i++) {
      hcount[hsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        hcount[hsum[i] - hsum[j]]++;
      }
    }
    for (int i = 0; i < m; i++) {
      wcount[wsum[i]]++;
      for (int j = i - 1; j >= 0; j--) {
        wcount[wsum[i] - wsum[j]]++;
      }
    }
    int count = 0;
    for (int i = 1; i < MAX; i++) {
      count += hcount[i] * wcount[i];
    }
    cout << count << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    map<int, int> mp1, mp2;
    rep(i, n + 1) rep(j, i) mp1[s1[i] - s1[j]]++;
    rep(i, m + 1) rep(j, i) mp2[s2[i] - s2[j]]++;
    for (auto p : mp1) {
      cnt += p.second * mp2[p.first];
    }
    printf("%d\\n", cnt);
  }
}

Error: TLE

Fixed submission:

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

int s1[2000], s2[2000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) scanf("%d", &s1[i + 1]);
    rep(i, m) scanf("%d", &s2[i + 1]);
    for (int i = 1; i <= n; i++)
      s1[i] += s1[i - 1];
    for (int i = 1; i <= m; i++)
      s2[i] += s2[i - 1];
    int cnt = 0;
    rep(i, n + 1) rep(j, m + 1) rep(k, i) rep(l, j) {
      if (s1[i] - s1[k] == s2[j] - s2[l])
        cnt++;
    }
    printf("%d\\n", cnt);
  }
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int main() {
  int h[1501], w[1501];
  int n, m, i, j;

  while (scanf("%d%d", &n, &m) && (n | m)) {
    int dat[1500001] = {};
    n++, m++;
    h[0] = w[0] = 0;
    for (i = 1; i < n; i++) {
      scanf("%d", &h[i]);
      h[i] += h[i - 1];
    }
    for (i = 1; i < m; i++) {
      scanf("%d", &w[i]);
      w[i] += w[i - 1];
    }

    for (i = 0; i < m - 1; i++)
      for (j = i + 1; j < m; j++)
        dat[w[j] - w[i]]++;

    int cnt = 0;
    for (i = 0; i < n - 1; i++)
      for (j = i + 1; j < n; j++)
        cnt += dat[h[j] - h[i]];

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

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int main() {
  int h[1501], w[1501];
  int n, m, i, j;

  while (scanf("%d%d", &n, &m) && (n | m)) {
    int dat[3010] = {};
    n++, m++;
    h[0] = w[0] = 0;
    for (i = 1; i < n; i++)
      scanf("%d", &h[i]);
    for (i = 1; i < m; i++)
      scanf("%d", &w[i]);

    for (i = 1; i < n - 1; i++)
      h[i + 1] += h[i];
    for (i = 1; i < m - 1; i++)
      w[i + 1] += w[i];

    for (i = 0; i < m - 1; i++)
      for (j = i + 1; j < m; j++)
        dat[w[j] - w[i]]++;

    int cnt = 0;
    for (i = 0; i < n - 1; i++)
      for (j = i + 1; j < n; j++)
        cnt += dat[h[j] - h[i]];

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

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

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M && (N | M)) {
    vector<int> y(N + 1), x(M + 1);
    for (int i = 0; i < N; i++) {
      cin >> y[i + 1];
      y[i + 1] += y[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> x[i + 1];
      x[i + 1] += x[i];
    }
    N++, M++;
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        mp[y[i] - x[j]]++;
    // 斜め45度線
    int ans = 0;
    for (auto e : mp)
      if (e.second >= 2)
        ans += e.second * (e.second - 1) / 2;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int N, M;
  while (cin >> N >> M && (N | M)) {
    vector<int> x(N + 1), y(M + 1);
    for (int i = 0; i < N; i++) {
      cin >> y[i + 1];
      y[i + 1] += y[i];
    }
    for (int i = 0; i < M; i++) {
      cin >> x[i + 1];
      x[i + 1] += x[i];
    }
    N++, M++;
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        mp[y[i] - x[j]]++;
    // 斜め45度線
    ll ans = 0;
    for (auto e : mp)
      if (e.second >= 2)
        ans += (ll)e.second * (e.second - 1) / 2;
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int m = 0, n = 0;
  while (cin >> m >> n && (m > 0 && n > 0)) {
    int h[m];
    int w[n];
    int hcum[m];
    int wcum[n];

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

    int hend = hcum[m - 1];
    int wend = wcum[n - 1];
    int hcurs[hend];
    int wcurs[wend];
    for (int i = 0; i < hend; i++)
      hcurs[i] = 0;
    for (int i = 0; i < wend; i++)
      wcurs[i] = 0;

    for (int i = 0; i < m; i++)
      hcurs[hcum[i] - 1] += 1;
    for (int i = 0; i < n; i++)
      wcurs[wcum[i] - 1] += 1;

    for (int i = 1; i < m; i++) {
      for (int j = i; j < m; j++) {
        int l = hcum[j] - hcum[i - 1];
        hcurs[l - 1] += 1;
      }
    }

    for (int i = 1; i < n; i++) {
      for (int j = i; j < n; j++) {
        int l = wcum[j] - wcum[i - 1];
        wcurs[l - 1] += 1;
      }
    }

    int cnt = 0;
    int l = hend < wend ? hend : wend;
    for (int i = 0; i < l; i++) {
      cnt += hcurs[i] * wcurs[i];
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

    int hcurs[m * (m - 1) / 2];
    int wcurs[n * (n - 1) / 2];
    int ii = 0;
    hcurs[ii++] = hcum[0];
    for (int i2 = 1; i2 < m; i2++) {
      hcurs[ii++] = hcum[i2];
    }
    for (int i1 = 1; i1 < m; i1++) {
      for (int i2 = i1; i2 < m; i2++) {
        hcurs[ii++] = hcum[i2] - hcum[i1 - 1];
      }
    }
    int jj = 0;
    wcurs[jj++] = wcum[0];
    for (int j2 = 1; j2 < n; j2++) {
      wcurs[jj++] = wcum[j2];
    }
    for (int j1 = 1; j1 < n; j1++) {
      for (int j2 = j1; j2 < n; j2++) {
        wcurs[jj++] = wcum[j2] - wcum[j1 - 1];
      }
    }

    int cnt = 0;
    for (int i = 0; i < ii; i++) {
      for (int j = 0; j < jj; j++) {
        if (hcurs[i] == wcurs[j]) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M;
  while (cin >> N >> M && !(N == 0 && M == 0)) {
    map<int, int> hor, ver;
    int h[1500], w[1500];
    for (int i = 0; i < N; i++) {
      cin >> h[i];
      int s = 0;
      for (int j = i; j >= 0; j--) {
        s += h[j];
        ver[s]++;
      }
    }
    for (int i = 0; i < M; i++) {
      cin >> w[i];
      int s = 0;
      for (int j = i; j >= 0; j--) {
        s += w[j];
        hor[s]++;
      }
    }

    int count = 0;
    for (map<int, int>::iterator p = ver.begin(); p != ver.end(); ++p)
      count += (p->second) * (hor[p->first]);

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  int i;
  while (cin >> N && cin >> M && N > 0 && N > 0) {
    int count = 0;
    vector<int> h(N + 1, 0), w(M + 1, 0);
    for (i = 1; i <= N; i++) {
      cin >> h[i];
      h[i] += h[i - 1];
    }
    for (i = 1; i <= M; i++) {
      cin >> w[i];
      w[i] += w[i - 1];
    }

    for (int j = 0; j <= M - 1; j++) {
      for (int k = j + 1; k <= M; k++) {
        int width = w[k] - w[j];
        int from = 0, to = 1;
        while (to <= N) {
          int height = h[to] - h[from];
          if (height == width)
            count++;
          if (height > width)
            from++;
          else
            to++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}